Example usage for io.netty.util.concurrent DefaultEventExecutorGroup DefaultEventExecutorGroup

List of usage examples for io.netty.util.concurrent DefaultEventExecutorGroup DefaultEventExecutorGroup

Introduction

In this page you can find the example usage for io.netty.util.concurrent DefaultEventExecutorGroup DefaultEventExecutorGroup.

Prototype

public DefaultEventExecutorGroup(int nThreads) 

Source Link

Usage

From source file:code.google.nfs.rpc.netty4.server.Netty4Server.java

License:Apache License

public void start(int listenPort, final ExecutorService ignore) throws Exception {
    if (!startFlag.compareAndSet(false, true)) {
        return;//from w  w  w.  j av  a  2  s  .com
    }
    bossGroup = new NioEventLoopGroup();
    ioGroup = new NioEventLoopGroup();
    businessGroup = new DefaultEventExecutorGroup(businessThreads);

    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, ioGroup).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.TCP_NODELAY,
                    Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true")))
            .childOption(ChannelOption.SO_REUSEADDR,
                    Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true")))
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast("decoder", new Netty4ProtocolDecoder());
                    ch.pipeline().addLast("encoder", new Netty4ProtocolEncoder());
                    ch.pipeline().addLast(businessGroup, "handler", new Netty4ServerHandler());
                }
            });

    b.bind(listenPort).sync();
    LOGGER.warn("Server started,listen at: " + listenPort + ", businessThreads is " + businessThreads);
}

From source file:com.ancun.netty.httpserver.HttpServer.java

License:Apache License

private EventExecutorGroup initializeExecutorGroup() {
    if (getExecutorThreadCount() > 0) {
        return new DefaultEventExecutorGroup(getExecutorThreadCount());
    }//from www  .j  a  va 2  s.  c  om

    return null;
}

From source file:com.linkedin.r2.transport.http.client.AbstractNettyStreamClient.java

License:Apache License

AbstractNettyStreamClient(ChannelPoolFactory factory, ScheduledExecutorService executor, int requestTimeout,
        int shutdownTimeout, long maxResponseSize) {
    _maxResponseSize = maxResponseSize;//  w  w  w. ja v a  2  s  .c o m
    _scheduler = executor;
    _callbackExecutors = new DefaultEventExecutorGroup(1);
    _requestTimeout = requestTimeout;
    _shutdownTimeout = shutdownTimeout;
    _requestTimeoutMessage = "Exceeded request timeout of " + _requestTimeout + "ms";
    _jmxManager = AbstractJmxManager.NULL_JMX_MANAGER;
    _maxConcurrentConnections = Integer.MAX_VALUE;
    _allChannels = new DefaultChannelGroup("R2 client channels", GlobalEventExecutor.INSTANCE);
}

From source file:com.linkedin.r2.transport.http.client.HttpNettyClient.java

License:Apache License

HttpNettyClient(ChannelPoolFactory factory, ScheduledExecutorService executor, int requestTimeout,
        int shutdownTimeout, int maxResponseSize) {
    _maxResponseSize = maxResponseSize;/*from w  w w  . java2s  .  c  o m*/
    _channelPoolManager = new ChannelPoolManager(factory);
    _scheduler = executor;
    _callbackExecutors = new DefaultEventExecutorGroup(1);
    _requestTimeout = requestTimeout;
    _shutdownTimeout = shutdownTimeout;
    _requestTimeoutMessage = "Exceeded request timeout of " + _requestTimeout + "ms";
    _jmxManager = AbstractJmxManager.NULL_JMX_MANAGER;
    _jmxManager.onProviderCreate(_channelPoolManager);
    _maxHeaderSize = 8192;
    _maxChunkSize = 8192;
    _maxConcurrentConnections = Integer.MAX_VALUE;
    _allChannels = new DefaultChannelGroup("R2 client channels", GlobalEventExecutor.INSTANCE);
}

From source file:com.linkedin.r2.transport.http.server.HttpNettyServer.java

