List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup
public NioEventLoopGroup(ThreadFactory threadFactory)
From source file:com.beeswax.http.server.HttpServer.java
License:Apache License
public void run() throws InterruptedException { // Create event loop groups. One for incoming connections handling and // second for handling actual event by workers final NioEventLoopGroup bossGroup = new NioEventLoopGroup(serverConfig.bossGroupSize); final NioEventLoopGroup workerGroup = new NioEventLoopGroup(); try {//w w w. j a v a 2 s .com ServerBootstrap bootStrap = new ServerBootstrap(); bootStrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) // SO_BACKLOG : The maximum queue length for incoming connections. .option(ChannelOption.SO_BACKLOG, serverConfig.backlogSize) // TCP_NODELAY: option to disable Nagle's algorithm to achieve lower latency on every packet sent .option(ChannelOption.TCP_NODELAY, serverConfig.tcpNodelay) // SO_KEEPALIVE: option to enable keep-alive packets for a socket connection .childOption(ChannelOption.SO_KEEPALIVE, serverConfig.keepAlive) .childHandler(new HttpServerChannelInitializer(serverConfig, handlerFactory)); // bind to port final ChannelFuture channelFuture = bootStrap.bind(serverConfig.port).sync(); // Wait until the server socket is closed. channelFuture.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.bloom.zerofs.rest.NettyServer.java
License:Open Source License
@Override public void start() throws InstantiationException { long startupBeginTime = System.currentTimeMillis(); try {// www . ja v a 2 s . c o m logger.trace("Starting NettyServer deployment"); bossGroup = new NioEventLoopGroup(nettyConfig.nettyServerBossThreadCount); workerGroup = new NioEventLoopGroup(nettyConfig.nettyServerWorkerThreadCount); ServerBootstrap b = new ServerBootstrap(); // Netty creates a new instance of every class in the pipeline for every connection // i.e. if there are a 1000 active connections there will be a 1000 NettyMessageProcessor instances. b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, nettyConfig.nettyServerSoBacklog) .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(channelInitializer); b.bind(nettyConfig.nettyServerPort).sync(); logger.info("NettyServer now listening on port {}", nettyConfig.nettyServerPort); } catch (InterruptedException e) { logger.error("NettyServer start await was interrupted", e); nettyMetrics.nettyServerStartError.inc(); throw new InstantiationException( "Netty server bind to port [" + nettyConfig.nettyServerPort + "] was interrupted"); } finally { long startupTime = System.currentTimeMillis() - startupBeginTime; logger.info("NettyServer start took {} ms", startupTime); nettyMetrics.nettyServerStartTimeInMs.update(startupTime); } }
From source file:com.bloom.zerofs.tools.perf.rest.NettyPerfClient.java
License:Open Source License
/** * Starts the NettyPerfClient./* w w w. jav a 2s . co m*/ * @throws InterruptedException */ protected void start() throws InterruptedException { logger.info("Starting NettyPerfClient"); reporter.start(); group = new NioEventLoopGroup(concurrency); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HttpClientCodec()).addLast(new ChunkedWriteHandler()) .addLast(new ResponseHandler()); } }); logger.info("Connecting to {}:{}", host, port); b.remoteAddress(host, port); perfClientStartTime = System.currentTimeMillis(); for (int i = 0; i < concurrency; i++) { b.connect().addListener(channelConnectListener); } isRunning = true; logger.info("Created {} channel(s)", concurrency); logger.info("NettyPerfClient started"); }
From source file:com.bosscs.spark.commons.extractor.server.ExtractorServer.java
License:Apache License
public static void start() throws CertificateException, SSLException, InterruptedException { // Configure SSL. final SslContext sslCtx; if (SSL) {/* w ww .jav a 2 s . com*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); } else { sslCtx = null; } bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ExtractorServerInitializer(sslCtx)); b.bind(PORT).sync().channel().closeFuture().sync(); }
From source file:com.bow.demo.module.netty.demo.echo.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*w w w . java 2s. c om*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } 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) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(PORT).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.bow.demo.module.netty.demo.objectecho.ObjectEchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {// w w w. j a va 2 s . c o m 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.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new ObjectEchoServerHandler()); } }); // Bind and start to accept incoming connections. b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.caocao.nio.server.NettyServer.java
License:Apache License
public void initAndStart() { System.out.println("port:" + port); // Configure the server. bossGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2); workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2); try {/* w w w.ja v a 2 s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128).option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_SNDBUF, 5 * 1024) .option(ChannelOption.SO_SNDBUF, 5 * 1024) .option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(40, 64, 1024)) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new CustomerInitializer()); // Start the server. ChannelFuture f = b.bind(port).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } catch (InterruptedException e) { logger.error("netty??", e); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.carlos.netty.server.ObjectEchoServer.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 = 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 EchoLoginHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } p.addLast(new StringEncoder(), new StringDecoder(), new ObjectEchoServerHandler()); } }); // Bind and start to accept incoming connections. b.bind(PORT).sync().channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.cats.version.httpserver.HttpStaticVersionMonitorFileServer.java
License:Apache License
private void init() throws Exception { // Configure SSL. final SslContext sslCtx = null; EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// w w w . ja va 2 s .c o m ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new HttpStaticFileServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); System.err.println("Open your web browser and navigate to http://127.0.0.1:" + PORT + '/'); System.err.println("Welcome use " + VERSION_DESC); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.ccompass.netty.proxy.Proxy.java
License:Apache License
public static void main(String[] args) throws Exception { ProxyConfig.loadConfig();/* www . j a va 2 s . c o m*/ EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); checkOtherChannel(ProxyConfig.config.checktimes); //?grops for (int i = 0; i < ProxyConfig.config.branchList.size(); i++) { ChannelGroup group = new DefaultChannelGroup("server-group", null); NettyClient.sinkGroups.add(group); } //???? if (ProxyConfig.config.branchNumbers > 0) { List<List<Channel>> list = new ArrayList(); for (int i = 0; i < ProxyConfig.config.branchList.size(); i++) { List<Channel> channels = new ArrayList<Channel>(); list.add(channels); } NettyClient.setSinkChannels(list); } try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) // .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ProxyInitializer(ProxyConfig.config.mainIP, ProxyConfig.config.mainPort)) .childOption(ChannelOption.AUTO_READ, false).bind(ProxyConfig.config.proxyPort).sync().channel() .closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }