List of usage examples for io.netty.channel EventLoopGroup shutdownGracefully
Future<?> shutdownGracefully();
From source file:com.xing.netty.client.DiscardClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from w w w.j a v a 2 s . c om*/ sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE); } else { sslCtx = null; } EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // if (sslCtx != null) { // p.addLast(sslCtx.newHandler(ch.alloc(), HOST, // PORT)); // } // p.addLast(new DiscardClientHandler()); ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes()); p.addLast(new DelimiterBasedFrameDecoder(1024, delimiter)); // p.addLast(new LineBasedFrameDecoder(1024)); p.addLast(new StringDecoder()); p.addLast(new TimeClientHandler()); } }); // Make the connection attempt. ChannelFuture f = b.connect(HOST, PORT).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } catch (Exception e) { System.out.println(e); } finally { group.shutdownGracefully(); } }
From source file:com.xsecret.chat.client.ChatClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE) .build();// w w w .j a v a 2s . c o m EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChatClientInitializer(sslCtx)); // Start the connection attempt. Channel ch = b.connect(HOST, PORT).sync().channel(); // Read commands from the stdin. ChannelFuture lastWriteFuture = null; DataManager dataManager = new DataManager("shengxinlei", "pangff"); dataManager.start(); for (;;) { System.out.println("================"); MsgProtocol.MsgContent baseMsg = msgLinkedBlockingQueue.take(); if (baseMsg == null) { break; } if (MsgProtocol.MsgType.LOGIN.equals(baseMsg.getMsgType())) { System.out.println("=======login========="); // If user typed the 'bye' command, wait until the server closes // the connection. } else if (MsgProtocol.MsgType.LOGOUT.equals(baseMsg.getMsgType())) { System.out.println("=======logout========="); ch.closeFuture().sync(); dataManager.stopThread(); break; } else { // Sends the received line to the server. lastWriteFuture = ch.writeAndFlush(baseMsg); System.out.println(baseMsg.getMsgType() + " to " + baseMsg.getToClientId() + ": " + baseMsg.getToClientMsg()); } } // Wait until all messages are flushed before closing the channel. if (lastWriteFuture != null) { lastWriteFuture.sync(); } } finally { // The connection is closed automatically on shutdown. group.shutdownGracefully(); } }
From source file:com.xxx.netty.run.SecureChatServer.java
License:Apache License
@SuppressWarnings("resource") public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:root-context.xml");// loading //jedis = context.getBean(RedisInitBean.class).getSingletonInstance(); SecureChatServer chatServer = context.getBean(SecureChatServer.class); // SelfSignedCertificate???? SelfSignedCertificate ssc = new SelfSignedCertificate(); // ???/*www . j a va 2s .c o m*/ SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap serverBootstrap = new ServerBootstrap();// ????? serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new SecureChatServerInitializer(sslCtx)); if (null != args && args.length > 1 && args[0].matches("\\d")) { chatServer.PORT = Integer.parseInt(args[0]); } LOGGER.debug("SSL TCP server started on port:{}", chatServer.PORT); serverBootstrap.bind(chatServer.PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); context = null; } }
From source file:com.xxx.util.io.server.netty.EchoServer.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 ww w.j a va 2s . c o m 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(); //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.xx_dev.apn.proxy.test.TestProxyWithNetty.java
License:Apache License
public void test(String host, String path) { EventLoopGroup group = new NioEventLoopGroup(); try {//from w w w.j av a 2s . co m Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new TestHttpClientChannelInitializer()); // Make the connection attempt. Channel ch = b.connect("127.0.0.1", ApnProxyConfig.getConfig().getPort()).sync().channel(); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://" + host + path); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); //request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); // Send the HTTP request. ch.writeAndFlush(request); // Wait for the server to close the connection. ch.closeFuture().sync(); } catch (InterruptedException e) { logger.error(e.getMessage(), e); } finally { // Shut down executor threads to exit. group.shutdownGracefully(); } }
From source file:com.xx_dev.apn.socks.local.PortForwardProxy.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// w w w. j av a 2 s.c o m ServerBootstrap b = new ServerBootstrap(); ChannelFuture bindFuture = b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new PortForwardProxyFrontendInitializer(LocalConfig.ins().getRemoteHost(), LocalConfig.ins().getRemotePort())) .childOption(ChannelOption.AUTO_READ, false).bind(LocalConfig.ins().getLocalPort()); bindFuture.sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.xx_dev.apn.socks.remote.SocksServer.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w. ja va 2 s . c o m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler("NET_LOGGER", LogLevel.DEBUG)) .childHandler(new SocksServerInitializer()); b.bind(RemoteConfig.ins().getListenPort()).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.xx_dev.apn.socks.test.SocksClient.java
License:Apache License
public static void main(String[] args) throws Throwable { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) .option(ChannelOption.SO_KEEPALIVE, true).handler(new SocksClientInitializer()); // Make the connection attempt. b.connect("112.124.38.73", 8888).sync().channel().closeFuture().sync(); group.shutdownGracefully(); }
From source file:com.xx_dev.port_forwared.HexDumpProxy.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); List<HexDumpForwardRule> ruleList = new ArrayList<HexDumpForwardRule>(); HexDumpForwardRule r3 = new HexDumpForwardRule(9000, "imap.gmail.com", 993, true); ruleList.add(r3);/*from w ww . j a va 2s . c o m*/ HexDumpForwardRule r4 = new HexDumpForwardRule(9001, "smtp.gmail.com", 465, true); ruleList.add(r4); List<ChannelFuture> bindFutureList = new ArrayList<ChannelFuture>(); try { for (HexDumpForwardRule r : ruleList) { ServerBootstrap b = new ServerBootstrap(); ChannelFuture bindFuture = b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler("BYTE_LOGGER", LogLevel.DEBUG)) .childHandler( new HexDumpProxyInitializer(r.getRemoteHost(), r.getRemotePort(), r.isRemoteSsl())) .childOption(ChannelOption.AUTO_READ, false).bind(r.getLocalPort()); bindFutureList.add(bindFuture); } for (ChannelFuture f : bindFutureList) { f.sync().channel().closeFuture().sync(); } } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.xx_dev.speed_test.SpeedTestClient.java
License:Apache License
public static void main(String[] args) { EventLoopGroup group = new NioEventLoopGroup(); try {/*from w ww. jav a2s. c o m*/ URL url = new URL(args[0]); boolean isSSL = false; String host = url.getHost(); int port = 80; if (StringUtils.equals(url.getProtocol(), "https")) { port = 443; isSSL = true; } if (url.getPort() > 0) { port = url.getPort(); } String path = url.getPath(); if (StringUtils.isNotBlank(url.getQuery())) { path += "?" + url.getQuery(); } PrintWriter resultPrintWriter = null; if (StringUtils.isNotBlank(args[1])) { String resultFile = args[1]; resultPrintWriter = new PrintWriter( new OutputStreamWriter(new FileOutputStream(resultFile, false), "UTF-8")); } Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .handler(new SpeedTestHttpClientChannelInitializer(isSSL, resultPrintWriter)); // Make the connection attempt. Channel ch = b.connect(host, port).sync().channel(); // Prepare the HTTP request. HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path); request.headers().set(HttpHeaders.Names.HOST, host); request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); //request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); // Send the HTTP request. ch.writeAndFlush(request); // Wait for the server to close the connection. ch.closeFuture().sync(); if (resultPrintWriter != null) { resultPrintWriter.close(); } } catch (InterruptedException e) { logger.error(e.getMessage(), e); } catch (FileNotFoundException e) { logger.error(e.getMessage(), e); } catch (UnsupportedEncodingException e) { logger.error(e.getMessage(), e); } catch (MalformedURLException e) { logger.error(e.getMessage(), e); } finally { group.shutdownGracefully(); } }