Search in sources :

Example 1 with NioDatagramWorkerPool

use of org.jboss.netty.channel.socket.nio.NioDatagramWorkerPool in project camel by apache.

the class NettyProducer method setupUDPCommunication.

protected void setupUDPCommunication() throws Exception {
    if (datagramChannelFactory == null) {
        int count = configuration.getWorkerCount() > 0 ? configuration.getWorkerCount() : NettyHelper.DEFAULT_IO_THREADS;
        workerPool = new NioDatagramWorkerPool(Executors.newCachedThreadPool(), count);
        datagramChannelFactory = new NioDatagramChannelFactory(workerPool);
    }
}
Also used : NioDatagramChannelFactory(org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory) NioDatagramWorkerPool(org.jboss.netty.channel.socket.nio.NioDatagramWorkerPool)

Example 2 with NioDatagramWorkerPool

use of org.jboss.netty.channel.socket.nio.NioDatagramWorkerPool in project camel by apache.

the class SingleUDPNettyServerBootstrapFactory method startServerBootstrap.

protected void startServerBootstrap() throws Exception {
    // create non-shared worker pool
    int count = configuration.getWorkerCount() > 0 ? configuration.getWorkerCount() : NettyHelper.DEFAULT_IO_THREADS;
    workerPool = new NioDatagramWorkerPool(Executors.newCachedThreadPool(), count);
    datagramChannelFactory = new NioDatagramChannelFactory(workerPool);
    connectionlessBootstrap = new ConnectionlessBootstrap(datagramChannelFactory);
    connectionlessBootstrap.setOption("child.keepAlive", configuration.isKeepAlive());
    connectionlessBootstrap.setOption("child.tcpNoDelay", configuration.isTcpNoDelay());
    connectionlessBootstrap.setOption("reuseAddress", configuration.isReuseAddress());
    connectionlessBootstrap.setOption("child.reuseAddress", configuration.isReuseAddress());
    connectionlessBootstrap.setOption("child.connectTimeoutMillis", configuration.getConnectTimeout());
    connectionlessBootstrap.setOption("child.broadcast", configuration.isBroadcast());
    connectionlessBootstrap.setOption("sendBufferSize", configuration.getSendBufferSize());
    connectionlessBootstrap.setOption("receiveBufferSize", configuration.getReceiveBufferSize());
    // only set this if user has specified
    if (configuration.getReceiveBufferSizePredictor() > 0) {
        connectionlessBootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(configuration.getReceiveBufferSizePredictor()));
    }
    if (configuration.getBacklog() > 0) {
        connectionlessBootstrap.setOption("backlog", configuration.getBacklog());
    }
    // set any additional netty options
    if (configuration.getOptions() != null) {
        for (Map.Entry<String, Object> entry : configuration.getOptions().entrySet()) {
            connectionlessBootstrap.setOption(entry.getKey(), entry.getValue());
        }
    }
    LOG.debug("Created ConnectionlessBootstrap {} with options: {}", connectionlessBootstrap, connectionlessBootstrap.getOptions());
    // set the pipeline factory, which creates the pipeline for each newly created channels
    connectionlessBootstrap.setPipelineFactory(pipelineFactory);
    InetSocketAddress hostAddress = new InetSocketAddress(configuration.getHost(), configuration.getPort());
    IpV4Subnet multicastSubnet = new IpV4Subnet(MULTICAST_SUBNET);
    if (multicastSubnet.contains(configuration.getHost())) {
        datagramChannel = (DatagramChannel) connectionlessBootstrap.bind(hostAddress);
        String networkInterface = configuration.getNetworkInterface() == null ? LOOPBACK_INTERFACE : configuration.getNetworkInterface();
        multicastNetworkInterface = NetworkInterface.getByName(networkInterface);
        ObjectHelper.notNull(multicastNetworkInterface, "No network interface found for '" + networkInterface + "'.");
        LOG.info("ConnectionlessBootstrap joining {}:{} using network interface: {}", new Object[] { configuration.getHost(), configuration.getPort(), multicastNetworkInterface.getName() });
        datagramChannel.joinGroup(hostAddress, multicastNetworkInterface).syncUninterruptibly();
        allChannels.add(datagramChannel);
    } else {
        LOG.info("ConnectionlessBootstrap binding to {}:{}", configuration.getHost(), configuration.getPort());
        channel = connectionlessBootstrap.bind(hostAddress);
        allChannels.add(channel);
    }
}
Also used : NioDatagramChannelFactory(org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory) InetSocketAddress(java.net.InetSocketAddress) NioDatagramWorkerPool(org.jboss.netty.channel.socket.nio.NioDatagramWorkerPool) FixedReceiveBufferSizePredictorFactory(org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory) Map(java.util.Map) IpV4Subnet(org.jboss.netty.handler.ipfilter.IpV4Subnet) ConnectionlessBootstrap(org.jboss.netty.bootstrap.ConnectionlessBootstrap)

Aggregations

NioDatagramChannelFactory (org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory)2 NioDatagramWorkerPool (org.jboss.netty.channel.socket.nio.NioDatagramWorkerPool)2 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 ConnectionlessBootstrap (org.jboss.netty.bootstrap.ConnectionlessBootstrap)1 FixedReceiveBufferSizePredictorFactory (org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory)1 IpV4Subnet (org.jboss.netty.handler.ipfilter.IpV4Subnet)1