Search in sources :

Example 6 with IoSession

use of org.apache.mina.common.IoSession in project camel by apache.

the class MinaLoggerOptionTest method testLoggerOptionTrue.

@Test
public void testLoggerOptionTrue() throws Exception {
    final String uri = "mina:tcp://localhost:{{port}}?textline=true&minaLogger=true&sync=false";
    context.addRoutes(new RouteBuilder() {

        public void configure() throws Exception {
            from(uri).to("mock:result");
        }
    });
    MockEndpoint mock = this.getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    Endpoint endpoint = context.getEndpoint(uri);
    Exchange exchange = endpoint.createExchange();
    Producer producer = endpoint.createProducer();
    producer.start();
    // set input and execute it
    exchange.getIn().setBody("Hello World");
    producer.process(exchange);
    Field field = producer.getClass().getDeclaredField("session");
    field.setAccessible(true);
    IoSession session = (IoSession) field.get(producer);
    assertTrue("There should be a logger filter", session.getFilterChain().contains("logger"));
    producer.stop();
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Field(java.lang.reflect.Field) RouteBuilder(org.apache.camel.builder.RouteBuilder) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Producer(org.apache.camel.Producer) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) IoSession(org.apache.mina.common.IoSession) Test(org.junit.Test)

Example 7 with IoSession

use of org.apache.mina.common.IoSession in project dubbo by alibaba.

the class MinaClient method doConnect.

@Override
protected void doConnect() throws Throwable {
    ConnectFuture future = connector.connect(getConnectAddress(), new MinaHandler(getUrl(), this));
    long start = System.currentTimeMillis();
    final AtomicReference<Throwable> exception = new AtomicReference<Throwable>();
    // resolve future.awaitUninterruptibly() dead lock
    final CountDownLatch finish = new CountDownLatch(1);
    future.addListener(new IoFutureListener() {

        public void operationComplete(IoFuture future) {
            try {
                if (future.isReady()) {
                    IoSession newSession = future.getSession();
                    try {
                        // Close old channel
                        // copy reference
                        IoSession oldSession = MinaClient.this.session;
                        if (oldSession != null) {
                            try {
                                if (logger.isInfoEnabled()) {
                                    logger.info("Close old mina channel " + oldSession + " on create new mina channel " + newSession);
                                }
                                oldSession.close();
                            } finally {
                                MinaChannel.removeChannelIfDisconnectd(oldSession);
                            }
                        }
                    } finally {
                        if (MinaClient.this.isClosed()) {
                            try {
                                if (logger.isInfoEnabled()) {
                                    logger.info("Close new mina channel " + newSession + ", because the client closed.");
                                }
                                newSession.close();
                            } finally {
                                MinaClient.this.session = null;
                                MinaChannel.removeChannelIfDisconnectd(newSession);
                            }
                        } else {
                            MinaClient.this.session = newSession;
                        }
                    }
                }
            } catch (Exception e) {
                exception.set(e);
            } finally {
                finish.countDown();
            }
        }
    });
    try {
        finish.await(getTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server " + getRemoteAddress() + " client-side timeout " + getTimeout() + "ms (elapsed: " + (System.currentTimeMillis() - start) + "ms) from netty client " + NetUtils.getLocalHost() + " using dubbo version " + Version.getVersion() + ", cause: " + e.getMessage(), e);
    }
    Throwable e = exception.get();
    if (e != null) {
        throw e;
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IoFuture(org.apache.mina.common.IoFuture) ConnectFuture(org.apache.mina.common.ConnectFuture) CountDownLatch(java.util.concurrent.CountDownLatch) RemotingException(com.alibaba.dubbo.remoting.RemotingException) IoFutureListener(org.apache.mina.common.IoFutureListener) RemotingException(com.alibaba.dubbo.remoting.RemotingException) IoSession(org.apache.mina.common.IoSession)

Example 8 with IoSession

use of org.apache.mina.common.IoSession in project dubbo by alibaba.

the class MinaServer method getChannels.

@Override
public Collection<Channel> getChannels() {
    Set<IoSession> sessions = acceptor.getManagedSessions(getBindAddress());
    Collection<Channel> channels = new HashSet<Channel>();
    for (IoSession session : sessions) {
        if (session.isConnected()) {
            channels.add(MinaChannel.getOrAddChannel(session, getUrl(), this));
        }
    }
    return channels;
}
Also used : Channel(org.apache.dubbo.remoting.Channel) IoSession(org.apache.mina.common.IoSession) HashSet(java.util.HashSet)

Aggregations

IoSession (org.apache.mina.common.IoSession)8 Field (java.lang.reflect.Field)3 Endpoint (org.apache.camel.Endpoint)3 Exchange (org.apache.camel.Exchange)3 Producer (org.apache.camel.Producer)3 RouteBuilder (org.apache.camel.builder.RouteBuilder)3 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ConnectFuture (org.apache.mina.common.ConnectFuture)2 IoFuture (org.apache.mina.common.IoFuture)2 IoFutureListener (org.apache.mina.common.IoFutureListener)2 Channel (com.alibaba.dubbo.remoting.Channel)1 RemotingException (com.alibaba.dubbo.remoting.RemotingException)1 Channel (org.apache.dubbo.remoting.Channel)1 RemotingException (org.apache.dubbo.remoting.RemotingException)1 ByteBuffer (org.apache.mina.common.ByteBuffer)1 ProtocolEncoder (org.apache.mina.filter.codec.ProtocolEncoder)1