Search in sources :

Example 6 with InetSocketAddress

use of java.net.InetSocketAddress in project camel by apache.

the class MinaComponent method createDatagramEndpoint.

protected MinaEndpoint createDatagramEndpoint(String uri, MinaConfiguration configuration) {
    boolean minaLogger = configuration.isMinaLogger();
    long timeout = configuration.getTimeout();
    boolean transferExchange = configuration.isTransferExchange();
    boolean sync = configuration.isSync();
    List<IoFilter> filters = configuration.getFilters();
    ExecutorService acceptorPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaDatagramAcceptor");
    ExecutorService connectorPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaDatagramConnector");
    ExecutorService workerPool = getCamelContext().getExecutorServiceManager().newCachedThreadPool(this, "MinaThreadPool");
    IoAcceptor acceptor = new DatagramAcceptor(acceptorPool);
    IoConnector connector = new DatagramConnector(connectorPool);
    SocketAddress address = new InetSocketAddress(configuration.getHost(), configuration.getPort());
    if (transferExchange) {
        throw new IllegalArgumentException("transferExchange=true is not supported for datagram protocol");
    }
    DatagramConnectorConfig connectorConfig = new DatagramConnectorConfig();
    // must use manual thread model according to Mina documentation
    connectorConfig.setThreadModel(ThreadModel.MANUAL);
    configureDataGramCodecFactory("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));
    DatagramAcceptorConfig acceptorConfig = new DatagramAcceptorConfig();
    // must use manual thread model according to Mina documentation
    acceptorConfig.setThreadModel(ThreadModel.MANUAL);
    configureDataGramCodecFactory("MinaConsumer", acceptorConfig, configuration);
    acceptorConfig.setDisconnectOnUnbind(true);
    // reuse address is default true for datagram
    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) DatagramConnector(org.apache.mina.transport.socket.nio.DatagramConnector) DatagramConnectorConfig(org.apache.mina.transport.socket.nio.DatagramConnectorConfig) DatagramAcceptorConfig(org.apache.mina.transport.socket.nio.DatagramAcceptorConfig) DatagramAcceptor(org.apache.mina.transport.socket.nio.DatagramAcceptor) IoAcceptor(org.apache.mina.common.IoAcceptor) ExecutorService(java.util.concurrent.ExecutorService) IoConnector(org.apache.mina.common.IoConnector) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 7 with InetSocketAddress

use of java.net.InetSocketAddress in project camel by apache.

the class MinaTcpLineDelimiterUsingPlainSocketTest method sendAndReceive.

private String sendAndReceive(String input) throws IOException {
    byte[] buf = new byte[128];
    Socket soc = new Socket();
    soc.connect(new InetSocketAddress("localhost", getPort()));
    // Send message using plain Socket to test if this works
    OutputStream os = null;
    InputStream is = null;
    try {
        os = soc.getOutputStream();
        // must append MAC newline at the end to flag end of textline to Camel-Mina
        os.write((input + "\r").getBytes());
        is = soc.getInputStream();
        int len = is.read(buf);
        if (len == -1) {
            // no data received
            return null;
        }
    } finally {
        IOHelper.close(is, os);
        soc.close();
    }
    // convert the buffer to chars
    StringBuilder sb = new StringBuilder();
    for (byte b : buf) {
        char ch = (char) b;
        if (ch == '\r' || ch == 0) {
            // use MAC delimiter denotes end of text (added in the end in the processor below)
            break;
        } else {
            sb.append(ch);
        }
    }
    return sb.toString();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket)

Example 8 with InetSocketAddress

use of java.net.InetSocketAddress in project camel by apache.

the class Mina2Consumer method doStop.

@Override
protected void doStop() throws Exception {
    if (configuration.isClientMode() && configuration.getProtocol().equals("tcp")) {
        LOG.info("Disconnect from server address: {} using connector: {}", address, connector);
        if (session != null) {
            CloseFuture closeFuture = session.closeNow();
            closeFuture.awaitUninterruptibly();
        }
        connector.dispose(true);
    } else {
        LOG.info("Unbinding from server address: {} using acceptor: {}", address, acceptor);
        if (address instanceof InetSocketAddress) {
            // need to check if the address is IPV4 all network address
            if ("0.0.0.0".equals(((InetSocketAddress) address).getAddress().getHostAddress())) {
                LOG.info("Unbind the server address {}", acceptor.getLocalAddresses());
                acceptor.unbind(acceptor.getLocalAddresses());
            } else {
                acceptor.unbind(address);
            }
        } else {
            acceptor.unbind(address);
        }
    }
    super.doStop();
}
Also used : CloseFuture(org.apache.mina.core.future.CloseFuture) InetSocketAddress(java.net.InetSocketAddress)

Example 9 with InetSocketAddress

use of java.net.InetSocketAddress in project camel by apache.

the class Mina2Consumer method setupDatagramProtocol.

protected void setupDatagramProtocol(String uri, Mina2Configuration configuration) {
    boolean minaLogger = configuration.isMinaLogger();
    List<IoFilter> filters = configuration.getFilters();
    address = new InetSocketAddress(configuration.getHost(), configuration.getPort());
    acceptor = new NioDatagramAcceptor();
    // acceptor connectorConfig
    configureDataGramCodecFactory("Mina2Consumer", acceptor, configuration);
    acceptor.setCloseOnDeactivation(true);
    // reuse address is default true for datagram
    if (configuration.isOrderedThreadPoolExecutor()) {
        workerPool = new OrderedThreadPoolExecutor(configuration.getMaximumPoolSize());
    } else {
        workerPool = new UnorderedThreadPoolExecutor(configuration.getMaximumPoolSize());
    }
    acceptor.getFilterChain().addLast("threadPool", new ExecutorFilter(workerPool));
    if (minaLogger) {
        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
    }
    appendIoFiltersToChain(filters, acceptor.getFilterChain());
    if (configuration.getSslContextParameters() != null) {
        LOG.warn("Using datagram protocol, " + configuration.getProtocol() + ", but an SSLContextParameters instance was provided.  SSLContextParameters is only supported on the TCP protocol.");
    }
}
Also used : 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) NioDatagramAcceptor(org.apache.mina.transport.socket.nio.NioDatagramAcceptor)

