Example usage for io.netty.bootstrap ServerBootstrap group

List of usage examples for io.netty.bootstrap ServerBootstrap group

Introduction

In this page you can find the example usage for io.netty.bootstrap ServerBootstrap group.

Prototype

public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) 

Source Link

Document

Set the EventLoopGroup for the parent (acceptor) and the child (client).

Usage

From source file:com.xxx.netty.run.SecureChatServer.java

License:Apache License

@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:root-context.xml");// loading
    //jedis = context.getBean(RedisInitBean.class).getSingletonInstance();
    SecureChatServer chatServer = context.getBean(SecureChatServer.class);
    // SelfSignedCertificate????
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    // ???/*  w  ww .  j a v  a 2  s .co m*/
    SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap serverBootstrap = new ServerBootstrap();// ?????
        serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new SecureChatServerInitializer(sslCtx));
        if (null != args && args.length > 1 && args[0].matches("\\d")) {
            chatServer.PORT = Integer.parseInt(args[0]);
        }
        LOGGER.debug("SSL TCP server started on port:{}", chatServer.PORT);
        serverBootstrap.bind(chatServer.PORT).sync().channel().closeFuture().sync();

    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        context = null;
    }
}

From source file:com.xxx.util.io.server.netty.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from   w w  w.ja v  a2  s.  co m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoServerHandler());
                    }
                });

        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.xx_dev.apn.proxy.ApnProxyServer.java

License:Apache License

public void start() {
    int bossThreadCount = ApnProxyConfig.getConfig().getBossThreadCount();
    int workerThreadCount = ApnProxyConfig.getConfig().getWorkerThreadCount();
    int port = ApnProxyConfig.getConfig().getPort();

    LoggerUtil.info(logger, "ApnProxy Server Listen on: " + port);

    ServerBootstrap serverBootStrap = new ServerBootstrap();
    serverBootStrap.childOption(ChannelOption.ALLOCATOR, UnpooledByteBufAllocator.DEFAULT);

    bossGroup = new NioEventLoopGroup(bossThreadCount);
    workerGroup = new NioEventLoopGroup(workerThreadCount);

    try {//from   w  ww  .j av a 2s.c  o  m
        serverBootStrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port)
                .childHandler(new ApnProxyServerChannelInitializer());
        serverBootStrap.bind().sync().channel().closeFuture().sync();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        logger.error("showdown the server");
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.xx_dev.apn.proxy.expriment.HttpServer.java

License:Apache License

public static void main(String[] args) {
    ServerBootstrap serverBootStrap = new ServerBootstrap();
    serverBootStrap.childOption(ChannelOption.ALLOCATOR, UnpooledByteBufAllocator.DEFAULT);

    try {/*from w ww  .j  a  va 2s  . c  o m*/
        serverBootStrap.group(new NioEventLoopGroup(1), new NioEventLoopGroup(2))
                .channel(NioServerSocketChannel.class).localAddress(5000)
                .childHandler(new HttpServerChannelInitializer());
        serverBootStrap.bind().sync().channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
    }
}

From source file:com.xx_dev.apn.socks.local.PortForwardProxy.java

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {/*  ww  w .  j  a va2 s .c om*/
        ServerBootstrap b = new ServerBootstrap();
        ChannelFuture bindFuture = b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new PortForwardProxyFrontendInitializer(LocalConfig.ins().getRemoteHost(),
                        LocalConfig.ins().getRemotePort()))
                .childOption(ChannelOption.AUTO_READ, false).bind(LocalConfig.ins().getLocalPort());

        bindFuture.sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }

}

From source file:com.xx_dev.apn.socks.remote.SocksServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from www.java2  s.co m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler("NET_LOGGER", LogLevel.DEBUG))
                .childHandler(new SocksServerInitializer());
        b.bind(RemoteConfig.ins().getListenPort()).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.xx_dev.port_forwared.HexDumpProxy.java

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    List<HexDumpForwardRule> ruleList = new ArrayList<HexDumpForwardRule>();
    HexDumpForwardRule r3 = new HexDumpForwardRule(9000, "imap.gmail.com", 993, true);
    ruleList.add(r3);/*from ww w  .  j av  a2s .c o  m*/
    HexDumpForwardRule r4 = new HexDumpForwardRule(9001, "smtp.gmail.com", 465, true);
    ruleList.add(r4);

    List<ChannelFuture> bindFutureList = new ArrayList<ChannelFuture>();

    try {
        for (HexDumpForwardRule r : ruleList) {

            ServerBootstrap b = new ServerBootstrap();
            ChannelFuture bindFuture = b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler("BYTE_LOGGER", LogLevel.DEBUG))
                    .childHandler(
                            new HexDumpProxyInitializer(r.getRemoteHost(), r.getRemotePort(), r.isRemoteSsl()))
                    .childOption(ChannelOption.AUTO_READ, false).bind(r.getLocalPort());

            bindFutureList.add(bindFuture);

        }
        for (ChannelFuture f : bindFutureList) {
            f.sync().channel().closeFuture().sync();
        }
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }

}

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  www.j  a  va 2 s.  c  o  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:com.yahoo.ads.pb.network.netty.NettyPistachioServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//  w  w w .j av  a2s .c om
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } else {
        sslCtx = null;
    }

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new NettyPistachioServerInitializer(sslCtx));

        b.bind(PORT).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.yahoo.pulsar.broker.service.BrokerService.java

License:Apache License

public void start() throws Exception {
    this.producerNameGenerator = new DistributedIdGenerator(pulsar.getZkClient(), producerNameGeneratorPath,
            pulsar.getConfiguration().getClusterName());

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    bootstrap.group(acceptorGroup, workerGroup);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
            new AdaptiveRecvByteBufAllocator(1024, 16 * 1024, 1 * 1024 * 1024));

    if (workerGroup instanceof EpollEventLoopGroup) {
        bootstrap.channel(EpollServerSocketChannel.class);
        bootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    } else {//w ww.j  a v a 2s.c  o m
        bootstrap.channel(NioServerSocketChannel.class);
    }

    ServiceConfiguration serviceConfig = pulsar.getConfiguration();

    bootstrap.childHandler(new PulsarChannelInitializer(this, serviceConfig, false));
    // Bind and start to accept incoming connections.
    bootstrap.bind(new InetSocketAddress(pulsar.getBindAddress(), port)).sync();
    log.info("Started Pulsar Broker service on port {}", port);

    if (serviceConfig.isTlsEnabled()) {
        ServerBootstrap tlsBootstrap = bootstrap.clone();
        tlsBootstrap.childHandler(new PulsarChannelInitializer(this, serviceConfig, true));
        tlsBootstrap.bind(new InetSocketAddress(pulsar.getBindAddress(), tlsPort)).sync();
        log.info("Started Pulsar Broker TLS service on port {}", tlsPort);
    }

    // start other housekeeping functions
    this.startStatsUpdater();
    this.startInactivityMonitor();
    this.startMessageExpiryMonitor();
    this.startBacklogQuotaChecker();
}