List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup
public NioEventLoopGroup(ThreadFactory threadFactory)
From source file:MyNettyServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from w w w . j ava 2 s . co 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()) .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:nwses.java
License:Open Source License
/** * Main//from ww w . ja v a2 s . co 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;/* ww w. ja va 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:NNIOEngine.java
License:Apache License
@Override public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception { int ioWorkerCount = Runtime.getRuntime().availableProcessors() * 2; NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(ioWorkerCount); try {/*from w w w . j av a2 s . c om*/ final Netty4ClientHttpRequestFactory netty4ClientHttpRequestFactory = new Netty4ClientHttpRequestFactory( eventLoopGroup); netty4ClientHttpRequestFactory.setConnectTimeout(2000); netty4ClientHttpRequestFactory.setReadTimeout(2000); /* SslContext sslContext = SslContextBuilder .forClient() .sslProvider(SslProvider.JDK) .build() ; */ if (requestOptions.getUrl().toLowerCase().startsWith("https://")) { SslContext sslContext = new DefaultClientSslContext(); netty4ClientHttpRequestFactory.setSslContext(sslContext); } netty4ClientHttpRequestFactory.afterPropertiesSet(); ResponseEntity<String> stringResponseEntity = null; for (int i = 0; i < requestOptions.getCount(); i++) { final HttpHeaders headers = new HttpHeaders(); for (Map.Entry<String, String> e : requestOptions.getHeaderMap().entrySet()) { headers.put(e.getKey(), Collections.singletonList(e.getValue())); } final HttpEntity<Void> requestEntity = new HttpEntity<>(headers); AsyncRestTemplate template = new AsyncRestTemplate(netty4ClientHttpRequestFactory); final ListenableFuture<ResponseEntity<String>> exchange = template.exchange(requestOptions.getUrl(), HttpMethod.GET, requestEntity, String.class); stringResponseEntity = exchange.get(); System.out.println(stringResponseEntity.getBody()); } return stringResponseEntity; } finally { eventLoopGroup.shutdownGracefully(100, 500, TimeUnit.MILLISECONDS); } }
From source file:NettyHttpListner.java
License:Apache License
public void start() { System.out.println("Starting the server..."); System.out.println("Starting Inbound Http Listner on Port " + this.port); // Configure SSL. SslContext sslCtx = null;/* w ww . java2 s . c o m*/ if (SSL) { try { SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } catch (CertificateException ex) { Logger.getLogger(NettyHttpListner.class.getName()).log(Level.SEVERE, null, ex); } catch (SSLException ex) { Logger.getLogger(NettyHttpListner.class.getName()).log(Level.SEVERE, null, ex); } } commonEventLoopGroup = new NioEventLoopGroup(bossGroupSize); // bossGroup = new NioEventLoopGroup(bossGroupSize); // workerGroup = new NioEventLoopGroup(workerGroupSize); try { ServerBootstrap b = new ServerBootstrap(); // b.commonEventLoopGroup(bossGroup, workerGroup) b.group(commonEventLoopGroup).channel(NioServerSocketChannel.class) .childHandler( new NettyHttpTransportHandlerInitializer(HOST, HOST_PORT, maxConnectionsQueued, sslCtx)) .childOption(ChannelOption.AUTO_READ, false); b.option(ChannelOption.TCP_NODELAY, true); b.childOption(ChannelOption.TCP_NODELAY, true); b.option(ChannelOption.SO_BACKLOG, maxConnectionsQueued); b.option(ChannelOption.SO_KEEPALIVE, true); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 15000); b.option(ChannelOption.SO_SNDBUF, 1048576); b.option(ChannelOption.SO_RCVBUF, 1048576); b.childOption(ChannelOption.SO_RCVBUF, 1048576); b.childOption(ChannelOption.SO_SNDBUF, 1048576); b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); Channel ch = null; try { ch = b.bind(port).sync().channel(); ch.closeFuture().sync(); System.out.println("Inbound Listner Started"); } catch (InterruptedException e) { System.out.println("Exception caught"); } } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:NettyServer.java
License:Open Source License
void start() throws Exception { parentGroup = new NioEventLoopGroup(1); childGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap().group(parentGroup, childGroup) .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { @Override//from www . j a v a2 s.c o m protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("lineFrame", new LineBasedFrameDecoder(256)); pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); pipeline.addLast("handler", new ChannelInboundHandlerAdapter() { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { Channel channel = ctx.channel(); channelActiveHandler.accept(channel); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { messageConsumer.accept(msg); } }); } }); channel = bootstrap.bind(port).channel(); System.out.println("NettyServer started"); }
From source file:HttpRouterServer.java
License:Apache License
public static void main(String[] args) throws Exception { Router<String> router = new Router<String>().GET(PUBLIC_DIR + ":id", "public").GET("/", "index") .GET(PUBLIC_DIR, "index") // .GET("/image", "base64") // .GET("/img", "image") // .GET("/", "Index page") // .GET("/articles/:id", "Article show page") .notFound("404 Not Found"); System.out.println(router);// w w w.ja v a 2 s. c o m NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).childOption(ChannelOption.TCP_NODELAY, java.lang.Boolean.TRUE) .childOption(ChannelOption.SO_KEEPALIVE, java.lang.Boolean.TRUE) .channel(NioServerSocketChannel.class).childHandler(new HttpRouterServerInitializer(router)); Channel ch = b.bind(PORT).sync().channel(); System.out.println("Server started: http://127.0.0.1:" + 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) {//from w ww . j ava 2s.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:Http2Client.java
License:Apache License
public Http2Client(final String host, final int port, final boolean ssl) throws Exception { this.host = host; this.port = port; this.ssl = ssl; // Configure SSL. if (ssl) {//w w w .ja v a 2 s . com this.sslCtx = SslContext.newClientContext(null, InsecureTrustManagerFactory.INSTANCE, null, Arrays.asList(SelectedProtocol.HTTP_2.protocolName(), SelectedProtocol.HTTP_1_1.protocolName()), 0, 0); } else { this.sslCtx = null; } // XXX (dano): Http2Connection does not seem to be thread safe, use one thread only this.workerGroup = new NioEventLoopGroup(1); Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx); // Configure the client. Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.remoteAddress(host, port); b.handler(initializer); // Start the client. this.channel = b.connect().syncUninterruptibly().channel(); System.out.println("Connected to [" + host + ':' + port + ']'); // Wait for the HTTP/2 upgrade to occur. this.connectionHandler = initializer.connectionHandler(); connectionHandler.awaitInitialization(); }
From source file:adalightserver.http.HttpServer.java
License:Apache License
public HttpServer(IController ledController) { this.bossGroup = new NioEventLoopGroup(1); this.clientGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors()); this.scheduler = Schedulers.from(bossGroup); this.connections = new DefaultChannelGroup(bossGroup.next()); this.wsConnections = new DefaultChannelGroup(bossGroup.next()); this.ledController = ledController; }