Search in sources :

Example 1 with SocketAcceptorConfig

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

the class MinaServer method doOpen.

@Override
protected void doOpen() throws Throwable {
    // set thread pool.
    acceptor = new SocketAcceptor(getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS), Executors.newCachedThreadPool(new NamedThreadFactory("MinaServerWorker", true)));
    // config
    SocketAcceptorConfig cfg = (SocketAcceptorConfig) acceptor.getDefaultConfig();
    cfg.setThreadModel(ThreadModel.MANUAL);
    // set codec.
    acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MinaCodecAdapter(getCodec(), getUrl(), this)));
    acceptor.bind(getBindAddress(), new MinaHandler(getUrl(), this));
}
Also used : NamedThreadFactory(com.alibaba.dubbo.common.utils.NamedThreadFactory) SocketAcceptorConfig(org.apache.mina.transport.socket.nio.SocketAcceptorConfig) SocketAcceptor(org.apache.mina.transport.socket.nio.SocketAcceptor) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter)

Example 2 with SocketAcceptorConfig

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

the class ReverserServer method start.

public void start() throws Exception {
    acceptor = new SocketAcceptor();
    // Prepare the configuration
    SocketAcceptorConfig cfg = new SocketAcceptorConfig();
    cfg.setReuseAddress(true);
    Charset charset = Charset.forName("UTF-8");
    cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(charset)));
    // Bind
    acceptor.bind(new InetSocketAddress(port), new ReverseProtocolHandler(), cfg);
}
Also used : SocketAcceptorConfig(org.apache.mina.transport.socket.nio.SocketAcceptorConfig) InetSocketAddress(java.net.InetSocketAddress) SocketAcceptor(org.apache.mina.transport.socket.nio.SocketAcceptor) Charset(java.nio.charset.Charset) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) TextLineCodecFactory(org.apache.mina.filter.codec.textline.TextLineCodecFactory)

Example 3 with SocketAcceptorConfig

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

Aggregations

SocketAcceptor (org.apache.mina.transport.socket.nio.SocketAcceptor)3 SocketAcceptorConfig (org.apache.mina.transport.socket.nio.SocketAcceptorConfig)3 InetSocketAddress (java.net.InetSocketAddress)2 ProtocolCodecFilter (org.apache.mina.filter.codec.ProtocolCodecFilter)2 NamedThreadFactory (com.alibaba.dubbo.common.utils.NamedThreadFactory)1 SocketAddress (java.net.SocketAddress)1 Charset (java.nio.charset.Charset)1 ExecutorService (java.util.concurrent.ExecutorService)1 Endpoint (org.apache.camel.Endpoint)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 TextLineCodecFactory (org.apache.mina.filter.codec.textline.TextLineCodecFactory)1 ExecutorFilter (org.apache.mina.filter.executor.ExecutorFilter)1 SocketConnector (org.apache.mina.transport.socket.nio.SocketConnector)1 SocketConnectorConfig (org.apache.mina.transport.socket.nio.SocketConnectorConfig)1