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(long quietPeriod, long timeout, TimeUnit unit) 

Source Link

Usage

From source file:NNIOEngine.java

License:Apache License

@Override
public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception {
    int ioWorkerCount = Runtime.getRuntime().availableProcessors() * 2;
    NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(ioWorkerCount);

    try {/*from w w  w  . ja v a  2 s  .com*/
        final Netty4ClientHttpRequestFactory netty4ClientHttpRequestFactory = new Netty4ClientHttpRequestFactory(
                eventLoopGroup);
        netty4ClientHttpRequestFactory.setConnectTimeout(2000);
        netty4ClientHttpRequestFactory.setReadTimeout(2000);
        /*
                    SslContext sslContext = SslContextBuilder
        .forClient()
        .sslProvider(SslProvider.JDK)
        .build()
                    ;
        */
        if (requestOptions.getUrl().toLowerCase().startsWith("https://")) {
            SslContext sslContext = new DefaultClientSslContext();
            netty4ClientHttpRequestFactory.setSslContext(sslContext);
        }
        netty4ClientHttpRequestFactory.afterPropertiesSet();

        ResponseEntity<String> stringResponseEntity = null;
        for (int i = 0; i < requestOptions.getCount(); i++) {
            final HttpHeaders headers = new HttpHeaders();
            for (Map.Entry<String, String> e : requestOptions.getHeaderMap().entrySet()) {
                headers.put(e.getKey(), Collections.singletonList(e.getValue()));
            }

            final HttpEntity<Void> requestEntity = new HttpEntity<>(headers);

            AsyncRestTemplate template = new AsyncRestTemplate(netty4ClientHttpRequestFactory);
            final ListenableFuture<ResponseEntity<String>> exchange = template.exchange(requestOptions.getUrl(),
                    HttpMethod.GET, requestEntity, String.class);
            stringResponseEntity = exchange.get();
            System.out.println(stringResponseEntity.getBody());
        }
        return stringResponseEntity;
    } finally {
        eventLoopGroup.shutdownGracefully(100, 500, TimeUnit.MILLISECONDS);
    }
}

From source file:io.viewserver.network.netty.NettyNetworkAdapter.java

License:Apache License

@Override
public void reset() {
    //        Set<Channel> parents = new HashSet<>();
    //        for (Channel activeChannel : activeChannels) {
    //            try {
    ////                if (activeChannel.parent() != null) {
    ////                    parents.add(activeChannel.parent());
    ////                }
    //                activeChannel.close().sync();
    //            } catch (InterruptedException e) {
    //                e.printStackTrace();
    //            }
    //        }/*from  w w w  .ja v a 2 s.  c  o m*/

    //        for (Channel parent : parents) {
    //            try {
    //                parent.close().sync();
    //            } catch (InterruptedException e) {
    //                e.printStackTrace();
    //            }
    //        }

    try {
        NioEventLoopGroup handlers = this.handlers;
        this.handlers = null;
        if (handlers != null) {
            log.debug("Shutting down Netty handlers");
            handlers.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS).sync();
        }
        NioEventLoopGroup parentGroup = this.parentGroup;
        this.parentGroup = null;
        if (parentGroup != null) {
            log.debug("Shutting down Netty parent group");
            parentGroup.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS).sync();
        }
        NioEventLoopGroup clientWorkerGroup = this.clientWorkerGroup;
        this.clientWorkerGroup = null;
        if (clientWorkerGroup != null) {
            log.debug("Shutting down Netty client group");
            clientWorkerGroup.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS).sync();
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    log.debug("Shut down Netty");

    listeners.clear();
}