List of usage examples for io.netty.bootstrap ServerBootstrap group
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
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 w w.ja va 2s . co m*/ 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 va2 s . 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) {//from w w w .j av a 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 {// ww w . j a v a2 s.c o m 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 ww w.ja va2s .co 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 {/* w w w. java 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.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 {//from w w w . jav a 2s. c o m 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.caricah.iotracah.server.netty.ServerImpl.java
License:Apache License
/** * The @link configure method is responsible for starting the implementation server processes. * The implementation should return once the server has started this allows * the launcher to maintain the life of the application. * * @throws UnRetriableException//from w w w . j a v a2 s .c om */ public void initiate() throws UnRetriableException { log.info(" configure : initiating the netty server."); try { int countOfAvailableProcessors = Runtime.getRuntime().availableProcessors() + 1; if (Epoll.isAvailable()) { bossEventLoopGroup = new EpollEventLoopGroup(2, getExecutorService()); workerEventLoopGroup = new EpollEventLoopGroup(countOfAvailableProcessors, getExecutorService()); } else { bossEventLoopGroup = new NioEventLoopGroup(2, getExecutorService()); workerEventLoopGroup = new NioEventLoopGroup(countOfAvailableProcessors, getExecutorService()); } //Initialize listener for TCP ServerBootstrap tcpBootstrap = new ServerBootstrap(); tcpBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); tcpBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); tcpBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); tcpBootstrap = tcpBootstrap.group(bossEventLoopGroup, workerEventLoopGroup); if (Epoll.isAvailable()) { tcpBootstrap = tcpBootstrap.channel(EpollServerSocketChannel.class); } else { tcpBootstrap = tcpBootstrap.channel(NioServerSocketChannel.class); } tcpBootstrap = tcpBootstrap.handler(new LoggingHandler(LogLevel.INFO)) .childHandler(getServerInitializer(this, getConnectionTimeout())); ChannelFuture tcpChannelFuture = tcpBootstrap.bind(getTcpPort()).sync(); tcpChannel = tcpChannelFuture.channel(); if (isSslEnabled()) { //Initialize listener for SSL ServerBootstrap sslBootstrap = new ServerBootstrap(); sslBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); sslBootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); sslBootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); sslBootstrap = sslBootstrap.group(bossEventLoopGroup, workerEventLoopGroup); if (Epoll.isAvailable()) { sslBootstrap = sslBootstrap.channel(EpollServerSocketChannel.class); } else { sslBootstrap = sslBootstrap.channel(NioServerSocketChannel.class); } sslBootstrap = sslBootstrap.handler(new LoggingHandler(LogLevel.INFO)) .childHandler(getServerInitializer(this, getConnectionTimeout(), getSslHandler())); ChannelFuture sslChannelFuture = sslBootstrap.bind(getSslPort()).sync(); sslChannel = sslChannelFuture.channel(); } } catch (InterruptedException e) { log.error(" configure : Initialization issues ", e); throw new UnRetriableException(e); } }
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) {/*www. j a va 2s . c om*/ 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.cat.netty.file.FileServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w . j a v a 2s . c o m 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(); } }