Search in sources :

Example 6 with NioSocketConnector

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

the class Mina2Producer method setupSocketProtocol.

protected void setupSocketProtocol(String uri) throws Exception {
    boolean minaLogger = configuration.isMinaLogger();
    long timeout = configuration.getTimeout();
    List<IoFilter> filters = configuration.getFilters();
    address = new InetSocketAddress(configuration.getHost(), configuration.getPort());
    final int processorCount = Runtime.getRuntime().availableProcessors() + 1;
    connector = new NioSocketConnector(processorCount);
    // connector config
    connectorConfig = connector.getSessionConfig();
    if (configuration.isOrderedThreadPoolExecutor()) {
        workerPool = new OrderedThreadPoolExecutor(configuration.getMaximumPoolSize());
    } else {
        workerPool = new UnorderedThreadPoolExecutor(configuration.getMaximumPoolSize());
    }
    connector.getFilterChain().addLast("threadPool", new ExecutorFilter(workerPool));
    if (minaLogger) {
        connector.getFilterChain().addLast("logger", new LoggingFilter());
    }
    appendIoFiltersToChain(filters, connector.getFilterChain());
    if (configuration.getSslContextParameters() != null) {
        SslFilter filter = new SslFilter(configuration.getSslContextParameters().createSSLContext(getEndpoint().getCamelContext()), configuration.isAutoStartTls());
        filter.setUseClientMode(true);
        connector.getFilterChain().addFirst("sslFilter", filter);
    }
    configureCodecFactory("Mina2Producer", connector);
    connector.setConnectTimeoutMillis(timeout);
}
Also used : SslFilter(org.apache.mina.filter.ssl.SslFilter) NioSocketConnector(org.apache.mina.transport.socket.nio.NioSocketConnector) UnorderedThreadPoolExecutor(org.apache.mina.filter.executor.UnorderedThreadPoolExecutor) OrderedThreadPoolExecutor(org.apache.mina.filter.executor.OrderedThreadPoolExecutor) InetSocketAddress(java.net.InetSocketAddress) ExecutorFilter(org.apache.mina.filter.executor.ExecutorFilter) LoggingFilter(org.apache.mina.filter.logging.LoggingFilter) IoFilter(org.apache.mina.core.filterchain.IoFilter)

Example 7 with NioSocketConnector

use of org.apache.mina.transport.socket.nio.NioSocketConnector in project opennms by OpenNMS.

the class ConnectionFactoryNewConnectorImpl method getSocketConnector.

private static final NioSocketConnector getSocketConnector(long timeout, IoHandler handler) {
    // Create a new NioSocketConnector
    // NioSocketConnector connector = new NioSocketConnector(m_executor, m_processor);
    // To address bug NMS-6412, I'm changing this to use the default constructor so that the
    // Executor pool and IoProcessor pools are created and destroyed for every connector.
    // This slows things down but might be an acceptable tradeoff for more reliable detection.
    // 
    // @see http://issues.opennms.org/browse/NMS-6412
    // 
    NioSocketConnector connector = new NioSocketConnector(Runtime.getRuntime().availableProcessors());
    // Enable SO_REUSEADDR on the socket so that TIMED_WAIT connections that are bound on the
    // same port do not block new outgoing connections. If the connections are blocked, then
    // the following exception will be thrown under heavy load:
    // 
    // Caused by: java.net.SocketException: Invalid argument
    // at sun.nio.ch.Net.connect(Native Method)
    // at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:500)
    // at org.apache.mina.transport.socket.nio.NioSocketConnector.connect(NioSocketConnector.java:188)
    // ...
    // 
    // @see http://issues.opennms.org/browse/NMS-5469
    // 
    connector.getSessionConfig().setReuseAddress(true);
    // Setting SO_LINGER will prevent TIME_WAIT sockets altogether because TCP connections will be
    // forcefully terminated with RST packets. However, this doesn't seem to be necessary to maintain
    // performance of the outgoing connections. As long as SO_REUSEADDR is set, the operating system
    // and Java appear to recycle the ports effectively although some will remain in TIME_WAIT state.
    // 
    // @see http://issues.opennms.org/browse/NMS-5469
    // 
    // connector.getSessionConfig().setSoLinger(0);
    connector.setHandler(handler);
    connector.setConnectTimeoutMillis(timeout);
    return connector;
}
Also used : NioSocketConnector(org.apache.mina.transport.socket.nio.NioSocketConnector)

Aggregations

NioSocketConnector (org.apache.mina.transport.socket.nio.NioSocketConnector)7 InetSocketAddress (java.net.InetSocketAddress)3 IoFilter (org.apache.mina.core.filterchain.IoFilter)2 IoConnector (org.apache.mina.core.service.IoConnector)2 ProtocolCodecFilter (org.apache.mina.filter.codec.ProtocolCodecFilter)2 ExecutorFilter (org.apache.mina.filter.executor.ExecutorFilter)2 OrderedThreadPoolExecutor (org.apache.mina.filter.executor.OrderedThreadPoolExecutor)2 UnorderedThreadPoolExecutor (org.apache.mina.filter.executor.UnorderedThreadPoolExecutor)2 LoggingFilter (org.apache.mina.filter.logging.LoggingFilter)2 SslFilter (org.apache.mina.filter.ssl.SslFilter)2 MinaClientHandler (com.pancm.test.minatest.demo.MinaClientHandler)1 ConnectFuture (org.apache.mina.core.future.ConnectFuture)1 IoSession (org.apache.mina.core.session.IoSession)1 ObjectSerializationCodecFactory (org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory)1 SocketSessionConfig (org.apache.mina.transport.socket.SocketSessionConfig)1