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.hxr.javatone.concurrency.netty.official.objectecho.ObjectEchoServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from w ww  .ja v  a 2  s .  c  o m*/
        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 ObjectEncoder(),
                                new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                                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.hxr.javatone.concurrency.netty.official.securechat.SecureChatServer.java

License:Apache License

public void run() throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/* w w  w  . j  a v a 2 s .c om*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new SecureChatServerInitializer());

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

From source file:com.hxr.javatone.concurrency.netty.official.telnet.TelnetServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {// w w  w  .  j ava2  s. c o m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new TelnetServerInitializer());

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

From source file:com.hxr.javatone.nettyguide.d12.server.NettyServer.java

License:Apache License

public void bind() throws Exception {
    // ??NIO//from w w w .jav  a  2  s. com
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100)
            .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws IOException {
                    ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4));
                    ch.pipeline().addLast(new NettyMessageEncoder());
                    ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50));
                    ch.pipeline().addLast(new LoginAuthRespHandler());
                    ch.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler());
                }
            });

    // ???
    b.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync().channel().closeFuture().sync().channel();
    System.out.println("Netty server start ok : " + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT));
}

From source file:com.hzmsc.scada.Jmtis.server.PortUnificationServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL context
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    final SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {// w  w w .  j a  va  2 s  .  com
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        System.out.println("initChannel..........................");
                        ch.pipeline().addLast(new PortUnificationServerHandler(sslCtx));
                    }
                });

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

From source file:com.hzq.nio.websocket.WebSocketServer.java

License:Apache License

public void run(int port) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from   w  ww  .j  ava2 s .  c o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast("http-codec", new HttpServerCodec());
                        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
                        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                        pipeline.addLast("handler", new WebSocketServerHandler());
                    }
                });

        Channel ch = b.bind(port).sync().channel();
        System.out.println("Web socket server started at port " + port + '.');
        System.out.println("Open your browser and navigate to http://localhost:" + port + '/');
        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.ibm.crail.datanode.netty.server.NettyServer.java

License:Apache License

public void run() {
    /* start the netty server */
    EventLoopGroup acceptGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//  w w w  .j  a  v  a 2 s  . c o m
        ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, workerGroup);
        /* we use sockets */
        boot.channel(NioServerSocketChannel.class);
        /* for new incoming connection */
        boot.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                LOG.info("TID: " + Thread.currentThread().getId()
                        + " , a new client connection has arrived from : " + ch.remoteAddress().toString());
                /* incoming pipeline */
                ch.pipeline().addLast(new RdmaDecoderRx(), /* this makes full RDMA messages */
                        new IncomingRequestHandler(ch, dataNode));
                /* outgoing pipeline */
                //ch.pipeline().addLast(new RdmaEncoderTx());
            }
        });
        /* general optimization settings */
        boot.option(ChannelOption.SO_BACKLOG, 1024);
        boot.childOption(ChannelOption.SO_KEEPALIVE, true);

        /* now we bind the server and start */
        ChannelFuture f = boot.bind(this.inetSocketAddress.getAddress(), this.inetSocketAddress.getPort())
                .sync();
        LOG.info("Datanode binded to : " + this.inetSocketAddress);
        /* at this point we are binded and ready */
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        acceptGroup.shutdownGracefully();
        LOG.info("Datanode at " + this.inetSocketAddress + " is shutdown");
    }
}

From source file:com.ibm.crail.namenode.rpc.netty.NettyNameNode.java

License:Apache License

public void run(final RpcNameNodeService service) {
    /* here we run the incoming RPC service */
    InetSocketAddress inetSocketAddress = CrailUtils.getNameNodeAddress();
    LOG.info("Starting the NettyNamenode service at : " + inetSocketAddress);
    /* start the netty server */
    EventLoopGroup acceptGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//  ww w.j  a  va2s  .  c o  m
        ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, workerGroup);
        /* we use sockets */
        boot.channel(NioServerSocketChannel.class);
        /* for new incoming connection */
        boot.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                LOG.info("A new connection has arrived from : " + ch.remoteAddress().toString());
                /* incoming pipeline */
                ch.pipeline().addLast("RequestDecoder", new RequestDecoder());
                ch.pipeline().addLast("NNProcessor", new NamenodeProcessor(service));
                /* outgoing pipeline */
                ch.pipeline().addLast("ResponseEncoder", new ResponseEncoder());
            }
        });
        /* general optimization settings */
        boot.option(ChannelOption.SO_BACKLOG, 1024);
        boot.childOption(ChannelOption.SO_KEEPALIVE, true);

        /* now we bind the server and start */
        ChannelFuture f = boot.bind(inetSocketAddress.getAddress(), inetSocketAddress.getPort()).sync();
        /* at this point we are binded and ready */
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        acceptGroup.shutdownGracefully();
        LOG.info("Netty namenode at " + inetSocketAddress + " is shutdown");
    }
}

From source file:com.im.test.WebSocketServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from  w w  w  .  ja  v  a 2s . c o  m
        ServerBootstrap server = new ServerBootstrap();
        server.group(bossGroup, workerGroup);
        server.channel(NioServerSocketChannel.class);
        server.handler(new LoggingHandler(LogLevel.INFO));
        server.childHandler(new WebSocketServerInitializer());

        Channel ch = server.bind(PORT).sync().channel();
        System.out.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:com.informatica.surf.sources.http.HttpListener.java

License:Apache License

public void start() throws InterruptedException {
    ServerBootstrap bs = new ServerBootstrap();
    bs.group(_parentGroup, _childGroup).channel(NioServerSocketChannel.class)
            .childHandler(new HttpServerInitializer(_ppaths, _wpaths, _messageQueue));
    _channel = bs.bind(_port).sync().channel();
    _logger.info("Started HttpListener listening on port {}", _port);
}