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:cn.david.socks.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  . j  a  v a2  s .  c o  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new SocksServerInitializer());
        b.bind(PORT).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:cn.dennishucd.nettyhttpserver.HttpServer.java

License:Apache License

public void start(String host, int port) throws Exception {
    //create a reactor thread pool for NIO acceptor
    EventLoopGroup bossGroup = new NioEventLoopGroup();

    //create a reactor thread pool for NIO handler
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {//from   w  w  w. j  a  v a  2s. com
        ServerBootstrap b = new ServerBootstrap();

        b.option(ChannelOption.SO_BACKLOG, 1024);

        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new PushServerInitializer());

        ChannelFuture f = b.bind(host, port).sync();

        logger.info("The http server is started on " + host + ":" + port);

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

From source file:cn.npt.net.websocket.WebSocketServer.java

License:Apache License

private void init(int port, boolean ssl, String webSocketPath) throws Exception {
    final SslContext sslCtx;
    if (this.ssl) {
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {/*from   ww  w.  j  a v a  2s  . c om*/
        sslCtx = null;
    }

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

        Channel ch = b.bind(this.port).sync().channel();

        ch.closeFuture().sync();//
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
    System.out.println("start websocket...");
}

From source file:cn.wantedonline.puppy.Bootstrap.java

License:Apache License

/**
 * ?HttpServerBootstrap,??//from   w w  w  .j av a  2  s  .c om
 */
private ServerBootstrap initHttpServerBootstrap() {
    EventLoopGroup bossGroup = httpServerConfig.getBossEventLoopGroup();
    NioEventLoopGroup workerGroup = httpServerConfig.getWorkerEventLoopGroup();
    nioWorkerStat.registerWorkers(workerGroup);
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(httpServerConfig.getHttpServerHandler());
    return serverBootstrap;
}

From source file:cn.wcl.test.netty.server.netty.NettyHttpServer.java

License:Apache License

private void init() throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from   w  ww .  j a v  a  2 s.  co m*/
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }

    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup);
        b.channel(NioServerSocketChannel.class);
        b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
        b.childHandler(new HttpUploadServerInitializer(sslCtx, applicationContext, needlogin));

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

        System.err.println("Open your web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:"
                + port + '/');

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

From source file:cn.yesway.demo.book.protocol.http.fileServer.HttpFileServer.java

License:Apache License

public void run(final int port, final String url) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/* w w w  .ja  v  a2 s .com*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
                        ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
                        ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
                        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                        ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url));
                    }
                });
        ChannelFuture future = b.bind("127.0.0.1", port).sync();
        System.out.println("HTTP??? : " + "http://127.0.0.1:" + port + url);
        future.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:cn.yesway.demo.book.protocol.http.xml.server.HttpXmlServer.java

License:Apache License

public void run(final int port) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from  w ww .ja v a 2s .  c  om
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
                        ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
                        ch.pipeline().addLast("xml-decoder", new HttpXmlRequestDecoder(Order.class, true));
                        ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
                        ch.pipeline().addLast("xml-encoder", new HttpXmlResponseEncoder());
                        ch.pipeline().addLast("xmlServerHandler", new HttpXmlServerHandler());
                    }
                });
        ChannelFuture future = b.bind(new InetSocketAddress(port)).sync();
        System.out.println("HTTP??? : " + "http://localhost:" + port);
        future.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:cn.zyf.ObjectStorageServer.java

License:Open Source License

private void run(String host, int port, int packageSize) {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    EventLoopGroup backendGroup = new NioEventLoopGroup();

    try {/*from w ww.jav  a 2s . com*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
                        ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(packageSize));
                        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                        ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
                        ch.pipeline().addLast("oss-decoder", new ParseRequestHandler());
                        ch.pipeline().addLast("oss-filter", new FilterRequestHandler());
                        ch.pipeline().addLast("oss-authen", new AuthenticationHandler());
                        ch.pipeline().addLast("oss-author", new AuthorizationHandler());
                        ch.pipeline().addLast(backendGroup, "oss-backend", new ObjectStorageHandler());
                    }
                });

        ChannelFuture future = b.bind(host, port).sync();
        LOG.info("start Http Server with hostname=" + host + ":" + port);
        future.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        LOG.error("occur InterruptedException ", e);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:co.rsk.rpc.netty.Web3WebSocketServer.java

License:Open Source License

public void start() throws InterruptedException {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override/*from  ww w  .  j  av a  2s.c  o  m*/
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    p.addLast(new HttpServerCodec());
                    p.addLast(new HttpObjectAggregator(1024 * 1024 * 5));
                    p.addLast(new WebSocketServerProtocolHandler("/websocket"));
                    p.addLast(jsonRpcHandler);
                    p.addLast(web3ServerHandler);
                    p.addLast(new Web3ResultWebSocketResponseHandler());
                }
            });
    webSocketChannel = b.bind(host, port);
    webSocketChannel.sync();
}

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  ww w .  j a v a2 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(listenPort).sync();
    LOGGER.warn("Server started,listen at: " + listenPort + ", businessThreads is " + businessThreads);
}