Exemplo n.º 1
0
  @Override
  public void run(String... args) throws Exception {
    threadPoolDispatcher();
    multipleEventLoopDispatchers();
    multipleRingBufferDispatchers();

    env.shutdown();
  }
Exemplo n.º 2
0
  protected NettyTcpServer(
      Environment env,
      Reactor reactor,
      InetSocketAddress listenAddress,
      final ServerSocketOptions opts,
      final SslOptions sslOpts,
      Codec<Buffer, IN, OUT> codec,
      Collection<Consumer<TcpConnection<IN, OUT>>> connectionConsumers) {
    super(env, reactor, listenAddress, opts, sslOpts, codec, connectionConsumers);
    this.eventsReactor = reactor;
    this.listenAddress = listenAddress;
    Assert.notNull(opts, "ServerSocketOptions cannot be null");
    this.options = opts;

    int selectThreadCount =
        env.getProperty("reactor.tcp.selectThreadCount", Integer.class, Environment.PROCESSORS / 2);
    int ioThreadCount =
        env.getProperty("reactor.tcp.ioThreadCount", Integer.class, Environment.PROCESSORS);
    selectorGroup =
        new NioEventLoopGroup(
            selectThreadCount, new NamedDaemonThreadFactory("reactor-tcp-select"));
    ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-tcp-io"));

    this.bootstrap =
        new ServerBootstrap()
            .group(selectorGroup, ioGroup)
            .channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, options.backlog())
            .option(ChannelOption.SO_RCVBUF, options.rcvbuf())
            .option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_REUSEADDR, options.reuseAddr())
            .localAddress((null == listenAddress ? new InetSocketAddress(3000) : listenAddress))
            .childHandler(
                new ChannelInitializer<SocketChannel>() {
                  @Override
                  public void initChannel(final SocketChannel ch) throws Exception {
                    SocketChannelConfig config = ch.config();
                    config.setReceiveBufferSize(options.rcvbuf());
                    config.setSendBufferSize(options.sndbuf());
                    config.setKeepAlive(options.keepAlive());
                    config.setReuseAddress(options.reuseAddr());
                    config.setSoLinger(options.linger());
                    config.setTcpNoDelay(options.tcpNoDelay());

                    if (log.isDebugEnabled()) {
                      log.debug("CONNECT {}", ch);
                    }

                    if (null != sslOpts) {
                      SSLEngine ssl = new SSLEngineSupplier(sslOpts, false).get();
                      if (log.isDebugEnabled()) {
                        log.debug(
                            "SSL enabled using keystore {}",
                            (null != sslOpts.keystoreFile()
                                ? sslOpts.keystoreFile()
                                : "<DEFAULT>"));
                      }
                      ch.pipeline().addLast(new SslHandler(ssl));
                    }
                    if (options instanceof NettyServerSocketOptions
                        && null != ((NettyServerSocketOptions) options).pipelineConfigurer()) {
                      ((NettyServerSocketOptions) options)
                          .pipelineConfigurer()
                          .accept(ch.pipeline());
                    }
                    ch.pipeline().addLast(createChannelHandlers(ch));
                    ch.closeFuture()
                        .addListener(
                            new ChannelFutureListener() {
                              @Override
                              public void operationComplete(ChannelFuture future) throws Exception {
                                if (log.isDebugEnabled()) {
                                  log.debug("CLOSE {}", ch);
                                }
                                close(ch);
                              }
                            });
                  }
                });
  }