List of usage examples for io.netty.bootstrap ServerBootstrap group
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
From source file:MyNettyServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {// w ww . ja v a2 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:nwses.java
License:Open Source License
/** * Main/* w w w . j a va 2 s . c om*/ * @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;/*w w w . j av a2s . co m*/ 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:HttpUploadServer.java
License:Apache License
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// w ww .j a v a2 s. c o m 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: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);//from w w w. jav a 2s. 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 ww w . ja v a 2 s .c o 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:adalightserver.http.HttpServer.java
License:Apache License
public void start() { ServerBootstrap bootStrap = new ServerBootstrap(); bootStrap.group(bossGroup, clientGroup).channel(NioServerSocketChannel.class) .childHandler(new ServerInitializer(null)); try {// www . j a v a 2 s . c o m channel = bootStrap.bind(8081).sync().channel(); } catch (InterruptedException e) { } // Listen to the state and send it to all connected clients stateSub = ledController.stateChanged().observeOn(scheduler).subscribe(state -> { lastState = state; wsConnections.writeAndFlush(makeWebSocketEventFrame("stateChanged", state)); }); }
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 a v a 2 s. c om 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(); } }
From source file:alluxio.worker.netty.NettyDataServer.java
License:Apache License
/** * Creates a default {@link io.netty.bootstrap.ServerBootstrap} where the channel and groups are * preset.// w w w . j a v a 2 s . c o m * * @param type the channel type; current channel types supported are nio and epoll * @return an instance of {@code ServerBootstrap} */ private ServerBootstrap createBootstrapOfType(final ChannelType type) { final ServerBootstrap boot = new ServerBootstrap(); final int bossThreadCount = Configuration.getInt(PropertyKey.WORKER_NETWORK_NETTY_BOSS_THREADS); // If number of worker threads is 0, Netty creates (#processors * 2) threads by default. final int workerThreadCount = Configuration.getInt(PropertyKey.WORKER_NETWORK_NETTY_WORKER_THREADS); final EventLoopGroup bossGroup = NettyUtils.createEventLoop(type, bossThreadCount, "data-server-boss-%d", false); final EventLoopGroup workerGroup = NettyUtils.createEventLoop(type, workerThreadCount, "data-server-worker-%d", false); final Class<? extends ServerChannel> socketChannelClass = NettyUtils.getServerChannelClass(type); boot.group(bossGroup, workerGroup).channel(socketChannelClass); return boot; }