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.caricah.iotracah.server.netty.ServerImpl.java

License:Apache License

/**
 * The @link configure method is responsible for starting the implementation server processes.
 * The implementation should return once the server has started this allows
 * the launcher to maintain the life of the application.
 *
 * @throws UnRetriableException//from w w  w. j  a  v  a2 s  .  c o m
 */
public void initiate() throws UnRetriableException {

    log.info(" configure : initiating the netty server.");

    try {

        int countOfAvailableProcessors = Runtime.getRuntime().availableProcessors() + 1;

        if (Epoll.isAvailable()) {
            bossEventLoopGroup = new EpollEventLoopGroup(2, getExecutorService());
            workerEventLoopGroup = new EpollEventLoopGroup(countOfAvailableProcessors, getExecutorService());

        } else {
            bossEventLoopGroup = new NioEventLoopGroup(2, getExecutorService());
            workerEventLoopGroup = new NioEventLoopGroup(countOfAvailableProcessors, getExecutorService());
        }

        //Initialize listener for TCP
        ServerBootstrap tcpBootstrap = new ServerBootstrap();
        tcpBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
        tcpBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
        tcpBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);

        tcpBootstrap = tcpBootstrap.group(bossEventLoopGroup, workerEventLoopGroup);

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

        tcpBootstrap = tcpBootstrap.handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(getServerInitializer(this, getConnectionTimeout()));

        ChannelFuture tcpChannelFuture = tcpBootstrap.bind(getTcpPort()).sync();
        tcpChannel = tcpChannelFuture.channel();

        if (isSslEnabled()) {
            //Initialize listener for SSL
            ServerBootstrap sslBootstrap = new ServerBootstrap();
            sslBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
            sslBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024);
            sslBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024);

            sslBootstrap = sslBootstrap.group(bossEventLoopGroup, workerEventLoopGroup);

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

            sslBootstrap = sslBootstrap.handler(new LoggingHandler(LogLevel.INFO))
                    .childHandler(getServerInitializer(this, getConnectionTimeout(), getSslHandler()));

            ChannelFuture sslChannelFuture = sslBootstrap.bind(getSslPort()).sync();
            sslChannel = sslChannelFuture.channel();
        }

    } catch (InterruptedException e) {

        log.error(" configure : Initialization issues ", e);

        throw new UnRetriableException(e);

    }

}

From source file:com.carlos.netty.server.ObjectEchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from  w  w  w  . ja  va2 s.  c o m*/
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } 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 EchoLoginHandler(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(), new StringDecoder(), new ObjectEchoServerHandler());
                    }
                });

        // Bind and start to accept incoming connections.
        b.bind(PORT).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.cat.netty.file.FileServer.java

License:Apache License

public void run(int port) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*w w w  .  j  a  v a  2s .com*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).childHandler(new ChannelInitializer<SocketChannel>() {
                    /*
                     * (non-Javadoc)
                     *
                     * @see
                     * io.netty.channel.ChannelInitializer#initChannel(io
                     * .netty.channel.Channel)
                     */
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8),
                                new LineBasedFrameDecoder(1024), new StringDecoder(CharsetUtil.UTF_8),
                                new FileServerHandler());
                    }
                });
        ChannelFuture f = b.bind(port).sync();
        System.out.println("Server start at port : " + port);
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.cats.version.httpserver.HttpStaticVersionMonitorFileServer.java

License:Apache License

private void init() throws Exception {
    // Configure SSL.
    final SslContext sslCtx = null;

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from w  w  w.j  ava  2  s.c  om*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new HttpStaticFileServerInitializer(sslCtx));

        Channel ch = b.bind(PORT).sync().channel();

        System.err.println("Open your web browser and navigate to http://127.0.0.1:" + PORT + '/');
        System.err.println("Welcome use " + VERSION_DESC);

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

From source file:com.ccompass.netty.proxy.Proxy.java

License:Apache License

public static void main(String[] args) throws Exception {
    ProxyConfig.loadConfig();/*from   www .j av  a 2  s  .  co m*/
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    checkOtherChannel(ProxyConfig.config.checktimes);
    //?grops
    for (int i = 0; i < ProxyConfig.config.branchList.size(); i++) {
        ChannelGroup group = new DefaultChannelGroup("server-group", null);
        NettyClient.sinkGroups.add(group);
    }
    //????
    if (ProxyConfig.config.branchNumbers > 0) {
        List<List<Channel>> list = new ArrayList();
        for (int i = 0; i < ProxyConfig.config.branchList.size(); i++) {
            List<Channel> channels = new ArrayList<Channel>();
            list.add(channels);
        }
        NettyClient.setSinkChannels(list);
    }
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                //    .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ProxyInitializer(ProxyConfig.config.mainIP, ProxyConfig.config.mainPort))
                .childOption(ChannelOption.AUTO_READ, false).bind(ProxyConfig.config.proxyPort).sync().channel()
                .closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.cdg.study.netty.discard.DiscardServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1); // bossGroup 1?  ??
    EventLoopGroup workerGroup = new NioEventLoopGroup(); // default *2  ??
    try {//from   w  ww. j a va2 s  .com
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new DiscardServerHandler());

        ChannelFuture f = b.bind(8010).sync();

        System.err.println("Ready for 0.0.0.0:8010");

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

From source file:com.cdg.study.netty.echo.EchoServer.java

License:Open Source License

public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*  w ww . j  a  v a 2 s . co m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new EchoServerHandler());

        ChannelFuture f = b.bind(8011).sync();

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

From source file:com.cdg.study.netty.util.NettyStartupUtil.java

License:Open Source License

public static void runServer(int port, ChannelHandler childHandler, Consumer<ServerBootstrap> block)
        throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  w  w  w .  j  a v  a2 s. c om*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);
        b.handler(new LoggingHandler(LogLevel.INFO));
        b.childHandler(childHandler);
        block.accept(b);
        Channel ch = b.bind(port).sync().channel();
        System.err.println("Ready for 0.0.0.0:" + port);
        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.celeral.netlet.benchmark.netty.EchoTcpServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    int port;/*from ww w.  ja  va 2s .c  o m*/
    if (args.length > 0) {
        port = Integer.parseInt(args[0]);
    } else {
        port = 8080;
    }

    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new EchoTcpServer());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture f = b.bind(port).sync(); // (7)
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:com.changxx.phei.netty.codec.protobuf.SubReqServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO/*from   w  ww. ja v  a2s . c o m*/
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    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) {
                        // ch.pipeline().addLast(
                        // new ProtobufVarint32FrameDecoder());
                        ch.pipeline().addLast(
                                new ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance()));
                        ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
                        ch.pipeline().addLast(new ProtobufEncoder());
                        ch.pipeline().addLast(new SubReqServerHandler());
                    }
                });

        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}