private ServerBootstrap createServerBootstrap( ServerConfigurationProvider configurationProvider, ProtocolInitializer protocolInitializer ) { return new ServerBootstrap() .group( bossGroup, selectorGroup ) .channel( configurationProvider.getChannelClass() ) .option( ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT ) .option( ChannelOption.SO_REUSEADDR, true ) .childOption( ChannelOption.SO_KEEPALIVE, true ) .childHandler( protocolInitializer.channelInitializer() ); } }
protected ServerBootstrap getBootstrap(MessageInput input) { final LinkedHashMap<String, Callable<? extends ChannelHandler>> parentHandlers = getChannelHandlers(input); final LinkedHashMap<String, Callable<? extends ChannelHandler>> childHandlers = getChildChannelHandlers(input); childEventLoopGroup = eventLoopGroupFactory.create(workerThreads, localRegistry, "workers"); return new ServerBootstrap() .group(parentEventLoopGroup, childEventLoopGroup) .channelFactory(new ServerSocketChannelFactory(nettyTransportConfiguration.getType())) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(8192)) .option(ChannelOption.SO_RCVBUF, getRecvBufferSize()) .childOption(ChannelOption.SO_RCVBUF, getRecvBufferSize()) .childOption(ChannelOption.SO_KEEPALIVE, tcpKeepalive) .handler(getChannelInitializer(parentHandlers)) .childHandler(getChannelInitializer(childHandlers)); }
}); awaitLatch(latch); ServerBootstrap bs = new ServerBootstrap(); bs.group(context.nettyEventLoop()); bs.channelFactory(((VertxInternal)vertx).transport().serverChannelFactory(false)) ; bs.option(ChannelOption.SO_BACKLOG, 100); bs.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelFuture fut = bs.bind("localhost", 1234); try { fut.sync(); await(); } finally { fut.channel().close().sync();
private void groupsNio(final ServerBootstrap bootstrap, final int workThreads) { workerGroup = new NioEventLoopGroup(workThreads); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(EpollChannelOption.TCP_CORK, true) .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 100) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(nettyServerHandlerInitializer); }
}); awaitLatch(latch); ServerBootstrap bs = new ServerBootstrap(); bs.group(context.nettyEventLoop()); bs.channelFactory(((VertxInternal)vertx).transport().serverChannelFactory(false)) ; bs.option(ChannelOption.SO_BACKLOG, 100); bs.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelFuture fut = bs.bind("localhost", 1234); try { fut.sync(); await(); } finally { fut.channel().close().sync();
private void groupsNio(final ServerBootstrap bootstrap) { workerGroup = new NioEventLoopGroup(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024)) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ServerHandlerInitializer()); } }
private void groupsNio(final ServerBootstrap bootstrap) { workerGroup = new NioEventLoopGroup(); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024)) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ServerHandlerInitializer()); } }
private void groupsEpoll(final ServerBootstrap bootstrap) { workerGroup = new EpollEventLoopGroup(); bootstrap.group(bossGroup, workerGroup) .channel(EpollServerSocketChannel.class) .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024)) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ServerHandlerInitializer()); }
private void groupsEpoll(final ServerBootstrap bootstrap) { workerGroup = new EpollEventLoopGroup(); bootstrap.group(bossGroup, workerGroup) .channel(EpollServerSocketChannel.class) .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024)) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ServerHandlerInitializer()); }
private void groupsEpoll(final ServerBootstrap bootstrap, final int workThreads) { workerGroup = new EpollEventLoopGroup(workThreads); bootstrap.group(bossGroup, workerGroup) .channel(EpollServerSocketChannel.class) .option(EpollChannelOption.TCP_CORK, true) .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 100) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(nettyServerHandlerInitializer); }
@Override protected ServerBootstrap getServerBootstrap() { ServerBootstrap b = new ServerBootstrap(); b.group(_bossGroup, _workerGroup).channel(NioServerSocketChannel.class).childHandler(createChannelInitializer()) .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); return b; }
@Override protected int initServer() throws InterruptedException { bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(final Channel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new TracingDispatchHandler()); p.addLast(new HelloWorldHandler()); } }); Channel ch = b.bind(0).sync().channel(); return ((InetSocketAddress) ch.localAddress()).getPort(); }
private void initFactory(String host, int port, String protocol, final PipelineInitializer pipelieInitializer) { LOG.debug("Initializing integration. Protocol={}", protocol); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(channelClass) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { pipelieInitializer.init(ch); } }) .option(ChannelOption.SO_BACKLOG, nettySoBacklog) .option(ChannelOption.SO_REUSEADDR, nettySoReuseaddr) .childOption(ChannelOption.TCP_NODELAY, nettyTcpNodelay) .childOption(ChannelOption.SO_KEEPALIVE, nettySoKeepalive); try { LOG.debug("Binding integration. host={}, port={}", host, port); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(host, port); LOG.info("Server bound to host={}, port={}, protocol={}", host, port, protocol); f.sync().addListener(FIRE_EXCEPTION_ON_FAILURE); } catch (InterruptedException ex) { LOG.error("An interruptedException was caught while initializing integration. Protocol={}", protocol, ex); } }
this.eventLoopGroup = eventLoopGroup; b = new ServerBootstrap() .channel(TransportCheck.getServerSocketChannel()) .option(ChannelOption.SO_BACKLOG, 1000) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30*1000) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.SO_RCVBUF, 1 << 17) .option(ChannelOption.SO_SNDBUF, 1 << 17) .group(eventLoopGroup) // .childOption(ChannelOption.ALLOCATOR, alloc) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception {
private ChannelFuture setupServerBootstrap(int port, ChannelInitializer channelInitializer) throws InterruptedException ServerBootstrap serverBootstrap = new ServerBootstrap().group( serverGroup.clientToProxyBossPool, serverGroup.clientToProxyWorkerPool); serverBootstrap = serverBootstrap.channel(EpollServerSocketChannel.class); channelOptions.put(EpollChannelOption.TCP_DEFER_ACCEPT, Integer.valueOf(-1)); serverBootstrap = serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap = serverBootstrap.option(optionEntry.getKey(), optionEntry.getValue()); serverBootstrap.childHandler(channelInitializer); serverBootstrap.validate(); return serverBootstrap.bind(port).sync();
public void start() { bootstrap.option(ChannelOption.SO_REUSEADDR, true); bootstrap.childOption(ChannelOption.TCP_NODELAY, true); bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ch.pipeline().addLast("connectionHandler", connectionHandler); ch.pipeline().addLast("encoder", new EncodeHandler()); ch.pipeline().addLast("decoder", new DecodeHandler(true)); ch.pipeline().addLast("dispatcher", serverHandler); } }); try { channel = bootstrap.bind(port).await().channel(); } catch (InterruptedException e) { LOG.error("server start fail", e); } LOG.info("listen on port {}", port); }
public FileServer(PathResolver pResolver, int port) { InetSocketAddress addr = new InetSocketAddress(port); // Configure the server. bootstrap = new ServerBootstrap(); bootstrap.group(new OioEventLoopGroup(), new OioEventLoopGroup()) .channel(OioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100) .option(ChannelOption.SO_RCVBUF, 1500) .childHandler(new FileServerChannelInitializer(pResolver)); // Start the server. channelFuture = bootstrap.bind(addr); try { // Get the address we bound to. InetSocketAddress boundAddress = ((InetSocketAddress) channelFuture.sync().channel().localAddress()); this.port = boundAddress.getPort(); } catch (InterruptedException ie) { this.port = 0; } }
/** * Netty HTTP server initialization. */ public void init() { try { LOG.info("NettyServer Initializing..."); bossGroup = new NioEventLoopGroup(); LOG.debug("NettyServer bossGroup created"); workerGroup = new NioEventLoopGroup(); LOG.debug("NettyServer workGroup created"); btsServer = new ServerBootstrap(); LOG.debug("NettyServer ServerBootstrap created"); ChannelInitializer<SocketChannel> serverInit = configureInitializer(); LOG.debug("NettyServer InitClass instance created"); LOG.debug("NettyServer InitClass instance init()"); btsServer.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(serverInit) .option(ChannelOption.SO_REUSEADDR, true); LOG.debug("NettyServer ServerBootstrap group initialized"); bindChannel = btsServer.bind(bindAddress, bindPort).sync().channel(); } catch (Exception exception) { LOG.error("NettyHttpServer init() failed", exception); } }
ServerBootstrap b = new ServerBootstrap(); this.bossGroup = new NioEventLoopGroup(1); this.workerGroup = new NioEventLoopGroup(DEFAULT_EVENT_LOOP_THREADS); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { .childOption(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.SO_RCVBUF, 32 * 1024); b.bind(port).addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) {
public void start() throws Exception { if (bossGroup == null && workerGroup == null) { bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel channel) throws Exception { channel.pipeline() .addLast(new LengthFieldBasedFrameDecoder(65536, 0, 4, 0, 0)) .addLast(new RpcDecoder(RpcRequest.class)) .addLast(new RpcEncoder(RpcResponse.class)) .addLast(new RpcHandler(handlerMap)); } }) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); String[] array = serverAddress.split(":"); String host = array[0]; int port = Integer.parseInt(array[1]); ChannelFuture future = bootstrap.bind(host, port).sync(); logger.info("Server started on port {}", port); if (serviceRegistry != null) { serviceRegistry.register(serverAddress); } future.channel().closeFuture().sync(); } }