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 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); }
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); }
.channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override
@Override public void init(ManagerProperties managerProperties) { NettyContext.type = NettyType.server; NettyContext.params = managerProperties; nettyRpcServerHandlerInitHandler.setManagerProperties(managerProperties); int port = managerProperties.getRpcPort(); bossGroup = new NioEventLoopGroup(); 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(nettyRpcServerHandlerInitHandler); // Start the server. b.bind(port); log.info("Socket started on port(s):{}(socket)",port); } catch (Exception e) { // Shut down all event loops to terminate all threads. e.printStackTrace(); } }
private Channel bootstrap(final EventExecutorGroup executor, final SslContext sslCtx, final int port) throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); boolean epoll = bossLoop instanceof EpollEventLoopGroup; bootstrap.group(bossLoop, workerLoop) .channel(epoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class) .handler(new LoggingHandler(Server.class, LogLevel.DEBUG)) .childHandler(new NettyPipeline(executor, dispatcher, conf, sslCtx)); configure(conf.getConfig("netty.options"), "netty.options", (option, value) -> bootstrap.option(option, value)); configure(conf.getConfig("netty.worker.options"), "netty.worker.options", (option, value) -> bootstrap.childOption(option, value)); return bootstrap .bind(host(conf.getString("application.host")), port) .sync() .channel(); }
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout()); b.handler(new LoggingHandler()); b.childHandler(ethereumChannelInitializer);
public static void start(int port) throws Exception { 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 HttpStaticFileServerInitializer()); b.bind(port).sync().channel(); LOGGER.info("Open your web browser and navigate to " + ("http") + "://localhost:" + port + '/'); }
@Override public void run() { bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); new ServerBootstrap().group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100) .handler(new LoggingHandler(EchoServer.class)) .childHandler(new EchoServerInitializer(mockServerLogger, secure, error)) .childAttr(LOG_FILTER, logFilter) .childAttr(NEXT_RESPONSE, nextResponse) .childAttr(ONLY_RESPONSE, onlyResponse) .bind(0) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { boundPort.set(((InetSocketAddress) future.channel().localAddress()).getPort()); } else { boundPort.setException(future.cause()); eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.MILLISECONDS); } } }); } }, "MockServer EchoServer Thread").start();
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)); }
private ChannelFuture doStart(ServerPort port) { final ServerBootstrap b = new ServerBootstrap(); serverBootstrap = b; config.channelOptions().forEach((k, v) -> { @SuppressWarnings("unchecked") final ChannelOption<Object> castOption = (ChannelOption<Object>) k; b.option(castOption, v); }); config.childChannelOptions().forEach((k, v) -> { @SuppressWarnings("unchecked") final ChannelOption<Object> castOption = (ChannelOption<Object>) k; b.childOption(castOption, v); }); b.group(EventLoopGroups.newEventLoopGroup(1, r -> { final FastThreadLocalThread thread = new FastThreadLocalThread(r, bossThreadName(port)); thread.setDaemon(false); return thread; }), config.workerGroup()); b.channel(TransportType.detectTransportType().serverChannelType()); b.handler(connectionLimitingHandler); b.childHandler(new HttpServerPipelineConfigurator(config, port, sslContexts, gracefulShutdownSupport)); return b.bind(port.localAddress()); }
bootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.DEBUG)) .childHandler(childHandler);
.childOption(ChannelOption.SO_RCVBUF, 8192 * 128) .childOption(ChannelOption.SO_SNDBUF, 8192 * 128) .handler(new LoggingHandler(LogLevel.DEBUG)) .childOption(ChannelOption.ALLOCATOR, NettyHelper.getByteBufAllocator()) .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(
.childOption(ChannelOption.SO_RCVBUF, 8192 * 128) .childOption(ChannelOption.SO_SNDBUF, 8192 * 128) .handler(new LoggingHandler(LogLevel.DEBUG)) .childOption(ChannelOption.ALLOCATOR, NettyHelper.getByteBufAllocator()) .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(
@Override public boolean startService() throws Exception{ boolean serviceFlag = super.startService(); bossGroup = new NioEventLoopGroup(1, bossThreadNameFactory); workerGroup = new NioEventLoopGroup(0, workerThreadNameFactory); try{ ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap = serverBootstrap.group(bossGroup, workerGroup); serverBootstrap.channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) .childOption(ChannelOption.SO_REUSEADDR, true) //重用地址 .childOption(ChannelOption.SO_RCVBUF, 65536) .childOption(ChannelOption.SO_SNDBUF, 65536) .childOption(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false)) // heap buf 's better .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, Integer.valueOf(1000)) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(channelInitializer); serverChannelFuture = serverBootstrap.bind(serverPort).sync(); serverChannelFuture.channel().closeFuture().addListener(ChannelFutureListener.CLOSE); }catch (Exception e) { logger.error(e.toString(), e); serviceFlag = false; } return serviceFlag; }
.childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false)) // heap buf 's better .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(channelInitializer);
.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false)) // heap buf 's better .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, Integer.valueOf(1000)) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(channelInitializer);