Example usage for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup

List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup

Introduction

In this page you can find the example usage for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup.

Prototype

public NioEventLoopGroup(int nThreads, Executor executor) 

Source Link

Usage

From source file:com.heliosapm.streams.forwarder.HttpJsonMetricForwarder.java

License:Apache License

/**
 * Starts the forwarder/*from www  . ja v a2 s  . c  o  m*/
 * @throws Exception thrown on any error
 */
public void start() throws Exception {
    log.info(">>>>> Starting HttpJsonMetricForwarder [{}]....", beanName);
    threadPool = new JMXManagedThreadPool(EXECUTOR_OBJECT_NAME, beanName, workerThreads, workerThreads * 2, 1,
            60000, 100, 99, true);
    eventLoopGroup = new NioEventLoopGroup(workerThreads, (Executor) threadPool);
    outboundHandler = new HttpJsonOutboundHandler(1500, endpointHost, endpointUri);
    bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).handler(this)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // FIXME: config
            .option(ChannelOption.SO_SNDBUF, 64000) // FIXME: config
            .option(ChannelOption.SO_KEEPALIVE, true);
    senderChannel = bootstrap.connect(endpointHost, endpointPort).sync().channel(); // FIXME: short timeout, reconnect loop
    consumerProperties.put("bootstrap.servers", kafkaBootstrapServers);
    consumerProperties.put("group.id", kafkaGroupId);
    consumerProperties.put("enable.auto.commit", "" + kafkaAutoCommit);
    consumerProperties.put("auto.commit.interval.ms", "" + kafkaAutoCommitInterval);
    consumerProperties.put("session.timeout.ms", "" + 30000);
    consumerProperties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
    consumerProperties.put("value.deserializer", HeliosSerdes.STREAMED_METRIC_VALUE_DESER.getClass().getName());
    subscriberThread = new Thread(this, "KafkaSubscriberThread-" + beanName);
    subscriberThread.setDaemon(true);
    subThreadActive.set(true);
    log.info("[{}] Subscriber Thread Starting...", beanName);
    subscriberThread.start();
    log.info("<<<<< HttpJsonMetricForwarder [{}] Started.", beanName);
}

From source file:com.heliosapm.streams.metrichub.HubManager.java

License:Apache License

private HubManager(final Properties properties) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                close();//www  . j  a  v  a2 s  . com
            } catch (Exception x) {
                /* No Op */}
        }
    });
    log.info(">>>>> Initializing HubManager...");
    metricMetaService = new MetricsMetaAPIImpl(properties);
    tsdbEndpoint = TSDBEndpoint.getEndpoint(metricMetaService.getSqlWorker());
    for (String url : tsdbEndpoint.getUpServers()) {
        final URL tsdbUrl = URLHelper.toURL(url);
        tsdbAddresses.add(new InetSocketAddress(tsdbUrl.getHost(), tsdbUrl.getPort()));
    }
    endpointCount = tsdbAddresses.size();
    endpointSequence = new AtomicInteger(endpointCount);
    group = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2,
            metricMetaService.getForkJoinPool());
    bootstrap = new Bootstrap();
    bootstrap.handler(channelInitializer).group(group).channel(NioSocketChannel.class)
            .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator())
            .option(ChannelOption.ALLOCATOR, BufferManager.getInstance());
    final ChannelPoolHandler poolHandler = this;
    poolMap = new AbstractChannelPoolMap<InetSocketAddress, SimpleChannelPool>() {
        @Override
        protected SimpleChannelPool newPool(final InetSocketAddress key) {
            final Bootstrap b = new Bootstrap().handler(channelInitializer).group(group).remoteAddress(key)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator())
                    .option(ChannelOption.ALLOCATOR, BufferManager.getInstance());
            return new SimpleChannelPool(b, poolHandler);
        }
    };
    eventExecutor = new DefaultEventExecutor(metricMetaService.getForkJoinPool());
    channelGroup = new DefaultChannelGroup("MetricHubChannelGroup", eventExecutor);

    //      tsdbAddresses.parallelStream().forEach(addr -> {
    //         final Set<Channel> channels = Collections.synchronizedSet(new HashSet<Channel>(3));
    //         IntStream.of(1,2,3).parallel().forEach(i -> {
    //            final ChannelPool pool = poolMap.get(addr); 
    //            try {channels.add(pool.acquire().awaitUninterruptibly().get());
    //            } catch (Exception e) {}
    //            log.info("Acquired [{}] Channels", channels.size());
    //            channels.parallelStream().forEach(ch -> pool.release(ch));
    //         });
    //      });

    log.info("<<<<< HubManager Initialized.");
}

