Example usage for io.netty.channel EventLoopGroup shutdownGracefully

List of usage examples for io.netty.channel EventLoopGroup shutdownGracefully

Introduction

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

Prototype

Future<?> shutdownGracefully();

Source Link

Document

Shortcut method for #shutdownGracefully(long,long,TimeUnit) with sensible default values.

Usage

From source file:com.phei.netty.protocol.websocket.server.WebSocketServer.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  va  2  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();
                        //???http?
                        pipeline.addLast("http-codec", new HttpServerCodec());
                        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
                        //???html5????websocket
                        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.proxy.ProxyServer.java

License:Apache License

public void start(final String remoteHost, final int remotePort) throws InterruptedException {
    System.err.println("Proxying *:" + LOCAL_PORT + " to " + REMOTE_HOST + ':' + REMOTE_PORT + " ...");

    // Configure the bootstrap.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from   ww w  .j  a  va 2s . c  o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ProxyChannelInit(remoteHost, remotePort))
                .childOption(ChannelOption.AUTO_READ, false).bind(LOCAL_PORT).sync().channel().closeFuture()
                .sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.qq.servlet.demo.netty.sample.myprotocol.MyProtocolClient.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from www . j  a va 2  s  .c o  m
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NioSocketChannel.class);
        b.handler(new MyProtocolClientChannelHandler());

        // Start the connection attempt.
        Channel channel = b.connect(host, port).sync().channel();//

        channel.closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.qq.servlet.demo.netty.sample.myprotocol.MyProtocolServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(3);
    try {/*from  ww  w .ja v a 2  s  .c  o  m*/
        final ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup);
        b.channel(NioServerSocketChannel.class);
        b.childHandler(new MyProtocolServerChannelHandler());
        new Thread() {
            @Override
            public void run() {
                Channel channel;
                try {
                    System.out.println("server is starting....");
                    channel = b.bind(port).sync().channel();
                    channel.closeFuture().sync();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    } finally {
        byte[] b = new byte[32];
        String read = null;
        do {
            if (read != null)
                System.out.println("" + read);
            int c = System.in.read(b);
            read = new String(b, 0, c);
        } while (read == null || !read.startsWith("exit"));

        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.qq.servlet.demo.netty.sample.objectecho.ObjectEchoClient.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*w  ww  . j  av  a  2 s.  com*/
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NioSocketChannel.class);
        b.handler(new ObjectEchoClientChannelHandler());

        // Start the connection attempt.
        Channel channel = b.connect(host, port).sync().channel();//

        channel.closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.qq.servlet.demo.netty.sample.objectecho.ObjectEchoServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(2);
    EventLoopGroup workerGroup = new NioEventLoopGroup(3);
    try {//from w  ww  .  ja v a2  s.c o  m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup);
        b.channel(NioServerSocketChannel.class);
        b.childHandler(new ObjectServerChannelHandler());
        System.out.println("start server...");
        // Bind and start to accept incoming connections.
        Channel channel = b.bind(port).sync().channel();
        //waitting for close this server channel
        channel.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.qq.servlet.demo.netty.sample.telnet.TelnetClient.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {/* ww  w .  j  av  a  2s  .  co m*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new TelnetClientInitializer());

        // Start the connection attempt.
        Channel ch = b.connect(host, port).sync().channel();
        ch.writeAndFlush("askfjasfljaslfsajl");
        // Read commands from the stdin.
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (;;) {
            String line = in.readLine();
            if (line == null) {
                break;
            }

            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(line + "\r\n");

            // If user typed the 'bye' command, wait until the server closes
            // the connection.
            if ("bye".equals(line.toLowerCase())) {
                ch.closeFuture().sync();
                break;
            }
        }

        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.qq.servlet.demo.netty.sample.telnet.TelnetServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  w w  w .  j  av  a 2  s  . co  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup);
        b.channel(NioServerSocketChannel.class);
        b.childHandler(new TelnetServerInitializer());

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

From source file:com.qq.servlet.demo.netty.sample.thrift.client.ThriftProtocolClient.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*  ww  w  . java 2  s .c  om*/
        Bootstrap b = new Bootstrap();
        b.group(group);
        b.channel(NioSocketChannel.class);
        b.handler(new ThriftProtocolClientChannelHandler());

        // Start the connection attempt.
        Channel channel = b.connect(host, port).sync().channel();//

        channel.closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.qq.servlet.demo.netty.sample.thrift.service.ThriftProtocolServer.java

License:Apache License

public void run() throws Exception {
    final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    final EventLoopGroup workerGroup = new NioEventLoopGroup(3);

    startCloseCmdThread(bossGroup, workerGroup);

    try {/*from   w w w.  j a va  2  s. co m*/
        final ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup);
        b.channel(NioServerSocketChannel.class);
        b.childHandler(new ThriftProtocolServerChannelHandler());
        Channel channel;
        System.out.println("server is starting....");
        channel = b.bind(port).sync().channel();
        channel.closeFuture().sync();
        System.out.println("server is closing....");

    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        System.out.println("server is closed");
    }
}