Example usage for io.netty.channel.epoll EpollEventLoopGroup EpollEventLoopGroup

List of usage examples for io.netty.channel.epoll EpollEventLoopGroup EpollEventLoopGroup

Introduction

In this page you can find the example usage for io.netty.channel.epoll EpollEventLoopGroup EpollEventLoopGroup.

Prototype

@SuppressWarnings("deprecation")
public EpollEventLoopGroup(ThreadFactory threadFactory) 

Source Link

Document

Create a new instance using the default number of threads and the given ThreadFactory .

Usage

From source file:com.ancun.netty.common.NettyBootstrapFactory.java

License:Apache License

private ServerBootstrap newEpollServerBootstrap(int ioThreadCount) {
    bossGroup = new EpollEventLoopGroup(1);

    if (ioThreadCount > 0) {
        workerGroup = new EpollEventLoopGroup(ioThreadCount);
    } else {/*from  w ww  .  j ava  2  s  .  c o  m*/
        workerGroup = new EpollEventLoopGroup();
    }

    return new ServerBootstrap().group(bossGroup, workerGroup).channel(EpollServerSocketChannel.class);
}

From source file:com.codahale.grpcproxy.util.Netty.java

License:Apache License

public static EventLoopGroup newWorkerEventLoopGroup() {
    if (Epoll.isAvailable()) {
        return new EpollEventLoopGroup(WORKER_THREADS);
    }// ww  w  . j  a v a 2 s .  c  om
    return new NioEventLoopGroup(WORKER_THREADS);
}

From source file:com.corundumstudio.socketio.SocketIOServer.java

License:Apache License

protected void initGroups() {
    if (configCopy.isUseLinuxNativeEpoll()) {
        bossGroup = new EpollEventLoopGroup(configCopy.getBossThreads());
        workerGroup = new EpollEventLoopGroup(configCopy.getWorkerThreads());
    } else {/*from  www. j  a va 2 s  . com*/
        bossGroup = new NioEventLoopGroup(configCopy.getBossThreads());
        workerGroup = new NioEventLoopGroup(configCopy.getWorkerThreads());
    }
}

From source file:com.lambdaworks.redis.cluster.SingleThreadedReactiveClusterClientTest.java

License:Apache License

@Before
public void before() {

    AtomicReference<EventLoopGroup> ref = new AtomicReference<>();
    DefaultClientResources clientResources = DefaultClientResources.builder()
            .eventExecutorGroup(ImmediateEventExecutor.INSTANCE)
            .eventLoopGroupProvider(new EventLoopGroupProvider() {
                @Override/*from w  w  w  .  jav  a 2s  . c o  m*/
                public <T extends EventLoopGroup> T allocate(Class<T> type) {

                    if (ref.get() == null) {

                        if (type == EpollEventLoopGroup.class) {
                            ref.set(new EpollEventLoopGroup(1));
                        } else {
                            ref.set(new NioEventLoopGroup(1));
                        }
                    }

                    return (T) ref.get();
                }

                @Override
                public int threadPoolSize() {
                    return 0;
                }

                @Override
                public Future<Boolean> release(EventExecutorGroup eventLoopGroup, long quietPeriod,
                        long timeout, TimeUnit unit) {
                    return new SucceededFuture<>(ImmediateEventExecutor.INSTANCE, true);
                }

                @Override
                public Future<Boolean> shutdown(long quietPeriod, long timeout, TimeUnit timeUnit) {
                    ref.get().shutdownGracefully(quietPeriod, timeout, timeUnit);
                    return new SucceededFuture<>(ImmediateEventExecutor.INSTANCE, true);
                }

            }).commandLatencyCollectorOptions(DefaultCommandLatencyCollectorOptions.disabled()).build();

    client = RedisClusterClient.create(clientResources, RedisURI.create("localhost", 7379));
}

From source file:com.smoketurner.packet.application.config.NettyConfiguration.java

License:Apache License

@JsonIgnore
public ChannelFuture build(@Nonnull final Environment environment, @Nonnull final Uploader uploader,
        @Nonnull final Size maxUploadSize) throws SSLException, CertificateException {

    // Configure SSL
    final SslContext sslCtx;
    if (ssl) {/*from   w  ww  .j a  va2 s.  c  o m*/
        if (selfSignedCert) {
            final SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        } else {
            sslCtx = SslContextBuilder.forServer(new File(keyCertChainFile), new File(keyFile)).build();
        }
    } else {
        sslCtx = null;
    }

    final UploadInitializer initializer = new UploadInitializer(sslCtx, uploader, maxLength.toBytes(),
            maxUploadSize.toBytes());

    final EventLoopGroup bossGroup;
    final EventLoopGroup workerGroup;
    if (Epoll.isAvailable()) {
        LOGGER.info("Using epoll event loop");
        bossGroup = new EpollEventLoopGroup(1);
        workerGroup = new EpollEventLoopGroup();
    } else {
        LOGGER.info("Using NIO event loop");
        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();
    }

    environment.lifecycle().manage(new EventLoopGroupManager(bossGroup));
    environment.lifecycle().manage(new EventLoopGroupManager(workerGroup));

    final ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).handler(new LoggingHandler(LogLevel.INFO))
            .option(ChannelOption.SO_BACKLOG, 100).childOption(ChannelOption.SO_KEEPALIVE, true)
            .childHandler(initializer);

    if (Epoll.isAvailable()) {
        bootstrap.channel(EpollServerSocketChannel.class);
    } else {
        bootstrap.channel(NioServerSocketChannel.class);
    }

    // Start the server
    final ChannelFuture future = bootstrap.bind(listenPort);
    environment.lifecycle().manage(new ChannelFutureManager(future));
    return future;
}

From source file:com.streamsets.pipeline.lib.udp.UDPConsumingServer.java

License:Apache License

@Override
protected Bootstrap bootstrap(boolean enableEpoll) {
    if (enableEpoll) {
        // Direct buffers required for Epoll
        enableDirectBuffers();//  w w w  .ja  v  a2  s  . c  om
        EventLoopGroup group = new EpollEventLoopGroup(numThreads);
        groups.add(group);
        return new Bootstrap().group(group).channel(EpollDatagramChannel.class).handler(handler)
                .option(EpollChannelOption.SO_REUSEADDR, true).option(EpollChannelOption.SO_REUSEPORT, true)
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    } else {
        disableDirectBuffers();
        EventLoopGroup group = new NioEventLoopGroup(numThreads);
        groups.add(group);
        return new Bootstrap().group(group).channel(NioDatagramChannel.class).handler(handler)
                .option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.ALLOCATOR, new PooledByteBufAllocator()); // use on-heap buffers
    }
}

From source file:com.streamsets.pipeline.stage.origin.tcp.TCPConsumingServer.java

License:Apache License

@Override
protected AbstractBootstrap bootstrap(boolean enableEpoll) {
    if (enableEpoll) {
        enableDirectBuffers();//from w  w  w. j a va2 s.c o m
        // boss group simply opens channels and hands processing off to the child
        EpollEventLoopGroup bossGroup = new EpollEventLoopGroup(NUM_BOSS_THREADS);
        EventLoopGroup workerGroup = new EpollEventLoopGroup(numThreads);
        groups.add(bossGroup);
        groups.add(workerGroup);
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(EpollServerSocketChannel.class)
                .childHandler(this.channelInitializer)
                .option(ChannelOption.SO_BACKLOG, SOCKET_MAX_INBOUND_CONNECTION_QUEUE_DEPTH)
                .childOption(ChannelOption.SO_KEEPALIVE, SOCKET_KEEPALIVE);
        return b;
    } else {
        disableDirectBuffers();
        EventLoopGroup bossGroup = new NioEventLoopGroup(NUM_BOSS_THREADS);
        EventLoopGroup workerGroup = new NioEventLoopGroup(numThreads);
        groups.add(bossGroup);
        groups.add(workerGroup);
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(this.channelInitializer)
                .option(ChannelOption.SO_BACKLOG, SOCKET_MAX_INBOUND_CONNECTION_QUEUE_DEPTH)
                .childOption(ChannelOption.SO_KEEPALIVE, SOCKET_KEEPALIVE);
        return b;
    }

}

From source file:de.dentrassi.varlink.internal.VarlinkImpl.java

License:Open Source License

public VarlinkImpl() {
    this.group = new EpollEventLoopGroup(1);
    this.resolver = newService(org.varlink.Resolver.class, "unix:/run/org.varlink.resolver");
}

From source file:de.jackwhite20.japs.shared.pipeline.PipelineUtils.java

License:Open Source License

public static EventLoopGroup newEventLoopGroup(int threads) {

    return epoll ? new EpollEventLoopGroup(threads) : new NioEventLoopGroup(threads);
}

From source file:herddb.network.netty.NettyChannelAcceptor.java

License:Apache License

public void start() throws Exception {
    if (ssl) {//from   www. j a va 2  s .c o m
        if (sslCertFile == null) {
            LOGGER.log(Level.SEVERE, "start SSL with self-signed auto-generated certificate");
            if (sslCiphers != null) {
                LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers);
            }
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            try {
                sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).ciphers(sslCiphers)
                        .build();
            } finally {
                ssc.delete();
            }
        } else {
            LOGGER.log(Level.SEVERE, "start SSL with certificate " + sslCertFile.getAbsolutePath()
                    + " chain file " + sslCertChainFile.getAbsolutePath());
            if (sslCiphers != null) {
                LOGGER.log(Level.SEVERE, "required sslCiphers " + sslCiphers);
            }
            sslCtx = SslContextBuilder.forServer(sslCertChainFile, sslCertFile, sslCertPassword)
                    .ciphers(sslCiphers).build();
        }

    }

    if (callbackThreads == 0) {
        callbackExecutorQueue = new SynchronousQueue<Runnable>();
        callbackExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
                callbackExecutorQueue, threadFactory);
    } else {
        callbackExecutorQueue = new LinkedBlockingQueue<Runnable>();
        callbackExecutor = new ThreadPoolExecutor(callbackThreads, callbackThreads, 0L, TimeUnit.MILLISECONDS,
                callbackExecutorQueue, threadFactory);
    }
    statsLogger.registerGauge("callbacksqueue", new Gauge<Integer>() {
        @Override
        public Integer getDefaultValue() {
            return 0;
        }

        @Override
        public Integer getSample() {
            return callbackExecutorQueue.size();
        }

    });
    InetSocketAddress address = new InetSocketAddress(host, port);
    LOGGER.log(Level.SEVERE, "Starting HerdDB network server at {0}:{1}", new Object[] { host, port + "" });
    if (address.isUnresolved()) {
        throw new IOException("Bind address " + host + ":" + port + " cannot be resolved");
    }
    ChannelInitializer<io.netty.channel.Channel> channelInitialized = new ChannelInitializer<io.netty.channel.Channel>() {
        @Override
        public void initChannel(io.netty.channel.Channel ch) throws Exception {
            NettyChannel session = new NettyChannel("unnamed", ch, callbackExecutor);
            if (acceptor != null) {
                acceptor.createConnection(session);
            }

            //                        ch.pipeline().addLast(new LoggingHandler());
            // Add SSL handler first to encrypt and decrypt everything.
            if (ssl) {
                ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()));
            }

            ch.pipeline().addLast("lengthprepender", new LengthFieldPrepender(4));
            ch.pipeline().addLast("lengthbaseddecoder",
                    new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
            //                
            ch.pipeline().addLast("messagedecoder", new ProtocolMessageDecoder());
            ch.pipeline().addLast(new ServerInboundMessageHandler(session));
        }
    };
    if (enableRealNetwork) {
        if (NetworkUtils.isEnableEpoolNative()) {
            bossGroup = new EpollEventLoopGroup(workerThreads);
            workerGroup = new EpollEventLoopGroup(workerThreads);
            LOGGER.log(Level.FINE, "Using netty-native-epoll network type");
        } else {
            bossGroup = new NioEventLoopGroup(workerThreads);
            workerGroup = new NioEventLoopGroup(workerThreads);
            LOGGER.log(Level.FINE, "Using nio network type");
        }

        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)
                .channel(NetworkUtils.isEnableEpoolNative() ? EpollServerSocketChannel.class
                        : NioServerSocketChannel.class)
                .childHandler(channelInitialized).option(ChannelOption.SO_BACKLOG, 128);
        ChannelFuture f = b.bind(address).sync();
        this.channel = f.channel();

    }

    if (enableJVMNetwork) {
        localBossGroup = new DefaultEventLoopGroup(workerThreads);
        localWorkerGroup = new DefaultEventLoopGroup(workerThreads);
        ServerBootstrap b_local = new ServerBootstrap();
        b_local.group(localBossGroup, localWorkerGroup).channel(LocalServerChannel.class)
                .childHandler(channelInitialized);

        String hostAddress = NetworkUtils.getAddress(address);
        LocalServerRegistry.registerLocalServer(hostAddress, port, ssl);

        ChannelFuture local_f = b_local.bind(new LocalAddress(hostAddress + ":" + port + ":" + ssl)).sync();
        this.local_channel = local_f.channel();
    }

}