From source file:com.heliosapm.streams.onramp.OnRampBoot.java

License:Apache License

/**
 * Creates a new OnRampBoot/* w w w  .  j av  a  2  s  .c o  m*/
 * @param appConfig  The application configuration
 */
public OnRampBoot(final Properties appConfig) {
    final String jmxmpUri = ConfigurationHelper.getSystemThenEnvProperty("jmx.jmxmp.uri",
            "jmxmp://0.0.0.0:1893", appConfig);
    JMXHelper.fireUpJMXMPServer(jmxmpUri);
    MessageForwarder.initialize(appConfig);
    port = ConfigurationHelper.getIntSystemThenEnvProperty("onramp.network.port", 8091, appConfig);
    bindInterface = ConfigurationHelper.getSystemThenEnvProperty("onramp.network.bind", "0.0.0.0", appConfig);
    bindSocket = new InetSocketAddress(bindInterface, port);
    workerThreads = ConfigurationHelper.getIntSystemThenEnvProperty("onramp.network.worker_threads", CORES * 2,
            appConfig);
    connectTimeout = ConfigurationHelper.getIntSystemThenEnvProperty("onramp.network.sotimeout", 0, appConfig);
    backlog = ConfigurationHelper.getIntSystemThenEnvProperty("onramp.network.backlog", 3072, appConfig);
    writeSpins = ConfigurationHelper.getIntSystemThenEnvProperty("onramp.network.writespins", 16, appConfig);
    recvBuffer = ConfigurationHelper.getIntSystemThenEnvProperty("onramp.network.recbuffer", 43690, appConfig);
    sendBuffer = ConfigurationHelper.getIntSystemThenEnvProperty("onramp.network.sendbuffer", 8192, appConfig);
    disableEpoll = ConfigurationHelper.getBooleanSystemThenEnvProperty("onramp.network.epoll.disable", false,
            appConfig);
    async = ConfigurationHelper.getBooleanSystemThenEnvProperty("onramp.network.async_io", true, appConfig);
    tcpNoDelay = ConfigurationHelper.getBooleanSystemThenEnvProperty("onramp.network.tcp_no_delay", true,
            appConfig);
    keepAlive = ConfigurationHelper.getBooleanSystemThenEnvProperty("onramp.network.keep_alive", true,
            appConfig);
    reuseAddress = ConfigurationHelper.getBooleanSystemThenEnvProperty("onramp.network.reuse_address", true,
            appConfig);
    tcpPipelineFactory = new PipelineFactory(appConfig);
    udpPipelineFactory = new UDPPipelineFactory();
    tcpServerBootstrap.handler(new LoggingHandler(getClass(), LogLevel.INFO));
    tcpServerBootstrap.childHandler(tcpPipelineFactory);
    // Set the child options
    tcpServerBootstrap.childOption(ChannelOption.ALLOCATOR, BufferManager.getInstance().getAllocator());
    tcpServerBootstrap.childOption(ChannelOption.TCP_NODELAY, tcpNoDelay);
    tcpServerBootstrap.childOption(ChannelOption.SO_KEEPALIVE, keepAlive);
    tcpServerBootstrap.childOption(ChannelOption.SO_RCVBUF, recvBuffer);
    tcpServerBootstrap.childOption(ChannelOption.SO_SNDBUF, sendBuffer);
    tcpServerBootstrap.childOption(ChannelOption.WRITE_SPIN_COUNT, writeSpins);
    // Set the server options
    tcpServerBootstrap.option(ChannelOption.SO_BACKLOG, backlog);
    tcpServerBootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
    tcpServerBootstrap.option(ChannelOption.SO_RCVBUF, recvBuffer);
    tcpServerBootstrap.option(ChannelOption.SO_TIMEOUT, connectTimeout);

    final StringBuilder tcpUri = new StringBuilder("tcp");
    final StringBuilder udpUri = new StringBuilder("udp");
    if (IS_LINUX && !disableEpoll) {
        bossExecutorThreadFactory = new ExecutorThreadFactory("EpollServerBoss", true);
        bossGroup = new EpollEventLoopGroup(1, (ThreadFactory) bossExecutorThreadFactory);
        workerExecutorThreadFactory = new ExecutorThreadFactory("EpollServerWorker", true);
        workerGroup = new EpollEventLoopGroup(workerThreads, (ThreadFactory) workerExecutorThreadFactory);
        tcpChannelType = EpollServerSocketChannel.class;
        udpChannelType = EpollDatagramChannel.class;
        tcpUri.append("epoll");
        udpUri.append("epoll");
    } else {
        bossExecutorThreadFactory = new ExecutorThreadFactory("NioServerBoss", true);
        bossGroup = new NioEventLoopGroup(1, bossExecutorThreadFactory);
        workerExecutorThreadFactory = new ExecutorThreadFactory("NioServerWorker", true);
        workerGroup = new NioEventLoopGroup(workerThreads, workerExecutorThreadFactory);
        tcpChannelType = NioServerSocketChannel.class;
        udpChannelType = NioDatagramChannel.class;
        tcpUri.append("nio");
        udpUri.append("nio");
    }

    tcpUri.append("://").append(bindInterface).append(":").append(port);
    udpUri.append("://").append(bindInterface).append(":").append(port);
    URI u = null;
    try {
        u = new URI(tcpUri.toString());
    } catch (URISyntaxException e) {
        log.warn("Failed TCP server URI const: [{}]. Programmer Error", tcpUri, e);
    }
    tcpServerURI = u;
    try {
        u = new URI(udpUri.toString());
    } catch (URISyntaxException e) {
        log.warn("Failed UDP server URI const: [{}]. Programmer Error", udpUri, e);
    }
    udpServerURI = u;

    log.info(">>>>> Starting OnRamp TCP Listener on [{}]...", tcpServerURI);
    log.info(">>>>> Starting OnRamp UDP Listener on [{}]...", udpServerURI);
    final ChannelFuture cf = tcpServerBootstrap.channel(tcpChannelType).group(bossGroup, workerGroup)
            .bind(bindSocket).awaitUninterruptibly()
            .addListener(new GenericFutureListener<Future<? super Void>>() {
                public void operationComplete(final Future<? super Void> f) throws Exception {
                    log.info("<<<<< OnRamp TCP Listener on [{}] Started", tcpServerURI);
                };
            }).awaitUninterruptibly();
    final ChannelFuture ucf = udpBootstrap.channel(udpChannelType).group(workerGroup)
            .option(ChannelOption.SO_BROADCAST, true).handler(new UDPPipelineFactory()).bind(bindSocket)
            .awaitUninterruptibly().addListener(new GenericFutureListener<Future<? super Void>>() {
                public void operationComplete(final Future<? super Void> f) throws Exception {
                    log.info("<<<<< OnRamp UDP Listener on [{}] Started", udpServerURI);
                };
            }).awaitUninterruptibly();

    tcpServerChannel = cf.channel();
    udpServerChannel = ucf.channel();
    tcpCloseFuture = tcpServerChannel.closeFuture();
    udpCloseFuture = udpServerChannel.closeFuture();
    Runtime.getRuntime().addShutdownHook(shutdownHook);

}