License:Apache License

@Override
public void start() {
    _eventExecutors = new DefaultEventExecutorGroup(_threadPoolSize);
    _bossGroup = new NioEventLoopGroup(1, new NamedThreadFactory("R2 Nio Boss"));
    _workerGroup = new NioEventLoopGroup(0, new NamedThreadFactory("R2 Nio Worker"));

    ServerBootstrap bootstrap = new ServerBootstrap().group(_bossGroup, _workerGroup)
            .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<NioSocketChannel>() {
                @Override//from  w  w  w  .j  av a2s.c o m
                protected void initChannel(NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast("decoder", new HttpRequestDecoder());
                    ch.pipeline().addLast("aggregator", new HttpObjectAggregator(1048576));
                    ch.pipeline().addLast("encoder", new HttpResponseEncoder());
                    ch.pipeline().addLast("rapi", new RAPServerCodec());
                    ch.pipeline().addLast(_eventExecutors, "handler",
                            _restOverStream ? new StreamHandler() : new RestHandler());
                }
            });

    bootstrap.bind(new InetSocketAddress(_port));
}

From source file:com.ltln.modules.ni.omc.system.simulator.AlmClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    Constants.init();//from ww  w.  ja  v a 2s  . c  o  m
    final SslContext sslCtx;
    if (SSL) {
        sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    final EventExecutorGroup handlerGroup = new DefaultEventExecutorGroup(1);
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                        }
                        p.addLast(new AlarmMsgDecoder(8192, 7, 2, 0, 0, false));
                        p.addLast(new AlarmMsgEncoder());
                        p.addLast(handlerGroup, new AlarmClientHandler());
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

From source file:com.opentable.logging.RedisServerRule.java

License:Apache License

@Override
protected void before() throws Throwable {
    try (ServerSocket ss = new ServerSocket(0)) {
        port = ss.getLocalPort();//from w ww  . j a  v  a  2  s .  c  o  m
    }

    // Only execute the command handler in a single thread
    final RedisCommandHandler commandHandler = new RedisCommandHandler(new SimpleRedisServer());

    b = new ServerBootstrap();
    group = new DefaultEventExecutorGroup(1);
    b.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, 100).localAddress(port)
            .childOption(ChannelOption.TCP_NODELAY, true).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    //                 p.addLast(new ByteLoggingHandler(LogLevel.INFO));
                    p.addLast(new RedisCommandDecoder());
                    p.addLast(new RedisReplyEncoder());
                    p.addLast(group, commandHandler);
                }
            });

    // Start the server.
    b.bind().sync();
}

From source file:com.xyz.rpc.netty4.server.Netty4Server.java

License:Apache License

public void start(int listenPort, final ExecutorService ignore) throws Exception {
    if (!startFlag.compareAndSet(false, true)) {
        return;/*from  w  ww. jav  a 2 s . co  m*/
    }
    bossGroup = new NioEventLoopGroup();
    ioGroup = new NioEventLoopGroup();
    businessGroup = new DefaultEventExecutorGroup(businessThreads);

    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, ioGroup).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.TCP_NODELAY,
                    Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.nodelay", "true")))
            .childOption(ChannelOption.SO_REUSEADDR,
                    Boolean.parseBoolean(System.getProperty("nfs.rpc.tcp.reuseaddress", "true")))
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast("decoder", new Netty4ProtocolDecoder());
                    ch.pipeline().addLast("encoder", new Netty4ProtocolEncoder());
                    ch.pipeline().addLast(businessGroup, "handler", new Netty4ServerHandler());
                }
            });
    b.bind(new InetSocketAddress("127.0.0.1", listenPort)).sync();
    LOGGER.warn("Server started,listen at: " + listenPort + ", businessThreads is " + businessThreads);
}

From source file:fr.kissy.zergling_push.MainServer.java

License:Apache License

