private void groupsNio(final Bootstrap bootstrap) { bootstrap.group(workerGroup) .channel(NioSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); }
@Override protected void doOpen() throws Throwable { final NettyClientHandler nettyClientHandler = new NettyClientHandler(getUrl(), this); bootstrap = new Bootstrap(); bootstrap.group(nioEventLoopGroup) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) //.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getTimeout()) .channel(NioSocketChannel.class); if (getConnectTimeout() < 3000) { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000); } else { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeout()); } bootstrap.handler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { int heartbeatInterval = UrlUtils.getIdleTimeout(getUrl()); NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); ch.pipeline()//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug .addLast("decoder", adapter.getDecoder()) .addLast("encoder", adapter.getEncoder()) .addLast("client-idle-handler", new IdleStateHandler(heartbeatInterval, 0, 0, MILLISECONDS)) .addLast("handler", nettyClientHandler); } }); }
/** * Populate connection builder with necessary resources. * * @param socketAddressSupplier address supplier for initial connect and re-connect * @param connectionBuilder connection builder to configure the connection * @param redisURI URI of the Redis instance */ protected void connectionBuilder(Mono<SocketAddress> socketAddressSupplier, ConnectionBuilder connectionBuilder, RedisURI redisURI) { Bootstrap redisBootstrap = new Bootstrap(); redisBootstrap.option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 32 * 1024); redisBootstrap.option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 8 * 1024); redisBootstrap.option(ChannelOption.ALLOCATOR, BUF_ALLOCATOR); SocketOptions socketOptions = getOptions().getSocketOptions(); redisBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Math.toIntExact(socketOptions.getConnectTimeout().toMillis())); if (LettuceStrings.isEmpty(redisURI.getSocket())) { redisBootstrap.option(ChannelOption.SO_KEEPALIVE, socketOptions.isKeepAlive()); redisBootstrap.option(ChannelOption.TCP_NODELAY, socketOptions.isTcpNoDelay()); } connectionBuilder.timeout(redisURI.getTimeout()); connectionBuilder.password(redisURI.getPassword()); connectionBuilder.bootstrap(redisBootstrap); connectionBuilder.channelGroup(channels).connectionEvents(connectionEvents).timer(timer); connectionBuilder.socketAddressSupplier(socketAddressSupplier); }
(AddressResolverGroup<InetSocketAddress>) addressResolverGroupFactory.apply(workerGroup); final Bootstrap baseBootstrap = new Bootstrap(); baseBootstrap.channel(TransportType.socketChannelType(workerGroup)); baseBootstrap.resolver(addressResolverGroup); @SuppressWarnings("unchecked") final ChannelOption<Object> castOption = (ChannelOption<Object>) option; baseBootstrap.option(castOption, value); });
private Bootstrap initClientBootstrap() { Bootstrap b = new Bootstrap(); eventLoopGroup = new NioEventLoopGroup(); b.group(eventLoopGroup) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ClusterClientConfigManager.getConnectTimeout()) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { clientHandler = new TokenClientHandler(currentState, disconnectCallback); ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2)); pipeline.addLast(new NettyResponseDecoder()); pipeline.addLast(new LengthFieldPrepender(2)); pipeline.addLast(new NettyRequestEncoder()); pipeline.addLast(clientHandler); } }); return b; }
private void groupsEpoll(final Bootstrap bootstrap) { bootstrap.group(workerGroup) .channel(EpollSocketChannel.class) .option(EpollChannelOption.TCP_CORK, true) .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); }
public void init() { bootstrap = new Bootstrap(); bootstrap.group(new OioEventLoopGroup()) .channel(OioSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout) .handler(new FileClientChannelInitializer(handler)); }
private void groupsNio(final Bootstrap bootstrap) { bootstrap.group(workerGroup) .channel(NioSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); }
private void groupsEpoll(final Bootstrap bootstrap) { bootstrap.group(workerGroup) .channel(EpollSocketChannel.class) .option(EpollChannelOption.TCP_CORK, true) .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.SO_BACKLOG, 128) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); }
public Bootstrap newBootstrap(final EventPublisher eventPublisher, final ClientEventListener eventListener) { final Bootstrap nettyBootstrap = new Bootstrap().group(eventLoopGroup) .channel(channelClass) .option(ChannelOption.AUTO_READ, false);// by default do not read content unless asked. for (Entry<ChannelOption<?>, Object> optionEntry : options.entrySet()) { // Type is just for safety for user of ClientState, internally in Bootstrap, types are thrown on the floor. @SuppressWarnings("unchecked") ChannelOption<Object> key = (ChannelOption<Object>) optionEntry.getKey(); nettyBootstrap.option(key, optionEntry.getValue()); } nettyBootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(ClientChannelActiveBufferingHandler.getName(), new ChannelActivityBufferingHandler(eventPublisher, eventListener)); } }); return nettyBootstrap; }
private Bootstrap createBootstrap(RedisClientConfig config, Type type) { Bootstrap bootstrap = new Bootstrap() .resolver(config.getResolverGroup()) .channel(config.getSocketChannelClass()) .group(config.getGroup()); bootstrap.handler(new RedisChannelInitializer(bootstrap, config, this, channels, type)); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout()); bootstrap.option(ChannelOption.SO_KEEPALIVE, config.isKeepAlive()); bootstrap.option(ChannelOption.TCP_NODELAY, config.isTcpNoDelay()); return bootstrap; }
private void groups(final Bootstrap bootstrap, final int workThreads) { if (Objects.equals(StandardSystemProperty.OS_NAME.value(), OS_NAME)) { workerGroup = new EpollEventLoopGroup(workThreads); bootstrap.group(workerGroup); bootstrap.channel(EpollSocketChannel.class); bootstrap.option(EpollChannelOption.TCP_CORK, true) .option(EpollChannelOption.SO_KEEPALIVE, true) .option(EpollChannelOption.CONNECT_TIMEOUT_MILLIS, 5) .option(EpollChannelOption.SO_BACKLOG, 1024) .option(EpollChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) .handler(nettyClientHandlerInitializer); } else { workerGroup = new NioEventLoopGroup(workThreads); bootstrap.group(workerGroup); bootstrap.channel(NioSocketChannel.class); bootstrap.option(ChannelOption.SO_BACKLOG, 1024) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .handler(new LoggingHandler(LogLevel.INFO)) .handler(nettyClientHandlerInitializer); } }
private Bootstrap createBootstrap(RedisClientConfig config, Type type) { Bootstrap bootstrap = new Bootstrap() .resolver(config.getResolverGroup()) .channel(config.getSocketChannelClass()) .group(config.getGroup()); bootstrap.handler(new RedisChannelInitializer(bootstrap, config, this, channels, type)); bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout()); bootstrap.option(ChannelOption.SO_KEEPALIVE, config.isKeepAlive()); bootstrap.option(ChannelOption.TCP_NODELAY, config.isTcpNoDelay()); return bootstrap; }
private CompletableFuture<Void> bootstrap() { Bootstrap serverBootstrap = new Bootstrap() .group(group) .channel(NioDatagramChannel.class) .handler(new SimpleChannelInboundHandler<DatagramPacket>() { @Override protected void channelRead0(ChannelHandlerContext context, DatagramPacket packet) throws Exception { byte[] payload = new byte[packet.content().readInt()]; packet.content().readBytes(payload); Message message = SERIALIZER.decode(payload); Map<BiConsumer<Address, byte[]>, Executor> listeners = NettyUnicastService.this.listeners.get(message.subject()); if (listeners != null) { listeners.forEach((consumer, executor) -> executor.execute(() -> consumer.accept(message.source(), message.payload()))); } } }) .option(ChannelOption.RCVBUF_ALLOCATOR, new DefaultMaxBytesRecvByteBufAllocator()) .option(ChannelOption.SO_BROADCAST, true) .option(ChannelOption.SO_REUSEADDR, true); return bind(serverBootstrap); }
private void createClient(Listener listener, EventLoopGroup workerGroup, ChannelFactory<? extends Channel> channelFactory) { this.workerGroup = workerGroup; this.bootstrap = new Bootstrap(); bootstrap.group(workerGroup)// .option(ChannelOption.SO_REUSEADDR, true)// .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// .channelFactory(channelFactory); bootstrap.handler(new ChannelInitializer<Channel>() { // (4) @Override public void initChannel(Channel ch) throws Exception { initPipeline(ch.pipeline()); } }); initOptions(bootstrap); listener.onSuccess(); }
public ChannelFuture connectAsync(String host, int port, String remoteId, boolean discoveryMode) { ethereumListener.trace("Connecting to: " + host + ":" + port); EthereumChannelInitializer ethereumChannelInitializer = ctx.getBean(EthereumChannelInitializer.class, remoteId); ethereumChannelInitializer.setPeerDiscoveryMode(discoveryMode); Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout()); b.remoteAddress(host, port); b.handler(ethereumChannelInitializer); // Start the client. return b.connect(); }
public LogEventMonitor(InetSocketAddress address) { group = new NioEventLoopGroup(); bootstrap = new Bootstrap(); bootstrap.group(group) .channel(NioDatagramChannel.class) .option(ChannelOption.SO_BROADCAST, true) .handler( new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast(new LogEventDecoder()); pipeline.addLast(new LogEventHandler()); } } ) .localAddress(address); }
public final void run(final String host, final int port, final ChannelHandler pipelineFactory) { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) .channel(NioSocketChannel.class) .remoteAddress(host, port) .option(ChannelOption.TCP_NODELAY, true) .handler(pipelineFactory); try { Channel channel = bootstrap.connect().sync().channel(); ChannelFuture future = channel.closeFuture().sync(); future.addListener(ChannelFutureListener.CLOSE); } catch (InterruptedException e) { throw new MocoException(e); } finally { group.shutdownGracefully(); } } }