List of usage examples for io.netty.bootstrap ServerBootstrap group
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
From source file:com.dinstone.rpc.netty.server.NettyServer.java
License:Apache License
public void start() { bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); try {//from w ww .ja v a2 s . c o m ServerBootstrap boot = new ServerBootstrap(); boot.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new RpcProtocolDecoder(true)); ch.pipeline().addLast(new RpcProtocolEncoder(true)); ch.pipeline().addLast(new NettyServerHandler(handler)); } }); boot.option(ChannelOption.SO_BACKLOG, 128); boot.childOption(ChannelOption.SO_KEEPALIVE, true); int port = config.getInt(Constants.SERVICE_PORT, Constants.DEFAULT_SERVICE_PORT); InetSocketAddress localAddress = new InetSocketAddress(port); String host = config.get(Constants.SERVICE_HOST); if (host != null) { localAddress = new InetSocketAddress(host, port); } LOG.info("RPC service works on " + localAddress); boot.bind(localAddress).sync(); } catch (InterruptedException e) { throw new RpcException(500, "Server can't bind to the specified local address ", e); } }
From source file:com.dlc.server.DLCHttpServer.java
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {//from w w w. j av a2 s . c om 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.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new DLCHttpServerInitializer(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.doctor.netty5.ebook.netty_the_definitive_guide.Chapter4ForTimerServer.java
License:Apache License
public void bind(int port) throws InterruptedException { // ??NIO/* w ww .ja v a 2 s .co m*/ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(1024)); ch.pipeline().addLast(new StringDecoder()); ch.pipeline().addLast(new TimeServerHander()); } }); ChannelFuture future = b.bind(port).sync(); future.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.ebook.netty_the_definitive_guide.TimeServer.java
License:Apache License
public void bind(int port) throws InterruptedException { // ??NIO/*from w w w. j a v a2 s . c om*/ EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeServerHander()); } }); ChannelFuture future = b.bind(port).sync(); future.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.chatroom.ChatRoomServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from ww w . j av a 2s .c o m*/ bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( new DelimiterBasedFrameDecoder(8049, true, Delimiters.lineDelimiter())); ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new ChatRoomServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(ChatRoomServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.echo.EchoServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w ww. ja va 2 s.c o m*/ bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(2048)); ch.pipeline().addLast(new StringDecoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new StringEncoder(StandardCharsets.UTF_8)); ch.pipeline().addLast(new EchoServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(EchoServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.echo_object.EchoServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w .j av a2 s.c o m*/ bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ObjectEncoder()); ch.pipeline().addLast(new ObjectDecoder( ClassResolvers.softCachingResolver(this.getClass().getClassLoader()))); ch.pipeline().addLast(new EchoServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(EchoServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.factorial_algorithm.FactorialServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {// ww w . ja v a 2 s.c om bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new NumberEncoder()); ch.pipeline().addLast(new BigIntegerDecoder()); ch.pipeline().addLast(new FactorialServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(FactorialServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.doctor.netty5.example.http.helloworld.HelloWorldServer.java
License:Apache License
public void start() throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); NioEventLoopGroup bossGroup = new NioEventLoopGroup(1); NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w . j a v a 2 s . c o m*/ bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port) .option(ChannelOption.SO_BACKLOG, 1024).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HttpServerCodec()); ch.pipeline().addLast(new HelloWorldServerHandler()); } }); ChannelFuture channelFuture = bootstrap.bind().sync(); System.out.println(HelloWorldServer.class.getName() + " started and listen on port:" + channelFuture.channel().localAddress()); channelFuture.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.dwarf.netty.guide.factorial.FactorialServer.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 va 2 s.c o 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(LogLevel.INFO)) .childHandler(new FactorialServerInitializer(sslCtx)); b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }