List of usage examples for io.netty.channel EventLoopGroup shutdownGracefully
Future<?> shutdownGracefully();
From source file:com.qq.servlet.demo.netty.sample.thrift.service.ThriftProtocolServer.java
License:Apache License
private void startCloseCmdThread(final EventLoopGroup bossGroup, final EventLoopGroup workerGroup) { new Thread() { @Override//from w ww . ja v a 2 s. c o m public void run() { try { 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(); System.out.println("?"); } catch (Exception e) { e.printStackTrace(); } } }.start(); }
From source file:com.qq.servlet.demo.netty.telnet.client.TelnetClient.java
License:Apache License
public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(2); try {/*from w ww . ja va 2s. c o m*/ final Bootstrap b = new Bootstrap(); b.group(group); b.channel(NioSocketChannel.class); b.handler(new ClientInitializer()); for (int t = 0; t < 10; t++) { for (int i = 0; i < 50; i++) { new Thread() { int j = 0; public void run() { try { test(b, j++); } catch (InterruptedException e) { e.printStackTrace(); } }; }.start(); } } System.in.read(); } finally { group.shutdownGracefully(); } }
From source file:com.qq.servlet.demo.netty.telnet.TelnetServer.java
License:Apache License
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(5); EventLoopGroup workerGroup = new NioEventLoopGroup(500); try {//from w ww . j av a2 s .c o 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.thrift.netty.client.NettyThriftClient.java
License:Apache License
public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(2); try {/*from w ww . j a v a 2s . c o m*/ final Bootstrap b = new Bootstrap(); b.group(group); b.channel(NioSocketChannel.class); b.handler(new NettyThriftClientInitializer()); // Start the connection attempt. ChannelFuture future = b.connect(host, port); ChannelFuture sync = future.sync(); Channel ch = sync.channel(); AttributeKey<String> key = AttributeKey.valueOf("ASYNC_CONTEXT"); Attribute<String> attr = ch.attr(key); String andSet = attr.getAndSet(Thread.currentThread().getName()); TMemoryBuffer transportr = new TMemoryBuffer(1024); TProtocol protocol = new TBinaryProtocol(transportr); TMultiplexedProtocol multiProtocol3 = new TMultiplexedProtocol(protocol, IdGenService.class.getSimpleName()); IdGenService.Client aoClient = new IdGenService.Client(multiProtocol3); IdGenReq req = new IdGenReq(); req.setTableName("t_lottery_append_task-----------"); req.setSource(andSet); aoClient.send_idGen(req); int length = transportr.length(); byte[] buf = new byte[length]; transportr.read(buf, 0, length); System.out.println(Arrays.toString(buf)); ChannelFuture lastWriteFuture = ch.writeAndFlush(buf); // Wait until all messages are flushed before closing the channel. if (lastWriteFuture != null) { lastWriteFuture.sync(); } System.in.read(); } finally { group.shutdownGracefully(); } }
From source file:com.rain.websocketclient.WebSocketClient.java
License:Apache License
public static void main(String[] args) throws Exception { URI uri = new URI(URL); String scheme = uri.getScheme() == null ? "http" : uri.getScheme(); final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost(); final int port; if (uri.getPort() == -1) { if ("http".equalsIgnoreCase(scheme)) { port = 80;//from w w w . j a va2s . c om } else if ("https".equalsIgnoreCase(scheme)) { port = 443; } else { port = -1; } } else { port = uri.getPort(); } if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) { System.err.println("Only WS(S) is supported."); return; } final boolean ssl = "wss".equalsIgnoreCase(scheme); final SslContext sslCtx; if (ssl) { sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } EventLoopGroup group = new NioEventLoopGroup(); try { // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00. // If you change it to V00, ping is not supported and remember to change // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline. final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory .newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders())); Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), host, port)); } p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), handler); } }); Channel ch = b.connect(uri.getHost(), port).sync().channel(); handler.handshakeFuture().sync(); BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); while (true) { String msg = console.readLine(); if (msg == null) { break; } else if ("bye".equals(msg.toLowerCase())) { ch.writeAndFlush(new CloseWebSocketFrame()); ch.closeFuture().sync(); break; } else if ("ping".equals(msg.toLowerCase())) { WebSocketFrame frame = new PingWebSocketFrame( Unpooled.wrappedBuffer(new byte[] { 8, 1, 8, 1 })); ch.writeAndFlush(frame); } else { WebSocketFrame frame = new TextWebSocketFrame(msg); ch.writeAndFlush(frame); } } } finally { group.shutdownGracefully(); } }
From source file:com.robert.NettyProject.EchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL.git final SslContext sslCtx; if (SSL) {//from w ww . j av a 2 s. 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("encode", new StringEncoder()); p.addLast("decode", new StringDecoder()); // p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(HOST, PORT).sync(); Channel channel = f.channel(); System.out.println("" + System.nanoTime()); // for(int i=0;i<10000;i++){ channel.writeAndFlush("hello"); // } // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.robert.NettyProject.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*w w w .j av a2 s . c o m*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } p.addLast("encode", new StringEncoder()); p.addLast("decode", new StringDecoder()); // p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(PORT).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.rr.echoserver.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { System.out.println("Writing on separate threads: " + EchoServer.THREADED); // Configure SSL. final SslContext sslCtx; if (SSL) {//from w ww. j a va 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.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(PORT).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.sample.netty.socket.client.Client.java
public static void main(String[] args) throws Exception { String host = "localhost"; int port = 8080; EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* w ww . j av a2 s . c o m*/ Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new MessageDecoder(), new ClientHandlerInbound()); ch.pipeline().addLast(new MessageEncoder(), new ClientHandlerOutbound()); } }); ChannelFuture f = b.connect(host, port).sync(); f.channel().writeAndFlush( String.format("Usurio = '%s'", System.getProperties().getProperty("user.name"))); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); } }
From source file:com.sample.netty.socket.server.Server.java
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* w w w .j a va 2s .c o m*/ 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 MessageDecoder(), new ServerHandlerInbound()); ch.pipeline().addLast(new MessageEncoder(), new ServerHandlerOutbound()); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }