Search in sources :

Example 1 with SocketConnectorConfig

use of org.apache.mina.transport.socket.nio.SocketConnectorConfig 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 SocketConnectorConfig

use of org.apache.mina.transport.socket.nio.SocketConnectorConfig 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 3 with SocketConnectorConfig

use of org.apache.mina.transport.socket.nio.SocketConnectorConfig 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)3 SocketConnectorConfig (org.apache.mina.transport.socket.nio.SocketConnectorConfig)3 ProtocolCodecFilter (org.apache.mina.filter.codec.ProtocolCodecFilter)2 NamedThreadFactory (com.alibaba.dubbo.common.utils.NamedThreadFactory)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 ExecutorService (java.util.concurrent.ExecutorService)1 Endpoint (org.apache.camel.Endpoint)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