Search in sources :

Example 1 with SocketConnector

use of org.apache.mina.transport.socket.nio.SocketConnector in project camel by apache.

the class MinaComponent method createSocketEndpoint.

protected MinaEndpoint createSocketEndpoint(String uri, MinaConfiguration configuration) {
    boolean minaLogger = configuration.isMinaLogger();
    long timeout = configuration.getTimeout();
    boolean sync = configuration.isSync();
    List<IoFilter> filters = configuration.getFilters();
    final int processorCount = Runtime.getRuntime().availableProcessors() + 1;
    ExecutorService acceptorPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaSocketAcceptor");
    ExecutorService connectorPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaSocketConnector");
    ExecutorService workerPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaThreadPool");
    IoAcceptor acceptor = new SocketAcceptor(processorCount, acceptorPool);
    IoConnector connector = new SocketConnector(processorCount, connectorPool);
    SocketAddress address = new InetSocketAddress(configuration.getHost(), configuration.getPort());
    // connector config
    SocketConnectorConfig connectorConfig = new SocketConnectorConfig();
    // must use manual thread model according to Mina documentation
    connectorConfig.setThreadModel(ThreadModel.MANUAL);
    configureCodecFactory("MinaProducer", connectorConfig, configuration);
    connectorConfig.getFilterChain().addLast("threadPool", new ExecutorFilter(workerPool));
    if (minaLogger) {
        connectorConfig.getFilterChain().addLast("logger", new LoggingFilter());
    }
    appendIoFiltersToChain(filters, connectorConfig.getFilterChain());
    // set connect timeout to mina in seconds
    connectorConfig.setConnectTimeout((int) (timeout / 1000));
    // acceptor connectorConfig
    SocketAcceptorConfig acceptorConfig = new SocketAcceptorConfig();
    // must use manual thread model according to Mina documentation
    acceptorConfig.setThreadModel(ThreadModel.MANUAL);
    configureCodecFactory("MinaConsumer", acceptorConfig, configuration);
    acceptorConfig.setReuseAddress(true);
    acceptorConfig.setDisconnectOnUnbind(true);
    acceptorConfig.getFilterChain().addLast("threadPool", new ExecutorFilter(workerPool));
    if (minaLogger) {
        acceptorConfig.getFilterChain().addLast("logger", new LoggingFilter());
    }
    appendIoFiltersToChain(filters, acceptorConfig.getFilterChain());
    MinaEndpoint endpoint = new MinaEndpoint(uri, this);
    endpoint.setAddress(address);
    endpoint.setAcceptor(acceptor);
    endpoint.setAcceptorConfig(acceptorConfig);
    endpoint.setConnector(connector);
    endpoint.setConnectorConfig(connectorConfig);
    endpoint.setConfiguration(configuration);
    // enlist threads pools in use on endpoint
    endpoint.addThreadPool(acceptorPool);
    endpoint.addThreadPool(connectorPool);
    endpoint.addThreadPool(workerPool);
    // set sync or async mode after endpoint is created
    if (sync) {
        endpoint.setExchangePattern(ExchangePattern.InOut);
    } else {
        endpoint.setExchangePattern(ExchangePattern.InOnly);
    }
    return endpoint;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ExecutorFilter(org.apache.mina.filter.executor.ExecutorFilter) LoggingFilter(org.apache.mina.filter.LoggingFilter) IoFilter(org.apache.mina.common.IoFilter) Endpoint(org.apache.camel.Endpoint) SocketAcceptorConfig(org.apache.mina.transport.socket.nio.SocketAcceptorConfig) IoAcceptor(org.apache.mina.common.IoAcceptor) ExecutorService(java.util.concurrent.ExecutorService) SocketAcceptor(org.apache.mina.transport.socket.nio.SocketAcceptor) SocketConnectorConfig(org.apache.mina.transport.socket.nio.SocketConnectorConfig) IoConnector(org.apache.mina.common.IoConnector) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SocketConnector(org.apache.mina.transport.socket.nio.SocketConnector)

Example 2 with SocketConnector

use of org.apache.mina.transport.socket.nio.SocketConnector in project camel by apache.

the class MinaProducerShutdownMockTest method testProducerShutdownTestingWithMock.

@Test
public void testProducerShutdownTestingWithMock() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedBodiesReceived("Hello World");
    // create our mock and record expected behavior = that worker timeout should be set to 0
    SocketConnector mockConnector = createMock(SocketConnector.class);
    mockConnector.setWorkerTimeout(0);
    replay(mockConnector);
    // normal camel code to get a producer
    Endpoint endpoint = context.getEndpoint("mina:tcp://localhost:{{port}}?textline=true&sync=false");
    Exchange exchange = endpoint.createExchange();
    Producer producer = endpoint.createProducer();
    producer.start();
    // set input and execute it
    exchange.getIn().setBody("Hello World");
    producer.process(exchange);
    // insert our mock instead of real MINA IoConnector
    Field field = producer.getClass().getDeclaredField("connector");
    field.setAccessible(true);
    field.set(producer, mockConnector);
    // stop using our mock
    producer.stop();
    verify(mockConnector);
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Field(java.lang.reflect.Field) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Producer(org.apache.camel.Producer) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) SocketConnector(org.apache.mina.transport.socket.nio.SocketConnector) Test(org.junit.Test)