Example 10 with InetSocketAddress

use of java.net.InetSocketAddress in project camel by apache.

the class Mina2TcpLineDelimiterUsingPlainSocketTest method sendAndReceive.

private String sendAndReceive(String input) throws IOException {
    byte[] buf = new byte[128];
    Socket soc = new Socket();
    soc.connect(new InetSocketAddress("localhost", getPort()));
    // Send message using plain Socket to test if this works
    OutputStream os = null;
    InputStream is = null;
    try {
        os = soc.getOutputStream();
        // must append MAC newline at the end to flag end of textline to camel-mina2
        os.write((input + "\r").getBytes());
        is = soc.getInputStream();
        int len = is.read(buf);
        if (len == -1) {
            // no data received
            return null;
        }
    } finally {
        IOHelper.close(is, os);
        soc.close();
    }
    // convert the buffer to chars
    StringBuilder sb = new StringBuilder();
    for (byte b : buf) {
        char ch = (char) b;
        if (ch == '\r' || ch == 0) {
            // use MAC delimiter denotes end of text (added in the end in the processor below)
            break;
        } else {
            sb.append(ch);
        }
    }
    return sb.toString();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Socket(java.net.Socket)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2586 Test (org.junit.Test)595 IOException (java.io.IOException)592 Socket (java.net.Socket)345 InetAddress (java.net.InetAddress)242 SocketAddress (java.net.SocketAddress)176 ServerSocket (java.net.ServerSocket)170 ArrayList (java.util.ArrayList)168 Configuration (org.apache.hadoop.conf.Configuration)140 ByteBuffer (java.nio.ByteBuffer)129 UnknownHostException (java.net.UnknownHostException)122 InputStream (java.io.InputStream)102 OutputStream (java.io.OutputStream)101 SocketChannel (java.nio.channels.SocketChannel)101 SocketException (java.net.SocketException)89 File (java.io.File)88 HashMap (java.util.HashMap)78 URI (java.net.URI)72 Proxy (java.net.Proxy)65 SocketTimeoutException (java.net.SocketTimeoutException)65