private void run() throws Exception {
    final ChannelGroup allPlayers = new DefaultChannelGroup("AllPlayers", GlobalEventExecutor.INSTANCE);
    final ArrayBlockingQueue<PlayerMessage> messagesQueue = new ArrayBlockingQueue<PlayerMessage>(1024);

    final World world = new World();
    final MainLoop mainLoop = new MainLoop(world, allPlayers, messagesQueue);

    ScheduledThreadPoolExecutor mainLoopExecutor = new ScheduledThreadPoolExecutor(1);
    mainLoopExecutor.scheduleAtFixedRate(mainLoop, 0, TICK_RATE, TimeUnit.MILLISECONDS);

    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    EventExecutorGroup executorGroup = new DefaultEventExecutorGroup(16);

    try {/*from   w w  w . j a va2 s. co m*/
        final ServerBootstrap sb = new ServerBootstrap();
        sb.childOption(ChannelOption.TCP_NODELAY, true);
        sb.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(final SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast("http-decoder", new HttpServerCodec());
                        pipeline.addLast("http-aggregator", new HttpObjectAggregator(65536));
                        pipeline.addLast("websocket-compression", new WebSocketServerCompressionHandler());
                        pipeline.addLast("websocket-protocol",
                                new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
                        pipeline.addLast(executorGroup, "flatbuffer-message",
                                new FlatBufferMessageHandler(world, messagesQueue));
                        pipeline.addLast("http-server", new HttpStaticFileServerHandler());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        final Channel ch = sb.bind(8080).sync().channel();
        ch.closeFuture().sync();
    } catch (Exception e) {
        throw new RuntimeException("Error while running server", e);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:github.com.cp149.netty.server.NettyappenderServer.java

License:Apache License

public void run() throws Exception {

    try {//from   w ww  . j a  v a 2 s. co m
        bootstrap = new ServerBootstrap();
        final EventExecutorGroup executor = new DefaultEventExecutorGroup(4);

        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childOption(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.TCP_NODELAY, true)
                .childOption(ChannelOption.SO_RCVBUF, 65535).childOption(ChannelOption.SO_SNDBUF, 2048)
                .childOption(ChannelOption.SO_REUSEADDR, true) //reuse address
                .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false))// heap buf 's better               
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        //                     ch.pipeline().addLast( new MarshallingEncoder(MarshallUtil.createProvider()));
                        //                     ch.pipeline().addLast(new CompatibleObjectDecoder());
                        //                     ch.pipeline().addLast(new ObjectEncoder(),
                        //                                  new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                        ch.pipeline().addLast(new MarshallingDecoder(MarshallUtil.createUnProvider()));

                        ch.pipeline().addLast(executor, new NettyappenderServerHandler());
                        //
                    }
                });

        bootstrap.bind(port).sync();

    } finally {

    }
    // bootstrap = new ServerBootstrap(new
    // NioServerSocketChannelFactory(Executors.newFixedThreadPool(4),
    // Executors.newFixedThreadPool(4)));
    // final ExecutionHandler executionHandler = new ExecutionHandler(new
    // OrderedMemoryAwareThreadPoolExecutor(4, 1024 * 1024 * 300, 1024 *
    // 1024 * 300 * 2));
    // bootstrap.setOption("tcpNoDelay", true);
    // bootstrap.setOption("keepAlive", true);
    // // bootstrap.setOption("writeBufferHighWaterMark", 100 * 64 * 1024);
    // // bootstrap.setOption("sendBufferSize", 1048576);
    // bootstrap.setOption("receiveBufferSize", 1048576*10 );
    //
    // // Set up the pipeline factory.
    // bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    // public ChannelPipeline getPipeline() throws Exception {
    // return Channels.pipeline(executionHandler, new
    // MarshallingDecoder(createProvider(createMarshallerFactory(),
    // createMarshallingConfig())),
    // new NettyappenderServerHandler());
    // }
    // });

    // // Bind and start to accept incoming connections.
    // bootstrap.bind(new InetSocketAddress(port));
    //       LoggerFactory.getLogger(this.getClass()).info("start server at" +
    //             port);
}