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.digisky.innerproxy.testclient.HttpSnoopClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    uri = new URI(URL);
    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
    int port = uri.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;/*  w  w  w.  j  a v  a  2  s .  co  m*/
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }

    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        System.err.println("Only HTTP(S) is supported.");
        return;
    }

    // Configure SSL context if necessary.
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new HttpSnoopClientInitializer(sslCtx));

        // Make the connection attempt.
        //            Channel ch = b.connect(host, port).sync().channel();
        TestTimer.add("main()::85");
        //test(ch, "username_register", R_username_register.j);
        //test(ch, "normal_login", R_normal_login.j_username);
        //test(ch, "normal_login", R_normal_login.j_openid);
        //test(ch, "normal_login", R_normal_login.j_telphone);
        //test(ch, "normal_login", R_normal_login.j_devid);
        //test(ch, "normal_login", R_normal_login.j_email);
        //test(ch, "email_bind", R_email_bind.j);
        //test(ch, "telphone_bind", R_telphone_bind.j);
        //test(ch, "req_telphone_vcode", R_req_telphone_vcode.j);
        //test(ch, "auth", R_auth.j);
        //test(ch, "telphone_register", R_register.j_telphone_register);
        //test(ch, "email_register", R_register.j_email_register);
        //test(ch, "req_register_mode", R_req_register_mode.j);
        //test(ch, "identify_exist", R_identify_exist.j);

    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.digisky.outerproxy.testclient.HttpSnoopClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    uri = new URI(URL);
    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
    int port = uri.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;//from w  ww .  j  a  v a 2  s . c  om
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }

    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        System.err.println("Only HTTP(S) is supported.");
        return;
    }

    // Configure SSL context if necessary.
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new HttpSnoopClientInitializer(sslCtx));

        // Make the connection attempt.
        Channel ch = b.connect("192.168.60.163", 8089).sync().channel();
        TestTimer.add("main()::85");
        //test(ch, "username_register", R_username_register.j);
        //test(ch, "normal_login", R_normal_login.j_username);
        //test(ch, "normal_login", R_normal_login.j_openid);
        //test(ch, "normal_login", R_normal_login.j_telphone);
        //test(ch, "normal_login", R_normal_login.j_devid);
        //test(ch, "normal_login", R_normal_login.j_email);
        //test(ch, "email_bind", R_email_bind.j);
        //test(ch, "telphone_bind", R_telphone_bind.j);
        //test(ch, "req_telphone_vcode", R_req_telphone_vcode.j);
        //test(ch, "auth", R_auth.j);
        //test(ch, "telphone_register", R_register.j_telphone_register);
        //test(ch, "email_register", R_register.j_email_register);
        //test(ch, "req_register_mode", R_req_register_mode.j);
        //test(ch, "identify_exist", R_identify_exist.j);
        test(ch, "third_login", R_identify_exist.j);

    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.digisky.stresstest.HttpSnoopClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    uri = new URI(URL);
    String scheme = uri.getScheme() == null ? "http" : uri.getScheme();
    String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    int port = uri.getPort();
    if (port == -1) {
        if ("http".equalsIgnoreCase(scheme)) {
            port = 80;//from   www.j  a va2  s. c om
        } else if ("https".equalsIgnoreCase(scheme)) {
            port = 443;
        }
    }

    if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
        System.err.println("Only HTTP(S) is supported.");
        return;
    }

    // Configure SSL context if necessary.
    final boolean ssl = "https".equalsIgnoreCase(scheme);
    final SslContext sslCtx;
    if (ssl) {
        sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
    } else {
        sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new HttpSnoopClientInitializer(sslCtx));

        // Make the connection attempt.
        System.out.println("try to connect!");
        ChannelFuture f = b.connect(host, port);
        f.addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) {
                // Perform post-closure operation
                // ...
                System.out.println("complete!");
                if (!future.isSuccess()) {
                    // You might get a NullPointerException here because the future
                    // might not be completed yet.
                    future.cause().printStackTrace();
                } else {
                    System.out.println("ok!!!!");
                }
            }
        });
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.dingwang.netty.client.DiscardClient.java

License:Open Source License

public void run() {
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    //BootStrapServerBootstrap,???channel?channel
    Bootstrap b = new Bootstrap();

    //?EventLoopGroup?bossworkder
    //??boss/* ww w .j  a  v a  2 s  . co m*/
    b.group(workerGroup);

    //NioServerSocketChannelNioSocketChannel,channel
    b.channel(NioSocketChannel.class);

    //??ServerBootstrap?childOption()SocketChannelchannel
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new TimeDecoder(), new TimeClientHandler());
        }
    });

    ChannelFuture f;
    try {
        //connect()bind()
        f = b.connect(host, port).sync();
        f.channel().writeAndFlush("dddddd");
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
    }
}

From source file:com.dingwang.netty.server.DiscardServer.java

License:Open Source License