From source file:com.heliosapm.streams.tracing.writers.NetWriter.java

License:Apache License

/**
 * {@inheritDoc}/*from  w  w w.java  2 s . com*/
 * @see com.heliosapm.streams.tracing.AbstractMetricWriter#configure(java.util.Properties)
 */
@Override
public void configure(final Properties config) {
    super.configure(config);
    remotes = ConfigurationHelper.getArraySystemThenEnvProperty(CONFIG_REMOTE_URIS, DEFAULT_REMOTE_URIS,
            config);
    Collections.addAll(remoteUris, remotes);
    channelGroupThreads = ConfigurationHelper.getIntSystemThenEnvProperty(CONFIG_EXEC_THREADS,
            DEFAULT_EXEC_THREADS, config);
    this.config.put("channelGroupThreads", channelGroupThreads);
    eventLoopThreads = ConfigurationHelper.getIntSystemThenEnvProperty(CONFIG_ELOOP_THREADS,
            DEFAULT_ELOOP_THREADS, config);
    this.config.put("eventLoopThreads", eventLoopThreads);
    eventExecutor = new UnorderedThreadPoolEventExecutor(channelGroupThreads, groupThreadFactory, this);
    channels = new DefaultChannelGroup(getClass().getSimpleName() + "Channels", eventExecutor);
    group = new NioEventLoopGroup(eventLoopThreads, eventLoopThreadFactory);
    bootstrap.group(group).channel(channelType).handler(getChannelInitializer());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000); // FIXME: config
    bootstrap.option(ChannelOption.ALLOCATOR, BufferManager.getInstance());
    this.config.put("connectTimeout", 5000);

    // FIXME: Tweaks for channel configuration

}