List of usage examples for io.netty.channel EventLoopGroup shutdownGracefully
Future<?> shutdownGracefully();
From source file:com.yahoo.ads.pb.network.netty.NettyPistachioClient.java
License:Open Source License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {//from w ww . j a va 2 s . com sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } System.out.println("start test"); EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new NettyPistachioClientInitializer(sslCtx)); // Make a new connection. Channel ch = b.connect(HOST, PORT).sync().channel(); // Get the handler instance to initiate the request. NettyPistachioClientHandler handler = ch.pipeline().get(NettyPistachioClientHandler.class); // Request and get the response. Response res = handler.lookup(12345L); // Close the connection. ch.close(); // Print the response at last but not least. System.out.println("response: " + res.getData()); } finally { group.shutdownGracefully(); } }
From source file:com.yahoo.ads.pb.network.netty.NettyPistachioServer.java
License:Open Source License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {//from ww w .j a v a 2 s . c o m SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } 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 NettyPistachioServerInitializer(sslCtx)); b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.yao.netty.objectecho.ObjectEchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/* ww w . j a va 2s. c o m*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new ObjectEchoClientHandler()); } }); // Start the connection attempt. b.connect(HOST, PORT).sync().channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:com.yao.netty.objectecho.ObjectEchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* www . j a v a2 s . co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new ObjectEchoServerHandler()); } }); // Bind and start to accept incoming connections. b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.yeetor.console.Console.java
License:Open Source License
/** * Console??//from w w w .j a v a 2s . c om * @param port */ public void listenOnTCP(int port) { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new Adapter()); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.zextras.modules.chat.server.xmpp.netty.ChatXmppService.java
License:Open Source License
public void run() { EventLoopGroup acceptorGroup = new NioEventLoopGroup(4); EventLoopGroup channelWorkerGroup = new NioEventLoopGroup(8); final boolean useLegacySSLPort = mChatProperties.isChatXmppSslPortEnabled(); String logListening = "XMPP listening on ports 5222"; if (useLegacySSLPort) { logListening += ", 5223"; }//from w w w . j a va 2 s . co m ChatLog.log.info(logListening); try { SSLContext zimbraSSLContext = mZimbraSSLContextProvider.get(); ServerBootstrap bootstrapTLS = buildBoostrap(acceptorGroup, channelWorkerGroup, zimbraSSLContext, false); ServerBootstrap bootstrapOldSSL = buildBoostrap(acceptorGroup, channelWorkerGroup, zimbraSSLContext, true); ChannelFuture futureTLS = bootstrapTLS .bind(mChatProperties.getChatXmppPort(mProvisioning.getLocalServer().getName())).sync(); //addListener ChannelFuture futureOldSSL = null; if (useLegacySSLPort) { futureOldSSL = bootstrapOldSSL .bind(mChatProperties.getChatXmppSslPort(mProvisioning.getLocalServer().getName())).sync(); } ChatLog.log.info("XMPP started"); mInitializationPromise.setSuccess(true); mLock.lock(); try { while (!mStopped) { try { mCondition.await(); } catch (InterruptedException ex) { } } mStopped = false; } finally { mLock.unlock(); } ChatLog.log.info("XMPP shutting down"); if (useLegacySSLPort) { futureOldSSL.channel().close().sync(); } futureTLS.channel().close().sync(); } catch (Throwable ex) { ChatLog.log.warn(Utils.exceptionToString(ex)); mInitializationPromise.setFailure(ex); } finally { acceptorGroup.shutdownGracefully(); channelWorkerGroup.shutdownGracefully(); } }
From source file:com.zextras.modules.chat.services.LocalXmppService.java
License:Open Source License
@Override public void run() { ChatLog.log.info("Listening on port " + DEFAULT_LOCAL_XMPP_PORT); EventLoopGroup acceptorGroup = new NioEventLoopGroup(4); EventLoopGroup channelWorkerGroup = new NioEventLoopGroup(8); Channel channel;/*w w w .j av a 2 s.co m*/ try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(acceptorGroup, channelWorkerGroup); bootstrap.channel(NioServerSocketChannel.class); ChannelHandler handler = new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { try { SSLEngine sslEngine = mZimbraSSLContextProvider.get().createSSLEngine(); sslEngine.setUseClientMode(false); SslHandler sslHandler = new SslHandler(sslEngine); ch.pipeline().addFirst("ssl", sslHandler); ch.pipeline().addLast(null, "SubTagTokenizer", new XmlSubTagTokenizer()); ch.pipeline().addLast(null, "XmlTagTokenizer", new XmlTagTokenizer()); ch.pipeline().addAfter("XmlTagTokenizer", "StanzaProcessor", new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { mLocalXmppReceiver.processStanza((String) msg); } }); } catch (Throwable t) { ChatLog.log.warn("Unable to initializer XMPP connection: " + Utils.exceptionToString(t)); ch.close(); } } }; ChannelFuture channelFuture = bootstrap.childHandler(handler).option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 0).bind(DEFAULT_LOCAL_XMPP_PORT).sync(); if (!channelFuture.isSuccess()) { throw channelFuture.cause(); } channel = channelFuture.channel(); mInitializationPromise.setSuccess(null); } catch (Throwable e) { mInitializationPromise.setFailure(e); return; } mLock.lock(); try { while (!mStopRequested) { try { mWaitStopRequest.await(); } catch (InterruptedException ignored) { } } channel.close().sync(); acceptorGroup.shutdownGracefully().sync(); channelWorkerGroup.shutdownGracefully().sync(); } catch (InterruptedException ignored) { } finally { mLock.unlock(); } }
From source file:com.zhaopeng.timeserver.netty.basic.TimeClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO/*from ww w. j a v a2 s .c o m*/ EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChildChannelHandler()); // ?? ChannelFuture f = b.connect(host, port).sync(); System.out.println("123"); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }
From source file:com.zhaopeng.timeserver.netty.delimiter.EchoClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO//from w w w .j a v a2s . com 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 { ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new EchoClientHandler()); } }); // ?? ChannelFuture f = b.connect(host, port).sync(); System.out.println(f); // f.channel().closeFuture().sync(); } finally { // NIO group.shutdownGracefully(); } }
From source file:com.zhaopeng.timeserver.protocol.udp.ChineseProverbServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/* w w w . j a v a2 s. co m*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true) .handler(new ChineseProverbServerHandler()); b.bind(port).sync().channel().closeFuture().await(); } finally { group.shutdownGracefully(); } }