Example usage for io.netty.bootstrap ServerBootstrap ServerBootstrap

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

Introduction

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

Prototype

public ServerBootstrap() 

Source Link

Usage

From source file:com.wolfbe.configcenter.remoting.netty.NettyRemotingServer.java

License:Apache License

public NettyRemotingServer(final NettyServerConfig nettyServerConfig,
        final ChannelEventListener channelEventListener) {
    super(nettyServerConfig.getServerOnewaySemaphoreValue(), nettyServerConfig.getServerAsyncSemaphoreValue());
    this.serverBootstrap = new ServerBootstrap();
    this.nettyServerConfig = nettyServerConfig;
    this.channelEventListener = channelEventListener;

    int publicThreadNums = nettyServerConfig.getServerCallbackExecutorThreads();
    if (publicThreadNums <= 0) {
        publicThreadNums = 4;/*www .  j a  v a2 s  .  c  o m*/
    }

    this.publicExecutor = Executors.newFixedThreadPool(publicThreadNums, new ThreadFactory() {
        private AtomicInteger threadIndex = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "NettyServerPublicExecutor_" + this.threadIndex.incrementAndGet());
        }
    });

    this.eventLoopGroupBoss = new NioEventLoopGroup(1, new ThreadFactory() {
        private AtomicInteger threadIndex = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, String.format("NettyBoss_%d", this.threadIndex.incrementAndGet()));
        }
    });

    if (RemotingUtil.isLinuxPlatform() //
            && nettyServerConfig.isUseEpollNativeSelector()) {
        this.eventLoopGroupSelector = new EpollEventLoopGroup(nettyServerConfig.getServerSelectorThreads(),
                new ThreadFactory() {
                    private AtomicInteger threadIndex = new AtomicInteger(0);
                    private int threadTotal = nettyServerConfig.getServerSelectorThreads();

                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(r, String.format("NettyServerEPOLLSelector_%d_%d", threadTotal,
                                this.threadIndex.incrementAndGet()));
                    }
                });
    } else {
        this.eventLoopGroupSelector = new NioEventLoopGroup(nettyServerConfig.getServerSelectorThreads(),
                new ThreadFactory() {
                    private AtomicInteger threadIndex = new AtomicInteger(0);
                    private int threadTotal = nettyServerConfig.getServerSelectorThreads();

                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(r, String.format("NettyServerNIOSelector_%d_%d", threadTotal,
                                this.threadIndex.incrementAndGet()));
                    }
                });
    }
}

From source file:com.wuma.file.FileServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    //        final SslContext sslCtx;
    //        if (SSL) {
    //            SelfSignedCertificate ssc = new SelfSignedCertificate();
    //            sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    //        } else {
    //            sslCtx = null;
    //        }/*from   w  w  w.ja v  a2 s.co m*/

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        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();
                        //                     if (sslCtx != null) {
                        //                         p.addLast(sslCtx.newHandler(ch.alloc()));
                        //                     }
                        p.addLast(new StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(8192),
                                new StringDecoder(CharsetUtil.UTF_8), new ChunkedWriteHandler(),
                                new FileServerHandler());
                    }
                });

        // 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.wx3.galacdecks.networking.NettyWebSocketServer.java

License:Open Source License

public void start() {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {// w  w  w.j  a  v  a  2 s.  c o m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new WebSocketServerInitializer(this, null));

        Channel ch = b.bind(port).sync().channel();
        // This blocks until the channel is closed:
        ch.closeFuture().sync();

    } catch (InterruptedException e) {
        // What do we do with an interupted exception?
        e.printStackTrace();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.xiovr.unibot.bot.network.impl.ConnectionFactoryImpl.java

License:Open Source License

@Override
public void createProxyListeners(@NonNull List<BotContext> proxyBots) {

    // Start boss client listeners
    for (int stage = 0; stage < botEnvironment.getClientAddresses().size(); ++stage) {
        EventsGroup eg = new EventsGroup();

        bs = new ServerBootstrap();
        bs.group(eg.bossClientEventGroup, eg.clientEventGroup);
        bs.channel(NioServerSocketChannel.class);
        bs.childHandler(new ClientConnectionChannelInitializer(proxyBots, stage));
        bs.option(ChannelOption.SO_BACKLOG, 128);
        bs.childOption(ChannelOption.SO_KEEPALIVE, true);
        ChannelFuture cf = bs.bind(botEnvironment.getClientAddresses().get(stage));

        eventsList.add(eg);/*from  ww w.  ja va2  s . c o m*/
        bossFutures.add(cf);
    }
}

From source file:com.xiovr.unibot.utils.EchoServer.java

License:Open Source License

public void startServer() {

    if (bStarted) {
        return;/*from  ww w  .ja  v  a2  s . c  om*/
    }
    // Configure the server.
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    try {
        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 EchoServerHandler());
                    }
                });

        // Start the server.
        cf = b.bind(addr).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                //               System.out.println("Echo server started");
                EchoServer.this.bStarted = true;
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }

}

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();
    // ???/*from www .j  a  va 2  s.  c  o 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 .j  a v  a 2s .c  o  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   www .  j  av  a  2 s .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  w  w  .j a v  a 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 {/*from   w w  w  .  j  a v  a2s .c o m*/
        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();
    }

}