Example 3 with SocketConnector

use of org.apache.mina.transport.socket.nio.SocketConnector in project dubbo by alibaba.

the class MinaClient method doOpen.

@Override
protected void doOpen() throws Throwable {
    connectorKey = getUrl().toFullString();
    SocketConnector c = connectors.get(connectorKey);
    if (c != null) {
        connector = c;
    } else {
        // set thread pool.
        connector = new SocketConnector(Constants.DEFAULT_IO_THREADS, Executors.newCachedThreadPool(new NamedThreadFactory("MinaClientWorker", true)));
        // config
        SocketConnectorConfig cfg = (SocketConnectorConfig) connector.getDefaultConfig();
        cfg.setThreadModel(ThreadModel.MANUAL);
        cfg.getSessionConfig().setTcpNoDelay(true);
        cfg.getSessionConfig().setKeepAlive(true);
        int timeout = getTimeout();
        cfg.setConnectTimeout(timeout < 1000 ? 1 : timeout / 1000);
        // set codec.
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this)));
        connectors.put(connectorKey, connector);
    }
}
Also used : NamedThreadFactory(com.alibaba.dubbo.common.utils.NamedThreadFactory) SocketConnectorConfig(org.apache.mina.transport.socket.nio.SocketConnectorConfig) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) SocketConnector(org.apache.mina.transport.socket.nio.SocketConnector)

Example 4 with SocketConnector

use of org.apache.mina.transport.socket.nio.SocketConnector in project dubbo by alibaba.

the class MinaClient method doOpen.

@Override
protected void doOpen() throws Throwable {
    connectorKey = getUrl().toFullString();
    SocketConnector c = CONNECTORS.get(connectorKey);
    if (c != null) {
        connector = c;
    } else {
        // set thread pool.
        connector = new SocketConnector(Constants.DEFAULT_IO_THREADS, Executors.newCachedThreadPool(new NamedThreadFactory("MinaClientWorker", true)));
        // config
        SocketConnectorConfig cfg = (SocketConnectorConfig) connector.getDefaultConfig();
        cfg.setThreadModel(ThreadModel.MANUAL);
        cfg.getSessionConfig().setTcpNoDelay(true);
        cfg.getSessionConfig().setKeepAlive(true);
        int timeout = getConnectTimeout();
        cfg.setConnectTimeout(timeout < 1000 ? 1 : timeout / 1000);
        // set codec.
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this)));
        CONNECTORS.put(connectorKey, connector);
    }
}
Also used : NamedThreadFactory(org.apache.dubbo.common.utils.NamedThreadFactory) SocketConnectorConfig(org.apache.mina.transport.socket.nio.SocketConnectorConfig) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) SocketConnector(org.apache.mina.transport.socket.nio.SocketConnector)

Aggregations

SocketConnector (org.apache.mina.transport.socket.nio.SocketConnector)4 SocketConnectorConfig (org.apache.mina.transport.socket.nio.SocketConnectorConfig)3 Endpoint (org.apache.camel.Endpoint)2 ProtocolCodecFilter (org.apache.mina.filter.codec.ProtocolCodecFilter)2 NamedThreadFactory (com.alibaba.dubbo.common.utils.NamedThreadFactory)1 Field (java.lang.reflect.Field)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 ExecutorService (java.util.concurrent.ExecutorService)1 Exchange (org.apache.camel.Exchange)1 Producer (org.apache.camel.Producer)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 NamedThreadFactory (org.apache.dubbo.common.utils.NamedThreadFactory)1 IoAcceptor (org.apache.mina.common.IoAcceptor)1 IoConnector (org.apache.mina.common.IoConnector)1 IoFilter (org.apache.mina.common.IoFilter)1 LoggingFilter (org.apache.mina.filter.LoggingFilter)1 ExecutorFilter (org.apache.mina.filter.executor.ExecutorFilter)1 SocketAcceptor (org.apache.mina.transport.socket.nio.SocketAcceptor)1 SocketAcceptorConfig (org.apache.mina.transport.socket.nio.SocketAcceptorConfig)1