public void run() {
    //NioEventLoopGroup ??I/O?Netty????EventLoopGroup
    //????????2NioEventLoopGroup
    //???boss?????worker???
    //boss?worker???
    //Channels??EventLoopGroup???
    EventLoopGroup bossGroup = new NioEventLoopGroup(5);
    EventLoopGroup workerGroup = new NioEventLoopGroup(20);

    //ServerBootstrap ?NIO????Channel??
    //????/*from  w ww . j  av  a  2 s  .c om*/
    ServerBootstrap b = new ServerBootstrap();

    //NioServerSocketChannel?Channel?
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            //?????ChannelChannelInitializer?
            //?Channel?DiscardServerHandle??
            //ChannelChannelPipeline???????
            //?pipline??????
            .childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new TimeEncoder(), new DiscardServerHandler());

                }
            })
            //????TCP/IP??socket?
            //tcpNoDelaykeepAlive?ChannelOptionChannelConfig??
            //ChannelOptions
            .option(ChannelOption.SO_BACKLOG, 128)
            //option()childOption()?option()??NioServerSocketChannel??
            //childOption()???ServerChannel?NioServerSocketChannel
            .childOption(ChannelOption.SO_KEEPALIVE, true);

    try {
        //?????8080?
        //?bind()(???)
        ChannelFuture f = b.bind(port).sync();
        f.channel().closeFuture().sync();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

}

From source file:com.dingwang.netty.server.PersonServer.java

License:Open Source License

public void run() {
    //NioEventLoopGroup ??I/O?Netty????EventLoopGroup
    //????????2NioEventLoopGroup
    //???boss?????worker???
    //boss?worker???
    //Channels??EventLoopGroup???
    EventLoopGroup bossGroup = new NioEventLoopGroup(5);
    EventLoopGroup workerGroup = new NioEventLoopGroup(20);

    //ServerBootstrap ?NIO????Channel??
    //????//from  w w  w  .j ava2 s . c  om
    ServerBootstrap b = new ServerBootstrap();

    //NioServerSocketChannel?Channel?
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            //?????ChannelChannelInitializer?
            //?Channel?DiscardServerHandle??
            //ChannelChannelPipeline???????
            //?pipline??????
            .childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new PersonDecoder(), new PersonEncoder(), new PersonOutHandler());

                }
            })
            //????TCP/IP??socket?
            //tcpNoDelaykeepAlive?ChannelOptionChannelConfig??
            //ChannelOptions
            .option(ChannelOption.SO_BACKLOG, 128)
            //option()childOption()?option()??NioServerSocketChannel??
            //childOption()???ServerChannel?NioServerSocketChannel
            .childOption(ChannelOption.SO_KEEPALIVE, true);

    try {
        //?????8080?
        //?bind()(???)
        ChannelFuture f = b.bind(port).sync();
        f.channel().closeFuture().sync();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

}

From source file:com.dingwang.rpc.client.RpcClient.java

License:Open Source License

public RpcResponse send(RpcRequest request) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {// w ww. j  av a  2s.c o  m
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel channel) throws Exception {
                channel.pipeline().addLast(new RpcEncoder(RpcRequest.class)) //  RPC ???
                        .addLast(new RpcDecoder(RpcResponse.class)) //  RPC ????
                        .addLast(RpcClient.this); //  RpcClient ?? RPC 
            }
        }).option(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture future = bootstrap.connect(host, port).sync();
        future.channel().writeAndFlush(request).sync();

        synchronized (obj) {
            obj.wait(); // ?
        }

        if (response != null) {
            future.channel().closeFuture().sync();
        }
        return response;
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.dingwang.rpc.server.RpcServer.java

License:Open Source License

@Override
public void afterPropertiesSet() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//  w ww  .jav a2s  .c  o m
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel channel) throws Exception {
                        channel.pipeline().addLast(new RpcDecoder(RpcRequest.class)) //  RPC ??
                                .addLast(new RpcEncoder(RpcResponse.class)) //  RPC ???
                                //                                    .addLast(new ProviderProxy()); // ? RPC 
                                .addLast(new RpcHandler(handlerMap));
                    }
                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

        String[] array = serverAddress.split(":");
        String host = array[0];
        int port = Integer.parseInt(array[1]);

        ChannelFuture future = bootstrap.bind(host, port).sync();
        LOGGER.debug("server started on port {}", port);

        if (serviceRegistry != null) {
            serviceRegistry.register(serverAddress); // ??
        }

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

}

From source file:com.dlc.server.DLCHttpServer.java

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/*from w ww  .  j  ava 2  s.c o  m*/
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } else {
        sslCtx = null;
    }

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new DLCHttpServerInitializer(sslCtx));

        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:com.doctor.netty5.ebook.netty_the_definitive_guide.Chapter4ForTimerClient.java

License:Apache License

public void connect(String host, int port) throws InterruptedException {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//  ww  w. j  av  a  2 s .c o  m
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new LineBasedFrameDecoder(2014));
                        ch.pipeline().addLast(new StringDecoder());
                        ch.pipeline().addLast(new TimeClientHander());

                    }
                });

        ChannelFuture f = b.connect(host, port).sync();
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }

}