List of usage examples for io.netty.bootstrap ServerBootstrap group
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)
From source file:com.zextras.modules.chat.services.LocalXmppService.java
License:Open Source License
@Override public void run() { ChatLog.log.info("Listening on port " + DEFAULT_LOCAL_XMPP_PORT); EventLoopGroup acceptorGroup = new NioEventLoopGroup(4); EventLoopGroup channelWorkerGroup = new NioEventLoopGroup(8); Channel channel;/*from w w w. j a v a2 s. c o m*/ try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(acceptorGroup, channelWorkerGroup); bootstrap.channel(NioServerSocketChannel.class); ChannelHandler handler = new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { try { SSLEngine sslEngine = mZimbraSSLContextProvider.get().createSSLEngine(); sslEngine.setUseClientMode(false); SslHandler sslHandler = new SslHandler(sslEngine); ch.pipeline().addFirst("ssl", sslHandler); ch.pipeline().addLast(null, "SubTagTokenizer", new XmlSubTagTokenizer()); ch.pipeline().addLast(null, "XmlTagTokenizer", new XmlTagTokenizer()); ch.pipeline().addAfter("XmlTagTokenizer", "StanzaProcessor", new ChannelInboundHandlerAdapter() { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { mLocalXmppReceiver.processStanza((String) msg); } }); } catch (Throwable t) { ChatLog.log.warn("Unable to initializer XMPP connection: " + Utils.exceptionToString(t)); ch.close(); } } }; ChannelFuture channelFuture = bootstrap.childHandler(handler).option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 0).bind(DEFAULT_LOCAL_XMPP_PORT).sync(); if (!channelFuture.isSuccess()) { throw channelFuture.cause(); } channel = channelFuture.channel(); mInitializationPromise.setSuccess(null); } catch (Throwable e) { mInitializationPromise.setFailure(e); return; } mLock.lock(); try { while (!mStopRequested) { try { mWaitStopRequest.await(); } catch (InterruptedException ignored) { } } channel.close().sync(); acceptorGroup.shutdownGracefully().sync(); channelWorkerGroup.shutdownGracefully().sync(); } catch (InterruptedException ignored) { } finally { mLock.unlock(); } }
From source file:com.zhucode.longio.transport.netty.NettyConnector.java
License:Open Source License
private void runOneHttpServer(int port, Dispatcher dispatcher, ProtocolType pt) { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup); b.channel(NioServerSocketChannel.class); b.childHandler(new ChannelInitializer<SocketChannel>() { @Override//from w ww . ja v a 2 s. co m protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HttpServerCodec()); ch.pipeline().addLast(new HttpObjectAggregator(65536)); ch.pipeline().addLast(new IdleStateHandler(6000, 3000, 0)); ch.pipeline().addLast(new HttpHandler(NettyConnector.this, dispatcher, callbackDispatcher, getProtocolParser(pt))); } }); b.option(ChannelOption.SO_BACKLOG, 4096); b.childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f; try { f = b.bind(port).sync(); f.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.zhucode.longio.transport.netty.NettyConnector.java
License:Open Source License
private void runOneRawSocketServer(int port, Dispatcher dispatcher, ProtocolType pt) { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup); b.channel(NioServerSocketChannel.class); b.childHandler(new ChannelInitializer<SocketChannel>() { @Override//w w w .j a v a2 s .c o m protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(65536, 0, 2, 0, 2)); ch.pipeline().addLast("encoder", new LengthFieldPrepender(2, false)); ch.pipeline().addLast(new IdleStateHandler(6000, 3000, 0)); ch.pipeline().addLast(new RawSocketHandler(NettyConnector.this, dispatcher, callbackDispatcher, getProtocolParser(pt))); } }); b.option(ChannelOption.SO_BACKLOG, 4096); b.childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f; try { f = b.bind(port).sync(); f.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.zhuika.server.DiscardServer.java
License:Apache License
protected static void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// w ww. ja va 2 s . c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) /* * AdaptiveRecvByteBufAllocator * Channel * * 2 * AdaptiveRecvByteBufAllocator */ //.option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT) //.handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); p.addLast(new IdleStateHandler(0, 0, 150, TimeUnit.SECONDS)); p.addLast("framer", new DelimiterBasedFrameDecoder(8192, false, Delimiters.lineDelimiter())); p.addLast(new ByteToMessageDec()); p.addLast(new MessageToByteEnc()); p.addLast(ServiceHandlerFactory.getDiscardServerHandler()); } }); Logger logger = Logger.getLogger(DiscardServer.class); // Server Config config = XMLReader.loadconfig(); String socketip = config.socketip; String socketport = config.socketport; // Bind and start to accept incoming connections. // ChannelFuture f = b.bind(PORT).sync(); ChannelFuture f = b.bind(socketip, Integer.parseInt(socketport)).sync(); logger.info("TCP server started successfully"); // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to gracefully // shut down your server. f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.zy.learning.netty.websocket.WebSocketServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {// ww w. j a v a2 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 ProtocolDetectorInitializer()); //.childHandler(new WebSocketServerInitializer(sslCtx)); Channel ch = b.bind(PORT).sync().channel(); System.out.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.zz.learning.netty5.chap12.server.NettyServer.java
License:Apache License
private void bind() throws Exception { // ??NIO// w w w. ja va 2s. 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)); // ch.pipeline().addLast(new NettyMessageDecoder(1024 * // 1024, 4, 4)); ch.pipeline().addLast(new NettyMessageEncoder()); ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50)); ch.pipeline().addLast(new LoginAuthRespHandler()); ch.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler()); ch.pipeline().addLast("BusinessMessageRespHandler", new BusinessMessageRespHandler()); } }); // ??? ChannelFuture cf = b.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync(); // serverChannel = cf.channel(); System.out.println("Netty server start ok : " + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT)); }
From source file:Communicate.InputServer.java
@Override public void run() { bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); try {/*from ww w.j a v a 2 s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) //.handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new InputServerInitializer(notifier)); b.bind(PORT).sync().channel().closeFuture().sync(); } catch (InterruptedException ex) { Logger.getLogger(InputServer.class.getName()).log(Level.SEVERE, null, ex); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:connexion.ServerSocket.java
public static void bind(int port) throws InterruptedException, SSLException, CertificateException { // Configure SSL. SelfSignedCertificate ssc = new SelfSignedCertificate(); SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); // Configure Group 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 ServerInitializer(sslCtx)); b.bind(port).sync().channel().closeFuture().sync(); }
From source file:controlspy3.ControlSpy3.java
public void run() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w ww .j av a 2 s.c om*/ // Server ServerBootstrap boots = new ServerBootstrap(); boots.group(bossGroup, workerGroup); boots.channel(NioServerSocketChannel.class); boots.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeEncoder(), new SpyAsServer()); if (ALGO == false) { System.out.println("se salio "); } System.out.println("Cliente conectado!"); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); //SERVER // Bind and start to accept incoming connections. ChannelFuture future_ch = boots.bind(port).sync(); //SERVER // Wait until the server socket is closed. Server future_ch.channel().closeFuture().sync(); } catch (InterruptedException ex) { } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:core.communication.threadpool.io.CallServer.java
License:Apache License
public static void connectServer() throws Exception { // Configure the bootstrap.server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); final CallServerHandler serverHandler = new CallServerHandler(); try {//from w w w .ja v a2 s . co m 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(); p.addLast(serverHandler); } }); // Start the bootstrap.server. ChannelFuture f = b.bind(PORT).sync(); // Wait until the bootstrap.server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }