List of usage examples for io.netty.channel EventLoopGroup shutdownGracefully
Future<?> shutdownGracefully();
From source file:com.zhucode.longio.example.service.TestClient.java
License:Open Source License
public static void main(String[] args) throws Exception { // 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(); ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(65536, 0, 2, 0, 2)); ch.pipeline().addLast("encoder", new LengthFieldPrepender(2, false)); p.addLast(new EchoClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(HOST, PORT).sync(); // 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.zhuika.discard.DiscardClient.java
License:Apache License
private static void client(final String[] args, final byte[] byteMsg) throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/*from w w w . ja v a 2 s. co m*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("framer", new DelimiterBasedFrameDecoder(8192, false, Delimiters.lineDelimiter())); p.addLast(new ByteToMessageDec()); p.addLast(new MessageToByteEnc()); if (byteMsg != null && byteMsg.length > 0) { p.addLast(new DiscardClientHandler(byteMsg)); } else { p.addLast(new DiscardClientHandler(args[2])); } } }); final ChannelFuture f = b.connect(args[0], Integer.valueOf(args[1])).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:com.zhuika.server.DiscardServer.java
License:Apache License
protected static void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w . j a va 2 s. c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) /* * AdaptiveRecvByteBufAllocator * Channel * * 2 * AdaptiveRecvByteBufAllocator */ //.option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT) //.handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); p.addLast(new IdleStateHandler(0, 0, 150, TimeUnit.SECONDS)); p.addLast("framer", new DelimiterBasedFrameDecoder(8192, false, Delimiters.lineDelimiter())); p.addLast(new ByteToMessageDec()); p.addLast(new MessageToByteEnc()); p.addLast(ServiceHandlerFactory.getDiscardServerHandler()); } }); Logger logger = Logger.getLogger(DiscardServer.class); // Server Config config = XMLReader.loadconfig(); String socketip = config.socketip; String socketport = config.socketport; // Bind and start to accept incoming connections. // ChannelFuture f = b.bind(PORT).sync(); ChannelFuture f = b.bind(socketip, Integer.parseInt(socketport)).sync(); logger.info("TCP server started successfully"); // 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(); } }