List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup
public NioEventLoopGroup()
From source file:com.bianlz.ndg.p14.server.NettyServer.java
License:Apache License
public void bind() throws Exception { // ??NIO//from ww w.j av a 2s . c o m EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws IOException { ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4)); ch.pipeline().addLast(new NettyMessageEncoder()); ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(5000)); ch.pipeline().addLast(new LoginAuthRespHandler()); ch.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler()); } }); // ??? b.bind(Constant.REMOTEIP, Constant.PORT).sync(); System.out.println("Netty server start ok : " + (Constant.REMOTEIP + " : " + Constant.PORT)); }
From source file:com.bow.demo.module.netty.demo.echo.EchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL.git final SslContext sslCtx; if (SSL) {//from w ww. j a va2s . co m sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT)); } //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(HOST, PORT).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.bow.demo.module.netty.demo.objectecho.ObjectEchoClient.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 va2 s. c om sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { sslCtx = null; } EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT)); } p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new ObjectEchoClientHandler()); } }); // Start the connection attempt. b.connect(HOST, PORT).sync().channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:com.brainlounge.zooterrain.netty.WebSocketServer.java
License:Apache License
public void run() throws Exception { final ZkStateObserver zkStateObserver = new ZkStateObserver(zkConnection); EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from ww w. j ava 2 s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new WebSocketServerInitializer(zkStateObserver)); Channel ch = b.bind(port).sync().channel(); System.out.println("Zooterrain server started at port " + port + '.'); zkStateObserver.start(); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.buildria.mocking.stub.StubHttpServer.java
License:Open Source License
@Override public StubHttpServer start() { Stopwatch sw = createStarted();//from w w w. jav a2 s .com bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { // CHECKSTYLE:OFF @Override public void initChannel(SocketChannel ch) throws Exception { // CHECKSTYLE:ON // int maxInitialLineLength, int maxHeaderSize, int maxChunkSize ch.pipeline().addLast("decoder", new HttpRequestDecoder(MAX_INITIALLINE_LENGH, MAX_HEADERS_SIZE, MAX_CHUNK_SIZE)); ch.pipeline().addLast("aggregator", new HttpObjectAggregator(MAX_CONTENT_LENGTH)); ch.pipeline().addLast("encoder", new HttpResponseEncoder()); ch.pipeline().addLast("deflater", new HttpContentCompressor()); if (config.isLogging()) { ch.pipeline().addLast("logging", new LoggingHandler(StubHttpServer.class)); } ch.pipeline().addLast("handler", new Handler()); } }).option(ChannelOption.SO_BACKLOG, SO_BACKLOG).childOption(ChannelOption.SO_KEEPALIVE, true); // Bind and start to accept incoming connections. int port = config.getPort(); ChannelFuture f; try { f = b.bind(port).sync(); } catch (InterruptedException ex) { throw new MockingException(ex); } f.awaitUninterruptibly(); sw.stop(); LOG.debug("### StubHttpServer(port:{}) started. It took {}", port, sw); return this; }
From source file:com.bunjlabs.fuga.network.netty.NettyHttpServer.java
License:Apache License
@Override public void start() { if (addr instanceof InetSocketAddress) { InetSocketAddress inetAddr = (InetSocketAddress) addr; log.info("Starting up HTTP server at {}{}{}/", "http://", inetAddr.getHostString(), inetAddr.getPort() == 80 ? "" : ":" + inetAddr.getPort()); } else {/*w w w. j av a 2 s .c om*/ log.info("Starting up HTTP server"); } EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new NettyHttpServerInitializer(app)); Channel ch = b.bind(addr).sync().channel(); ch.closeFuture().sync(); } catch (Exception ex) { log.error(ex); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.caocao.nio.client.EchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {/*from ww w. j a v a2 s. com*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new MsgDecoder()); p.addLast(new MsgEncoder()); p.addLast(new EchoClientHandler()); } }); // Start the client. ChannelFuture f = b.connect("127.0.0.1", 80).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.cat.netty.file.FileServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* w ww. jav a 2 s .c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).childHandler(new ChannelInitializer<SocketChannel>() { /* * (non-Javadoc) * * @see * io.netty.channel.ChannelInitializer#initChannel(io * .netty.channel.Channel) */ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(1024), new StringDecoder(CharsetUtil.UTF_8), new FileServerHandler()); } }); ChannelFuture f = b.bind(port).sync(); System.out.println("Server start at port : " + port); f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.cdg.study.netty.time.TimeClient.java
License:Open Source License
public static void main(String[] args) throws Exception { // String host = args[0]; // int port = Integer.parseInt(args[1]); String host = "localhost"; int port = 8021; EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w . jav a 2s. com*/ Bootstrap b = new Bootstrap(); // (1) b.group(workerGroup); // (2) b.channel(NioSocketChannel.class); // (3) b.option(ChannelOption.SO_KEEPALIVE, true); // (4) b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); // (5) // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); } }
From source file:com.celeral.netlet.benchmark.netty.BenchmarkTcpClient.java
License:Apache License
public static void main(String[] args) { String host = args[0];// ww w. j av a2s. co m int port = Integer.parseInt(args[1]); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); // (1) b.group(workerGroup); // (2) b.channel(NioSocketChannel.class); // (3) b.option(ChannelOption.SO_KEEPALIVE, true); // (4) b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new BenchmarkTcpClient()); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); // (5) // Wait until the connection is closed. f.channel().closeFuture().sync(); } catch (Exception e) { logger.error("", e); } finally { workerGroup.shutdownGracefully(); } }