List of usage examples for io.netty.bootstrap ServerBootstrap ServerBootstrap
public ServerBootstrap()
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 ww. ja v a 2s . co m*/ 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.bianlz.ndg.p14.server.NettyServer.java
License:Apache License
public void bind() throws Exception { // ??NIO/* w ww. j a v a 2 s .co 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.bloom.zerofs.rest.NettyServer.java
License:Open Source License
@Override public void start() throws InstantiationException { long startupBeginTime = System.currentTimeMillis(); try {//from w ww . j a va2s. 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.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) {//ww w .ja va 2s. c om 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) {/*from www. ja v a 2s .c o m*/ 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) {// ww w.ja va 2s. 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.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 w ww . j a v a 2 s . com 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. ja va2 s .c o m*/ 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 {/*from ww w. j av a2s. c o m*/ 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.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. j av a2 s . com*/ 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(); } }