Search in sources :

Example 1 with IoSession

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

the class MinaLoggerOptionTest method testNoLoggerOption.

@Test
public void testNoLoggerOption() throws Exception {
    final String uri = "mina:tcp://localhost:{{port}}?textline=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);
    assertFalse("There should NOT default 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 2 with IoSession

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

the class MinaLoggerOptionTest method testLoggerOptionFalse.

@Test
public void testLoggerOptionFalse() throws Exception {
    final String uri = "mina:tcp://localhost:{{port}}?textline=true&minaLogger=false&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);
    assertFalse("There should NOT 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 3 with IoSession

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

the class MinaServer method getChannels.

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(com.alibaba.dubbo.remoting.Channel) IoSession(org.apache.mina.common.IoSession) HashSet(java.util.HashSet)

Example 4 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() {

        @Override
        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.removeChannelIfDisconnected(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.removeChannelIfDisconnected(newSession);
                            }
                        } else {
                            MinaClient.this.session = newSession;
                        }
                    }
                }
            } catch (Exception e) {
                exception.set(e);
            } finally {
                finish.countDown();
            }
        }
    });
    try {
        finish.await(getConnectTimeout(), TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        throw new RemotingException(this, "client(url: " + getUrl() + ") failed to connect to server " + getRemoteAddress() + " client-side timeout " + getConnectTimeout() + "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(org.apache.dubbo.remoting.RemotingException) IoFutureListener(org.apache.mina.common.IoFutureListener) RemotingException(org.apache.dubbo.remoting.RemotingException) IoSession(org.apache.mina.common.IoSession)

Example 5 with IoSession

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

the class MinaUdpProtocolCodecFactory method getEncoder.

public ProtocolEncoder getEncoder() throws Exception {
    return new ProtocolEncoder() {

        public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
            ByteBuffer buf = toByteBuffer(message);
            buf.flip();
            out.write(buf);
        }

        public void dispose(IoSession session) throws Exception {
        // do nothing
        }
    };
}
Also used : ProtocolEncoder(org.apache.mina.filter.codec.ProtocolEncoder) ByteBuffer(org.apache.mina.common.ByteBuffer) IoSession(org.apache.mina.common.IoSession) ProtocolEncoderOutput(org.apache.mina.filter.codec.ProtocolEncoderOutput)

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