Example usage for io.netty.channel.nio NioEventLoopGroup shutdownGracefully

List of usage examples for io.netty.channel.nio NioEventLoopGroup shutdownGracefully

Introduction

In this page you can find the example usage for io.netty.channel.nio NioEventLoopGroup shutdownGracefully.

Prototype

@Override
    public Future<?> shutdownGracefully() 

Source Link

Usage

From source file:HttpRouterServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    Router<String> router = new Router<String>().GET(PUBLIC_DIR + ":id", "public").GET("/", "index")
            .GET(PUBLIC_DIR, "index")
            //            .GET("/image", "base64")
            //            .GET("/img", "image")
            //            .GET("/",             "Index page")
            //            .GET("/articles/:id", "Article show page")
            .notFound("404 Not Found");
    System.out.println(router);//from  w  ww .jav a2  s.c  o m

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

    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).childOption(ChannelOption.TCP_NODELAY, java.lang.Boolean.TRUE)
                .childOption(ChannelOption.SO_KEEPALIVE, java.lang.Boolean.TRUE)
                .channel(NioServerSocketChannel.class).childHandler(new HttpRouterServerInitializer(router));

        Channel ch = b.bind(PORT).sync().channel();
        System.out.println("Server started: http://127.0.0.1:" + PORT + '/');

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

From source file:com.beeswax.http.server.HttpServer.java

License:Apache License

public void run() throws InterruptedException {
    // Create event loop groups. One for incoming connections handling and
    // second for handling actual event by workers
    final NioEventLoopGroup bossGroup = new NioEventLoopGroup(serverConfig.bossGroupSize);
    final NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try {/* www. jav  a2s  . c  om*/
        ServerBootstrap bootStrap = new ServerBootstrap();
        bootStrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                // SO_BACKLOG : The maximum queue length for incoming connections.
                .option(ChannelOption.SO_BACKLOG, serverConfig.backlogSize)
                // TCP_NODELAY: option to disable Nagle's algorithm to achieve lower latency on every packet sent
                .option(ChannelOption.TCP_NODELAY, serverConfig.tcpNodelay)
                // SO_KEEPALIVE: option to enable keep-alive packets for a socket connection
                .childOption(ChannelOption.SO_KEEPALIVE, serverConfig.keepAlive)
                .childHandler(new HttpServerChannelInitializer(serverConfig, handlerFactory));

        // bind to port
        final ChannelFuture channelFuture = bootStrap.bind(serverConfig.port).sync();

        // Wait until the server socket is closed.
        channelFuture.channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.doctor.netty5.example.chatroom.ChatRoomClient.java

License:Apache License

public void start() throws InterruptedException {
    NioEventLoopGroup workersGroup = new NioEventLoopGroup(1);

    try {//w  w w .j a v  a 2s  .c  o m
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port)
                .handler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(
                                new DelimiterBasedFrameDecoder(8049, true, Delimiters.lineDelimiter()));
                        ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new ChatRoomClientHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.connect().sync();
        channelFuture.channel().closeFuture().sync();

    } finally {
        workersGroup.shutdownGracefully();
    }
}

From source file:com.doctor.netty5.example.chatroom.ChatRoomServer.java

License:Apache License

public void start() throws InterruptedException {
    ServerBootstrap bootstrap = new ServerBootstrap();
    NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
    NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try {/* ww  w . ja va2s. c o m*/
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(
                                new DelimiterBasedFrameDecoder(8049, true, Delimiters.lineDelimiter()));
                        ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new ChatRoomServerHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.bind().sync();
        System.out.println(ChatRoomServer.class.getName() + " started and listen on port:"
                + channelFuture.channel().localAddress());

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

From source file:com.doctor.netty5.example.echo.EchoClient.java

License:Apache License

public void start() throws InterruptedException {
    NioEventLoopGroup workersGroup = new NioEventLoopGroup(1);

    try {//from ww  w .j  a  va  2  s .  c  o m
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port)
                .handler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new LineBasedFrameDecoder(2048));
                        ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new EchoClientHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.connect().sync();
        channelFuture.channel().closeFuture().sync();

    } finally {
        workersGroup.shutdownGracefully();
    }
}

From source file:com.doctor.netty5.example.echo.EchoServer.java

License:Apache License

public void start() throws InterruptedException {
    ServerBootstrap bootstrap = new ServerBootstrap();
    NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
    NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try {//from  ww  w .j av a 2  s  .  co m
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new LineBasedFrameDecoder(2048));
                        ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8));
                        ch.pipeline().addLast(new EchoServerHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.bind().sync();
        System.out.println(EchoServer.class.getName() + " started and listen on port:"
                + channelFuture.channel().localAddress());

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

From source file:com.doctor.netty5.example.echo_object.EchoClient.java

License:Apache License

public void start() throws InterruptedException {
    NioEventLoopGroup workersGroup = new NioEventLoopGroup(1);

    try {/*from w ww  .j  a va  2 s.  c  om*/
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port)
                .handler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ObjectEncoder());
                        ch.pipeline().addLast(new ObjectDecoder(
                                ClassResolvers.softCachingResolver(this.getClass().getClassLoader())));
                        ch.pipeline().addLast(new EchoClientHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.connect().sync();
        channelFuture.channel().closeFuture().sync();

    } finally {
        workersGroup.shutdownGracefully();
    }
}

From source file:com.doctor.netty5.example.echo_object.EchoServer.java

License:Apache License

public void start() throws InterruptedException {
    ServerBootstrap bootstrap = new ServerBootstrap();
    NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
    NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try {//from   www  .  j a va 2  s .  c om
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ObjectEncoder());
                        ch.pipeline().addLast(new ObjectDecoder(
                                ClassResolvers.softCachingResolver(this.getClass().getClassLoader())));
                        ch.pipeline().addLast(new EchoServerHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.bind().sync();
        System.out.println(EchoServer.class.getName() + " started and listen on port:"
                + channelFuture.channel().localAddress());

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

From source file:com.doctor.netty5.example.factorial_algorithm.FactorialClient.java

License:Apache License

public void start() throws InterruptedException {
    NioEventLoopGroup workersGroup = new NioEventLoopGroup(1);

    try {//from w  ww.ja v a2  s. c om
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port)
                .handler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new NumberEncoder());
                        ch.pipeline().addLast(new BigIntegerDecoder());
                        ch.pipeline().addLast(new FactorialClientHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.connect().sync();
        channelFuture.channel().closeFuture().sync();

    } finally {
        workersGroup.shutdownGracefully();
    }
}

From source file:com.doctor.netty5.example.factorial_algorithm.FactorialServer.java

License:Apache License

public void start() throws InterruptedException {
    ServerBootstrap bootstrap = new ServerBootstrap();
    NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
    NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try {/*from   w  w w .  j  a  v  a2  s .c  om*/
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new NumberEncoder());
                        ch.pipeline().addLast(new BigIntegerDecoder());
                        ch.pipeline().addLast(new FactorialServerHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.bind().sync();
        System.out.println(FactorialServer.class.getName() + " started and listen on port:"
                + channelFuture.channel().localAddress());

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