List of usage examples for io.netty.channel EventLoopGroup shutdownGracefully
Future<?> shutdownGracefully();
From source file:MyNettyServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {// w w w. j ava 2 s.c om 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()) .childHandler(new ServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); System.out.println("Open your web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:" + PORT + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:WorldClockClient.java
License:Apache License
public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {//from w w w.j a v a 2 s . co m Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new WorldClockClientInitializer()); // Make a new connection. Channel ch = b.connect(host, port).sync().channel(); // Get the handler instance to initiate the request. WorldClockClientHandler handler = ch.pipeline().get(WorldClockClientHandler.class); // Request and get the response. List<String> response = handler.getLocalTimes(cities); // Close the connection. ch.close(); group.shutdownGracefully(); /* // Print the response at last but not least. Iterator<String> i1 = cities.iterator(); Iterator<String> i2 = response.iterator(); while (i1.hasNext()) { System.out.format("%28s: %s%n", i1.next(), i2.next()); }*/ } finally { group.shutdownGracefully(); } }
From source file:nwses.java
License:Open Source License
/** * Main//from w w w. j ava 2 s . c o m * @param args Optional command-line parameters for server port and tag * @throws Exception */ public static void main(String[] args) throws Exception { final int port; final String tag; boolean SSL = false; if (args.length == 0) { port = 80; tag = "server1"; } else if (args.length == 1) { if (isValidPortNumber(args[0])) { port = Integer.parseInt(args[0]); tag = "server1"; } else { return; } } else { if (isValidPortNumber(args[0])) { port = Integer.parseInt(args[0]); tag = args[1]; } else { return; } } // If port 443 is specified to be used, use SSL. if (port == 443) { SSL = true; System.out.println("Websocket secure connection server initialized."); } final SslContext sslCtx; if (SSL) { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } 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.TRACE)) .childHandler(new WebSocketServerInitializer(sslCtx, tag)); System.out.println("Server: " + tag + " started at port: " + port + " \n"); Channel chnl = b.bind(port).sync().channel(); chnl.closeFuture().sync(); } catch (Exception e) { //Catch exceptions e.g. trying to open second server on same port System.err.println("Caught exception: " + e.getMessage()); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); System.out.println("Server closed.."); } }
From source file:HttpSnoopServer.java
License:Apache License
public static void main(String[] args) throws Exception { SslContext sslCtx = null;/*from w w w .j a v a 2 s .c om*/ try { File certChainFile = new File("/Users/hyecheon/netty.cst"); File keyFile = new File("/Users/hyecheon/privatekey.pem"); keyFile.exists(); sslCtx = SslContext.newServerContext(certChainFile, keyFile, "HELLO"); } catch (SSLException e) { e.printStackTrace(); System.out.println("Can not create SSL context! \n Server will be stop!"); } // Configure the server. 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 HttpSnoopServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:WorldClockServer.java
License:Apache License
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from ww w . j a v a 2s. c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new WorldClockServerInitializer()); b.bind(port).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:TelnetClient.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 va2s .com sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { sslCtx = null; } EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new TelnetClientInitializer(sslCtx)); // Start the connection attempt. Channel ch = b.connect(HOST, PORT).sync().channel(); // Read commands from the stdin. ChannelFuture lastWriteFuture = null; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // Sends the received line to the server. while (true) { lastWriteFuture = ch.writeAndFlush("req" + "\r\n"); } // If user typed the 'bye' command, wait until the server closes // the connection. // ch.closeFuture().sync(); // Wait until all messages are flushed before closing the channel. /*if (lastWriteFuture != null) { lastWriteFuture.sync(); }*/ } finally { group.shutdownGracefully(); } }
From source file:HttpUploadServer.java
License:Apache License
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* ww w. ja va 2 s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new HttpUploadServerInitializer()); Channel ch = b.bind(port).sync().channel(); System.out.println("HTTP Upload Server at port " + port + '.'); System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:Http2Server.java
License:Apache License
public static void main(String... args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {// www . ja va2 s . co m SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey(), null, null, Arrays.asList(SelectedProtocol.HTTP_2.protocolName(), SelectedProtocol.HTTP_1_1.protocolName()), 0, 0); } else { sslCtx = null; } // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(1); try { ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new Http2ServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); System.err.println("Open your HTTP/2-enabled web browser and navigate to " + (SSL ? "https" : "http") + "://127.0.0.1:" + PORT + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:HttpUploadClient.java
License:Apache License
public void run() throws Exception { String postSimple, postFile, get; if (baseUri.endsWith("/")) { postSimple = baseUri + "formpost"; postFile = baseUri + "formpostmultipart"; get = baseUri + "formget"; } else {//from w ww. j a v a2s. co m postSimple = baseUri + "/formpost"; postFile = baseUri + "/formpostmultipart"; get = baseUri + "/formget"; } URI uriSimple; try { uriSimple = new URI(postSimple); } catch (URISyntaxException e) { logger.log(Level.WARNING, "Invalid URI syntax", e); return; } String scheme = uriSimple.getScheme() == null ? "http" : uriSimple.getScheme(); String host = uriSimple.getHost() == null ? "localhost" : uriSimple.getHost(); int port = uriSimple.getPort(); if (port == -1) { if ("http".equalsIgnoreCase(scheme)) { port = 80; } else if ("https".equalsIgnoreCase(scheme)) { port = 443; } } if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) { logger.log(Level.WARNING, "Only HTTP(S) is supported."); return; } boolean ssl = "https".equalsIgnoreCase(scheme); URI uriFile; try { uriFile = new URI(postFile); } catch (URISyntaxException e) { logger.log(Level.WARNING, "Error: ", e); return; } File file = new File(filePath); if (!file.canRead()) { logger.log(Level.WARNING, "A correct path is needed"); return; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); // setup the factory: here using a mixed memory/disk based on size threshold HttpDataFactory factory = new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE); // Disk if MINSIZE exceed DiskFileUpload.deleteOnExitTemporaryFile = true; // should delete file on exit (in normal exit) DiskFileUpload.baseDirectory = null; // system temp directory DiskAttribute.deleteOnExitTemporaryFile = true; // should delete file on exit (in normal exit) DiskAttribute.baseDirectory = null; // system temp directory try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new HttpUploadClientIntializer(ssl)); // Simple Get form: no factory used (not usable) List<Entry<String, String>> headers = formGet(b, host, port, get, uriSimple); if (headers == null) { factory.cleanAllHttpDatas(); return; } // Simple Post form: factory used for big attributes List<InterfaceHttpData> bodylist = formPost(b, host, port, uriSimple, file, factory, headers); if (bodylist == null) { factory.cleanAllHttpDatas(); return; } // Multipart Post form: factory used formPostMultipart(b, host, port, uriFile, factory, headers, bodylist); } finally { // Shut down executor threads to exit. group.shutdownGracefully(); // Really clean all temporary files if they still exist factory.cleanAllHttpDatas(); } }
From source file:afred.javademo.proxy.rpc.ObjectEchoServer.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 . com 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(8080).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }