List of usage examples for io.netty.bootstrap ServerBootstrap group
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
From source file:com.dwarf.netty.guide.http.snoop.HttpSnoopServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {//from ww w . j a v a 2 s . c o m SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } else { sslCtx = null; } // 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(); System.err.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:com.dwarf.netty.guide.securechat.SecureChatServer.java
License:Apache License
public static void main(String[] args) throws Exception { SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from www. jav a2s .c o m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new SecureChatServerInitializer(sslCtx)); b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.dwarf.netty.guide.worldclock.WorldClockServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {//from w ww. jav a 2s . 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(LogLevel.INFO)) .childHandler(new WorldClockServerInitializer(sslCtx)); b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.eightkdata.mongowp.mongoserver.MongoServer.java
License:Open Source License
public void run() { // TODO: provide custom ThreadFactories to the EventLoopGroup to name threads correctly? connectionGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); try {//from ww w. ja v a2 s .c o m ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(connectionGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { buildChildHandlerPipeline(socketChannel.pipeline()); } }) // TODO: set TCP channel options? ; ChannelFuture channelFuture = bootstrap.bind(port).awaitUninterruptibly(); try { channelFuture.channel().closeFuture().sync(); } catch (InterruptedException interruptedException) { LOGGER.error("Error", interruptedException); // TODO: perform proper shutdown } catch (Throwable throwable) { LOGGER.error("Error", throwable); // TODO: perform proper shutdown } } finally { workerGroup.shutdownGracefully(); connectionGroup.shutdownGracefully(); } }
From source file:com.eightkdata.mongowp.server.wp.NettyMongoServer.java
License:Open Source License
@Override protected void startUp() throws Exception { LOGGER.info("Listening MongoDB requests on port " + port); connectionGroup = new NioEventLoopGroup(0, new ThreadFactoryBuilder().setNameFormat("netty-connection-%d").build()); workerGroup = new NioEventLoopGroup(0, new ThreadFactoryBuilder().setNameFormat("netty-worker-%d").build()); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(connectionGroup, workerGroup).channel(NioServerSocketChannel.class) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from ww w . j av a 2s .co m*/ protected void initChannel(SocketChannel socketChannel) throws Exception { buildChildHandlerPipeline(socketChannel.pipeline()); } }) // TODO: set TCP channel options? ; ChannelFuture channelFuture = bootstrap.bind(port).awaitUninterruptibly(); if (!channelFuture.isSuccess()) { workerGroup.shutdownGracefully(); connectionGroup.shutdownGracefully(); } }
From source file:com.example.discard.DiscardServerMain.java
License:Apache License
public void run() throws Exception { final NioEventLoopGroup bossGroup = new NioEventLoopGroup();//1 final NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try (AutoCloseable ignore = new ShutDownGracefully(bossGroup, workerGroup)) { final ServerBootstrap bootstrap = new ServerBootstrap();//2 bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)//3 .childHandler(new ChannelInitializer<SocketChannel>() {//4 @Override/*from w w w . j a va 2 s. c o m*/ protected void initChannel(final SocketChannel ch) { ch.pipeline().addLast(new DiscardServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 128)//5 .childOption(ChannelOption.SO_KEEPALIVE, true); //6 final ChannelFuture channelFuture = bootstrap.bind(port).sync();//7 channelFuture.channel().closeFuture().sync(); } }
From source file:com.example.grpc.server.PrometheusServer.java
License:Apache License
public void start() { final ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from w w w . j ava 2 s . c om*/ protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast("decoder", new HttpRequestDecoder()); pipeline.addLast("encoder", new HttpResponseEncoder()); pipeline.addLast("prometheus", new SimpleChannelInboundHandler<Object>() { @Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception { if (!(o instanceof HttpRequest)) { return; } HttpRequest request = (HttpRequest) o; if (!"/metrics".equals(request.uri())) { final FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND); channelHandlerContext.writeAndFlush(response) .addListener(ChannelFutureListener.CLOSE); return; } if (!HttpMethod.GET.equals(request.method())) { final FullHttpResponse response = new DefaultFullHttpResponse( HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_ACCEPTABLE); channelHandlerContext.writeAndFlush(response) .addListener(ChannelFutureListener.CLOSE); return; } ByteBuf buf = Unpooled.buffer(); ByteBufOutputStream os = new ByteBufOutputStream(buf); OutputStreamWriter writer = new OutputStreamWriter(os); TextFormat.write004(writer, registry.metricFamilySamples()); writer.close(); os.close(); final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf); response.headers().set(HttpHeaderNames.CONTENT_TYPE, TextFormat.CONTENT_TYPE_004); channelHandlerContext.writeAndFlush(response) .addListener(ChannelFutureListener.CLOSE); } }); } }); try { this.channel = bootstrap.bind(this.port).sync().channel(); } catch (InterruptedException e) { // do nothing } }
From source file:com.example.http.hello.HttpHelloServer.java
License:Apache License
private void run() throws InterruptedException { final NioEventLoopGroup bossGroup = new NioEventLoopGroup(); final NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try (final GracefulShutdown ignored = GracefulShutdown.gracefulShutdown(bossGroup::shutdownGracefully, workerGroup::shutdownGracefully)) { final ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024); serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(ServerChannelInitializationConfigurer.channelInitializer()); final Channel channel = serverBootstrap.bind(port).sync().channel(); log.info("server started: http://localhost:{}/", port); channel.closeFuture().sync();//from w w w . j a v a 2 s.co m } }
From source file:com.example.server.ServerMain.java
License:Apache License
public void run() throws InterruptedException { final NioEventLoopGroup parentGroup = new NioEventLoopGroup(); final NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try (final GracefulShutdown ignored = GracefulShutdown.gracefulShutdown(parentGroup::shutdownGracefully, workerGroup::shutdownGracefully)) { final ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(parentGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(ServerChannelInitializationConfigurer.channelInitializer()) .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); log.info("server start"); final ChannelFuture channelFuture = bootstrap.bind(port).sync(); channelFuture.channel().closeFuture().sync(); }/*from www .j a v a 2s . c o m*/ }
From source file:com.example.spring.boot.netty.TcpServer.java
License:Apache License
public void bind() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// www . j a v a2 s . c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 65535).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 LineBasedFrameDecoder(1024)); p.addLast(new StringDecoder()); p.addLast(TCP_SERVER_HANDLER); } }); // Start the server. ChannelFuture f = b.bind(port).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } catch (Throwable e) { e.printStackTrace(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }