Search in sources :

Example 1 with NioSocketConnector

use of org.apache.mina.transport.socket.nio.NioSocketConnector in project directory-ldap-api by apache.

the class LdapNetworkConnection method createConnector.

/**
 * Create the connector
 */
private void createConnector() throws LdapException {
    // Use only one thread inside the connector
    connector = new NioSocketConnector(1);
    if (connectionConfig != null) {
        ((SocketSessionConfig) connector.getSessionConfig()).setAll(connectionConfig);
    } else {
        ((SocketSessionConfig) connector.getSessionConfig()).setReuseAddress(true);
    }
    // Add the codec to the chain
    connector.getFilterChain().addLast("ldapCodec", ldapProtocolFilter);
    // If we use SSL, we have to add the SslFilter to the chain
    if (config.isUseSsl()) {
        addSslFilter();
    }
    // Inject the protocolHandler
    connector.setHandler(this);
}
Also used : NioSocketConnector(org.apache.mina.transport.socket.nio.NioSocketConnector) SocketSessionConfig(org.apache.mina.transport.socket.SocketSessionConfig)

Example 2 with NioSocketConnector

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

the class MinaClient method main.

/**
 * The entry point of application.
 *
 * @param args the input arguments
 */
/*
    * 测试服务端与客户端程序!
    a. 启动服务端,然后再启动客户端(客户端发送的消息是"why are you so diao ")
    b. 服务端接收消息并处理成功;
    */
public static void main(String[] args) {
    // 创建一个非阻塞的客户端程序
    IoConnector connector = new NioSocketConnector();
    // 设置链接超时时间
    connector.setConnectTimeout(30000);
    ProtocolCodecFilter pf = new ProtocolCodecFilter((new MyTextLineCodecFactory(Charset.forName("utf-8"), "\r\n")));
    // 添加过滤器
    connector.getFilterChain().addLast("codec", pf);
    // 添加业务逻辑处理器类
    connector.setHandler(new MinaClientHandler());
    IoSession session = null;
    try {
        ConnectFuture future = connector.connect(new InetSocketAddress(HOST, // 创建连接
        PORT));
        // 等待连接创建完成
        future.awaitUninterruptibly();
        // 获得session
        session = future.getSession();
        String msg = "hello \r\n";
        // 发送消息
        session.write(msg);
        logger.info("客户端与服务端建立连接成功...发送的消息为:" + msg);
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("客户端链接异常...", e);
    }
    // 等待连接断开
    session.getCloseFuture().awaitUninterruptibly();
    connector.dispose();
}
Also used : NioSocketConnector(org.apache.mina.transport.socket.nio.NioSocketConnector) InetSocketAddress(java.net.InetSocketAddress) ConnectFuture(org.apache.mina.core.future.ConnectFuture) IoConnector(org.apache.mina.core.service.IoConnector) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter) IoSession(org.apache.mina.core.session.IoSession)

Example 3 with NioSocketConnector

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

the class ClientTestServer method creatClient.

/**
 * Creat client io connector.
 *
 * @return the io connector
 */
public IoConnector creatClient() {
    IoConnector connector = new NioSocketConnector();
    connector.setConnectTimeoutMillis(30000);
    connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
    connector.setHandler(new MinaClientHandler());
    return connector;
}
Also used : NioSocketConnector(org.apache.mina.transport.socket.nio.NioSocketConnector) MinaClientHandler(com.pancm.test.minatest.demo.MinaClientHandler) ObjectSerializationCodecFactory(org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory) IoConnector(org.apache.mina.core.service.IoConnector) ProtocolCodecFilter(org.apache.mina.filter.codec.ProtocolCodecFilter)

Example 4 with NioSocketConnector

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

the class ConnectionFactoryConnectorPoolImpl method getSocketConnector.

private static final NioSocketConnector getSocketConnector(long timeout, IoHandler handler) {
    NioSocketConnector connector = new NioSocketConnector();
    connector.setHandler(handler);
    connector.setConnectTimeoutMillis(timeout);
    return connector;
}
Also used : NioSocketConnector(org.apache.mina.transport.socket.nio.NioSocketConnector)

Example 5 with NioSocketConnector

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

the class Mina2Consumer method setupClientSocketProtocol.

protected void setupClientSocketProtocol(String uri, Mina2Configuration configuration) 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);
    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("Mina2Consumer", connector, configuration);
    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)

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