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.mapple.http.HttpHelloWorldServer.java

License:Apache License

public static void main(String[] args) throws Exception {

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from   w ww.  j a v a2  s .c o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 32);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpHelloWorldServerInitializer());

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

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

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

From source file:com.marphain.demo.communication.netty.UDPServer.java

License:Apache License

public void run() throws Exception {
    //number of threads is 8.
    EventLoopGroup group = new NioEventLoopGroup(8);

    try {//from  w  ww.j  av  a  2s .c  o  m
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, false)
                .option(ChannelOption.SO_RCVBUF, REMSIZE * 200).handler(new UDPServerHandler());

        b.bind(port).sync().channel().closeFuture().await();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.metasoft.empire.net.websocket.WebSocketServer.java

License:Apache License

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

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

        Channel ch = b.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.mikesilversides.mod1.ServerTest.EchoClient.java

License:Apache License

public static void init() throws Exception { // convert main to init
    // Configure SSL.git
    //        final SslContext sslCtx;
    if (SSL) {// w w  w . j  a v a  2s  .  c  o m
        //            sslCtx = SslContextBuilder.forClient()
        //                .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    } else {
        //            sslCtx = null;
    }

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        //                     if (sslCtx != null) {
                        //                         p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                        //                     }
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoClientHandler());
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync(); //Mike: I think this is a blocking wait
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}

From source file:com.mikesilversides.mod1.ServerTest.StubClient.java

License:Apache License

public void run() {
    System.out.println("StubClient.run() called!");
    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*from   w w w  . j av a 2  s.  co  m*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();

                        p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new StubClientHandler(stubPlayer));
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        // We've been interrupted: no more messages.
        System.out.println("InterruptedException caught, do return");
        return;
    } finally {
        // Shut down the event loop to terminate all threads.
        System.out.println("doing group.shutdownGracefully()");
        group.shutdownGracefully();
    }
}

From source file:com.mnxfst.stream.server.StreamAnalyzerServer.java

License:Apache License

public void run(final String configurationFilename, final int port) throws Exception {

    ObjectMapper mapper = new ObjectMapper();
    StreamAnalyzerConfiguration streamAnalyzerConfiguration = mapper.readValue(new File(configurationFilename),
            StreamAnalyzerConfiguration.class);

    // set up  the actor runtime environment
    this.rootActorSystem = ActorSystem.create("streamanalyzer");

    this.componentRegistryRef = componentRegistryInitialization();
    pipelineInitialization(streamAnalyzerConfiguration.getPipelines());
    dispatcherInitialization(streamAnalyzerConfiguration.getDispatchers(), componentRegistryRef);
    listenerInitialization(streamAnalyzerConfiguration.getListeners(), componentRegistryRef);

    EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from   w w w  .ja v  a2  s . co m
        ServerBootstrap b = new ServerBootstrap(); // (2)
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // (3)
                .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StreamAnalyzerStatsHandler());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128) // (5)
                .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)

        // Bind and start to accept incoming connections.
        ChannelFuture f = b.bind(port).sync(); // (7)

        // Wait until the server socket is closed.
        // In this example, this does not happen, but you can do that to gracefully
        // shut down your server.
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

}

From source file:com.mongodb.async.client.MongoClients.java

License:Apache License

private static Closeable getEventLoopGroupCloser(final EventLoopGroup eventLoopGroup) {
    if (eventLoopGroup == null) {
        return null;
    } else {/*from  w  w w. j a  v  a  2s .co  m*/
        return new Closeable() {
            @Override
            public void close() throws IOException {
                eventLoopGroup.shutdownGracefully().awaitUninterruptibly();
            }
        };
    }
}

From source file:com.mongodb.async.client.NettyMongoClients.java

License:Apache License

@SuppressWarnings("deprecation")
static MongoClient create(final MongoClientSettings settings,
        @Nullable final MongoDriverInformation mongoDriverInformation) {
    final EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    StreamFactory streamFactory = new NettyStreamFactory(settings.getSocketSettings(),
            settings.getSslSettings(), eventLoopGroup);
    StreamFactory heartbeatStreamFactory = new NettyStreamFactory(settings.getHeartbeatSocketSettings(),
            settings.getSslSettings(), eventLoopGroup);
    return MongoClients.createMongoClient(settings, mongoDriverInformation, streamFactory,
            heartbeatStreamFactory, new Closeable() {
                @Override//from   w  w  w . ja v a  2 s. c  o m
                public void close() {
                    eventLoopGroup.shutdownGracefully().awaitUninterruptibly();
                }
            });
}

From source file:com.mpcc.springmvc.configuration.Start.java

void shutdownWorkers(EventLoopGroup boss, EventLoopGroup worker) {
    Future fb = boss.shutdownGracefully();
    Future fw = worker.shutdownGracefully();
    try {//from w  ww  .j a  v  a  2  s . c  o m
        fb.await();
        fw.await();
    } catch (InterruptedException ignore) {
    }
}

From source file:com.my.netty.object.ObjectEchoClient.java

License:Apache License

public void run() throws Exception {
    Bootstrap b = new Bootstrap();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/* w w w  .jav  a 2s.c  o  m*/
        b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ObjectEncoder())
                                .addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)))
                                .addLast(new ObjectEchoClientHandler(firstMessageSize));
                    }
                });

        // Start the connection attempt.
        System.out.println("client prepare connect");
        ChannelFuture f1 = b.connect(host, port).sync();
        ChannelFuture f2 = b.connect(host, port).sync();

        f1.channel().closeFuture().sync();
        f2.channel().closeFuture().sync();
    } finally {
        System.out.println("client do finally");
        workerGroup.shutdownGracefully();
        System.out.println("client closing");
    }
}