List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup
public NioEventLoopGroup(ThreadFactory threadFactory)
From source file:com.dingwang.netty.server.DiscardServer.java
License:Open Source License
public void run() { //NioEventLoopGroup ??I/O?Netty????EventLoopGroup //????????2NioEventLoopGroup //???boss?????worker??? //boss?worker??? //Channels??EventLoopGroup??? EventLoopGroup bossGroup = new NioEventLoopGroup(5); EventLoopGroup workerGroup = new NioEventLoopGroup(20); //ServerBootstrap ?NIO????Channel?? //????//www . j a va 2 s . c o m ServerBootstrap b = new ServerBootstrap(); //NioServerSocketChannel?Channel? b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) //?????ChannelChannelInitializer? //?Channel?DiscardServerHandle?? //ChannelChannelPipeline??????? //?pipline?????? .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeEncoder(), new DiscardServerHandler()); } }) //????TCP/IP??socket? //tcpNoDelaykeepAlive?ChannelOptionChannelConfig?? //ChannelOptions .option(ChannelOption.SO_BACKLOG, 128) //option()childOption()?option()??NioServerSocketChannel?? //childOption()???ServerChannel?NioServerSocketChannel .childOption(ChannelOption.SO_KEEPALIVE, true); try { //?????8080? //?bind()(???) ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.dingwang.netty.server.PersonServer.java
License:Open Source License
public void run() { //NioEventLoopGroup ??I/O?Netty????EventLoopGroup //????????2NioEventLoopGroup //???boss?????worker??? //boss?worker??? //Channels??EventLoopGroup??? EventLoopGroup bossGroup = new NioEventLoopGroup(5); EventLoopGroup workerGroup = new NioEventLoopGroup(20); //ServerBootstrap ?NIO????Channel?? //????/* www. j a v a 2 s . c o m*/ ServerBootstrap b = new ServerBootstrap(); //NioServerSocketChannel?Channel? b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) //?????ChannelChannelInitializer? //?Channel?DiscardServerHandle?? //ChannelChannelPipeline??????? //?pipline?????? .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new PersonDecoder(), new PersonEncoder(), new PersonOutHandler()); } }) //????TCP/IP??socket? //tcpNoDelaykeepAlive?ChannelOptionChannelConfig?? //ChannelOptions .option(ChannelOption.SO_BACKLOG, 128) //option()childOption()?option()??NioServerSocketChannel?? //childOption()???ServerChannel?NioServerSocketChannel .childOption(ChannelOption.SO_KEEPALIVE, true); try { //?????8080? //?bind()(???) ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.dinstone.netty.Server.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_REUSEADDR, true).childHandler(new ChannelInitializer<NioSocketChannel>() { @Override//w w w . j ava2 s . co m protected void initChannel(NioSocketChannel ch) throws Exception { ch.pipeline().addLast(new SimpleServerHandler()); } }); b.bind(8090).sync().channel().closeFuture().sync(); }
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 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.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.example.chatroom.ChatRoomClient.java
License:Apache License
public void start() throws InterruptedException { NioEventLoopGroup workersGroup = new NioEventLoopGroup(1); try {//from w w w . j a v a2 s . com Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(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 ChatRoomClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } finally { workersGroup.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 {/* w w w. j av a 2 s.com*/ 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.EchoClient.java
License:Apache License
public void start() throws InterruptedException { NioEventLoopGroup workersGroup = new NioEventLoopGroup(1); try {/*from ww w . j a v a 2 s. c om*/ Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(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 EchoClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } finally { workersGroup.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 {// www .j av a 2s . 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 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.EchoClient.java
License:Apache License
public void start() throws InterruptedException { NioEventLoopGroup workersGroup = new NioEventLoopGroup(1); try {//from www . jav a 2 s . c o m Bootstrap bootstrap = new Bootstrap(); bootstrap.group(workersGroup).channel(NioSocketChannel.class).remoteAddress(host, port) .handler(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 EchoClientHandler()); } }); ChannelFuture channelFuture = bootstrap.connect().sync(); channelFuture.channel().closeFuture().sync(); } finally { workersGroup.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 {// www . j a va 2s . 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 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(); } }