io.netty.channel.group.ChannelGroup Java Examples
The following examples show how to use
io.netty.channel.group.ChannelGroup.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WhirlpoolMessageHandler.java From whirlpool with Apache License 2.0 | 6 votes |
public WhirlpoolMessageHandler(ChannelGroup channels) { this.channels = channels; ReadIncomingCallable toClientCallable = new ReadIncomingCallable(); FutureTask<String> toClientPc = new FutureTask<>(toClientCallable); ExecutorService toClientExecutor = Executors.newSingleThreadExecutor( new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat("to-client-%d") .build() ); toClientExecutor.execute(toClientPc); SendCommandsToKafkaCallable toKafkaCallable = new SendCommandsToKafkaCallable(); FutureTask<String> toKafka = new FutureTask<>(toKafkaCallable); ExecutorService toKafkaExecutor = Executors.newSingleThreadExecutor( new ThreadFactoryBuilder() .setDaemon(true) .setNameFormat("to-kafka-%d") .build() ); toKafkaExecutor.execute(toKafka); }
Example #2
Source File: NettyHttpServer.java From krpc with Apache License 2.0 | 6 votes |
public void close() { if (workerGroup != null) { log.info("stopping netty server"); bossGroup.shutdownGracefully(); bossGroup = null; ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); allChannels.add(serverChannel); for (Channel ch : conns.values()) { allChannels.add(ch); } ChannelGroupFuture future = allChannels.close(); future.awaitUninterruptibly(); workerGroup.shutdownGracefully(); workerGroup = null; log.info("netty server stopped"); } }
Example #3
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
private TcpServer configureTcpServer(TcpServer tcp, LoopResources loop, ChannelGroup group, CountDownLatch latch) { return tcp.wiretap(true) .host("localhost") .runOn(loop) .channelGroup(group) .doOnBound(s -> latch.countDown()) .doOnConnection(c -> latch.countDown()) .doOnUnbound(s -> latch.countDown()) .handle((req, res) -> res.send(req.receive().retain())) .noSSL() .port(0) .attr(AttributeKey.valueOf("testTcpConfiguration"), "testTcpConfiguration") .option(ChannelOption.valueOf("testTcpConfiguration"), "testTcpConfiguration") .childAttr(AttributeKey.valueOf("testTcpConfiguration"), "testTcpConfiguration") .childOption(ChannelOption.valueOf("testTcpConfiguration"), "testTcpConfiguration") .observe((conn, state) -> latch.countDown()) .childObserve((conn, state) -> latch.countDown()) .doOnChannelInit((observer, channel, address) -> latch.countDown()); }
Example #4
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("deprecation") public void testTcpConfiguration_1() throws Exception { CountDownLatch latch = new CountDownLatch(10); LoopResources loop = LoopResources.create("testTcpConfiguration"); ChannelGroup group = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); doTestTcpConfiguration( HttpServer.create().tcpConfiguration(tcp -> configureTcpServer(tcp, loop, group, latch)), HttpClient.create().tcpConfiguration(tcp -> configureTcpClient(tcp, loop, group, latch)) ); assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue(); FutureMono.from(group.close()) .then(loop.disposeLater()) .block(Duration.ofSeconds(30)); }
Example #5
Source File: SampleServerStartup.java From Sentinel with Apache License 2.0 | 6 votes |
@Override protected Map<Integer, ChannelInitializer> choosePortsAndChannels(ChannelGroup clientChannels) { Map<Integer, ChannelInitializer> portsToChannels = new HashMap<>(); int port = new DynamicIntProperty("zuul.server.port.main", 8085).get(); String mainPortName = "main"; ChannelConfig channelConfig = BaseServerStartup.defaultChannelConfig(mainPortName); ServerSslConfig sslConfig; /* These settings may need to be tweaked depending if you're running behind an ELB HTTP listener, TCP listener, * or directly on the internet. */ ChannelConfig channelDependencies = defaultChannelDependencies(mainPortName); channelConfig.set(CommonChannelConfigKeys.allowProxyHeadersWhen, StripUntrustedProxyHeadersHandler.AllowWhen.ALWAYS); channelConfig.set(CommonChannelConfigKeys.preferProxyProtocolForClientIp, false); channelConfig.set(CommonChannelConfigKeys.isSSlFromIntermediary, false); channelConfig.set(CommonChannelConfigKeys.withProxyProtocol, false); portsToChannels.put(port, new ZuulServerChannelInitializer(port, channelConfig, channelDependencies, clientChannels)); logPortConfigured(port, null); return portsToChannels; }
Example #6
Source File: NettyClient.java From krpc with Apache License 2.0 | 6 votes |
public void close() { if (workerGroup != null) { log.info("stopping netty client"); timer.stop(); timer = null; ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); for (Object ch : conns.values()) { if (ch != null && ch != dummyChannel) allChannels.add((Channel) ch); } ChannelGroupFuture future = allChannels.close(); future.awaitUninterruptibly(); workerGroup.shutdownGracefully(); workerGroup = null; log.info("netty client stopped"); } }
Example #7
Source File: NettyServer.java From krpc with Apache License 2.0 | 6 votes |
public void close() { if (workerGroup != null) { log.info("stopping netty server"); bossGroup.shutdownGracefully(); bossGroup = null; ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); allChannels.add(serverChannel); for (Channel ch : conns.values()) { allChannels.add(ch); } ChannelGroupFuture future = allChannels.close(); future.awaitUninterruptibly(); workerGroup.shutdownGracefully(); workerGroup = null; log.info("netty server stopped"); } }
Example #8
Source File: SelfCheckHttpServer.java From krpc with Apache License 2.0 | 6 votes |
public void close() { if (workerGroup != null) { log.info("stopping selfcheck server"); bossGroup.shutdownGracefully(); bossGroup = null; ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); allChannels.add(serverChannel); for (Channel ch : conns.values()) { allChannels.add(ch); } ChannelGroupFuture future = allChannels.close(); future.awaitUninterruptibly(); workerGroup.shutdownGracefully(); workerGroup = null; log.info("selfcheck server stopped"); } }
Example #9
Source File: CatNettyClient.java From krpc with Apache License 2.0 | 6 votes |
public void close() { if (workerGroup != null) { log.info("cat stopping netty client"); timer.cancel(); timer = null; ChannelGroup allChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); for (Object ch : conns.values()) { if (ch != null && ch != dummyChannel) allChannels.add((Channel) ch); } ChannelGroupFuture future = allChannels.close(); future.awaitUninterruptibly(); workerGroup.shutdownGracefully(); workerGroup = null; log.info("cat netty client stopped"); } }
Example #10
Source File: MetricBatcher.java From gruffalo with Apache License 2.0 | 6 votes |
public MetricBatcher(final MetricFactory metricFactory, final int batchBufferCapacity, final ChannelGroup activeChannels, final int maxChannelIdleTime) { Preconditions.checkArgument(maxChannelIdleTime > 0, "maxChannelIdleTime must be greater than 0"); this.maxChannelIdleTime = maxChannelIdleTime; Preconditions.checkNotNull(metricFactory, "metricFactory may not be null"); this.batchBufferCapacity = batchBufferCapacity; this.activeChannels = Preconditions.checkNotNull(activeChannels, "activeChannels must not be null"); prepareNewBatch(); final String component = getClass().getSimpleName(); connectionCounter = metricFactory.createCounter(component, "connections"); metricsCounter = metricFactory.createCounter(component, "metricsReceived"); unexpectedErrorCounter = metricFactory.createCounter(component, "unexpectedErrors"); ioErrorCounter = metricFactory.createCounter(component, "ioErrors"); idleChannelsClosed = metricFactory.createCounter(component, "idleChannelsClosed"); metricSize = metricFactory.createHistogram(component, "metricSize", false); try { metricFactory.registerGauge(component, "batchSize", new Gauge<Integer>() { @Override public Integer getValue() { return lastBatchSize.get(); } }); } catch (IllegalArgumentException e) { // ignore metric already exists } }
Example #11
Source File: BaseZuulChannelInitializer.java From zuul with Apache License 2.0 | 5 votes |
/** * After calling this method, child classes should not reference {@link #port} any more. */ protected BaseZuulChannelInitializer( String metricId, ChannelConfig channelConfig, ChannelConfig channelDependencies, ChannelGroup channels) { this(-1, metricId, channelConfig, channelDependencies, channels); }
Example #12
Source File: JoinGroupRequestHandler.java From netty-learning-example with Apache License 2.0 | 5 votes |
@Override protected void channelRead0(ChannelHandlerContext ctx, JoinGroupRequestPacket requestPacket) { // 1. 获取群对应的 channelGroup,然后将当前用户的 channel 添加进去 String groupId = requestPacket.getGroupId(); ChannelGroup channelGroup = SessionUtil.getChannelGroup(groupId); channelGroup.add(ctx.channel()); // 2. 构造加群响应发送给客户端 JoinGroupResponsePacket responsePacket = new JoinGroupResponsePacket(); responsePacket.setSuccess(true); responsePacket.setGroupId(groupId); ctx.writeAndFlush(responsePacket); }
Example #13
Source File: HttpServerTests.java From reactor-netty with Apache License 2.0 | 5 votes |
private TcpClient configureTcpClient(TcpClient tcp, LoopResources loop, ChannelGroup group, CountDownLatch latch) { return tcp.wiretap(true) .runOn(loop) .channelGroup(group) .doOnConnected(c -> latch.countDown()) .doOnDisconnected(c -> latch.countDown()) .noSSL() .noProxy() .remoteAddress(() -> disposableServer.address()) .attr(AttributeKey.valueOf("testTcpConfiguration"), "testTcpConfiguration") .option(ChannelOption.valueOf("testTcpConfiguration"), "testTcpConfiguration") .observe((conn, state) -> latch.countDown()) .doOnChannelInit((observer, channel, address) -> latch.countDown()); }
Example #14
Source File: ThreadPerChannelEventLoopGroupTest.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
private static void runTest(ThreadPerChannelEventLoopGroup loopGroup) throws InterruptedException { int taskCount = 100; EventExecutor testExecutor = new TestEventExecutor(); ChannelGroup channelGroup = new DefaultChannelGroup(testExecutor); while (taskCount-- > 0) { Channel channel = new EmbeddedChannel(NOOP_HANDLER); loopGroup.register(new DefaultChannelPromise(channel, testExecutor)); channelGroup.add(channel); } channelGroup.close().sync(); loopGroup.shutdownGracefully(100, 200, TimeUnit.MILLISECONDS).sync(); assertTrue(loopGroup.isTerminated()); }
Example #15
Source File: BaseServerStartup.java From zuul with Apache License 2.0 | 5 votes |
@ForOverride protected Map<SocketAddress, ChannelInitializer<?>> chooseAddrsAndChannels(ChannelGroup clientChannels) { @SuppressWarnings("unchecked") // Channel init map has the wrong generics and we can't fix without api breakage. Map<Integer, ChannelInitializer<?>> portMap = (Map<Integer, ChannelInitializer<?>>) (Map) choosePortsAndChannels(clientChannels); return Server.convertPortMap(portMap); }
Example #16
Source File: ThreadPerChannelEventLoopGroupTest.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
private static void runTest(ThreadPerChannelEventLoopGroup loopGroup) throws InterruptedException { int taskCount = 100; EventExecutor testExecutor = new TestEventExecutor(); ChannelGroup channelGroup = new DefaultChannelGroup(testExecutor); while (taskCount-- > 0) { Channel channel = new EmbeddedChannel(NOOP_HANDLER); loopGroup.register(channel, new DefaultChannelPromise(channel, testExecutor)); channelGroup.add(channel); } channelGroup.close().sync(); loopGroup.shutdownGracefully(100, 200, TimeUnit.MILLISECONDS).sync(); assertTrue(loopGroup.isTerminated()); }
Example #17
Source File: RedisChannelInitializer.java From redisson with Apache License 2.0 | 5 votes |
public RedisChannelInitializer(Bootstrap bootstrap, RedisClientConfig config, RedisClient redisClient, ChannelGroup channels, Type type) { super(); this.config = config; this.redisClient = redisClient; this.type = type; if (config.getPingConnectionInterval() > 0) { pingConnectionHandler = new PingConnectionHandler(config); } else { pingConnectionHandler = null; } connectionWatchdog = new ConnectionWatchdog(bootstrap, channels, config.getTimer()); }
Example #18
Source File: ActiveMQChannelHandler.java From activemq-artemis with Apache License 2.0 | 5 votes |
protected ActiveMQChannelHandler(final ChannelGroup group, final BufferHandler handler, final BaseConnectionLifeCycleListener<?> listener, final Executor listenerExecutor) { this.group = group; this.handler = handler; this.listener = listener; this.listenerExecutor = listenerExecutor; }
Example #19
Source File: NettyServer.java From ob1k with Apache License 2.0 | 5 votes |
public NettyServer(final int port, final ServiceRegistry registry, final StaticPathResolver staticResolver, final ChannelGroup activeChannels, final String contextPath, final String applicationName, final boolean acceptKeepAlive, final long idleTimeoutMs, final boolean supportZip, final MetricFactory metricFactory, final int maxContentLength, final long requestTimeoutMs, final CorsConfig corsConfig) { System.setProperty("com.outbrain.web.context.path", contextPath); this.port = port; this.staticResolver = staticResolver; this.activeChannels = activeChannels; this.contextPath = contextPath; this.applicationName = applicationName; this.marshallerRegistry = registry.getMarshallerRegistry(); this.dispatcher = new ServiceDispatcher(registry, marshallerRegistry); this.nioGroup = new NioEventLoopGroup(); this.acceptKeepAlive = acceptKeepAlive; this.supportZip = supportZip; this.maxContentLength = maxContentLength; this.requestTimeoutMs = requestTimeoutMs; this.idleTimeoutMs = idleTimeoutMs; this.corsConfig = corsConfig; registry.logRegisteredEndpoints(); this.internalErrors = metricFactory.createCounter("Ob1kDispatcher", "internalErrors"); this.requestTimeoutErrors = metricFactory.createCounter("Ob1kDispatcher", "requestTimeoutErrors"); this.notFoundErrors = metricFactory.createCounter("Ob1kDispatcher", "notFoundErrors"); this.unexpectedErrors = metricFactory.createCounter("Ob1kDispatcher", "unexpectedErrors"); this.ioErrors = metricFactory.createCounter("Ob1kDispatcher", "ioErrors"); metricFactory.registerGauge("Ob1kDispatcher", "currentConnections", activeChannels::size); }
Example #20
Source File: ChannelsHolder.java From leo-im-server with Apache License 2.0 | 5 votes |
/** * 将channel添加到GroupChannel中 * @param groupId * @param channel */ public static void addChannelToGroup(String groupId, Channel channel) { DefaultChannelGroup channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE); ChannelGroup returnChannelGroup = CHANNEL_GROUPS.putIfAbsent(groupId, channelGroup); if(returnChannelGroup == null) { // 不存在该ChannelGroup,第一次添加。 channelGroup.add(channel); return; } // ChannelGroup已经存在 returnChannelGroup.add(channel); }
Example #21
Source File: TextWebSocketFrameHandler.java From leo-im-server with Apache License 2.0 | 5 votes |
/** * 从GroupChannel中删除channel * @param message * @param channel */ private void removeChannelFromGroupChannel(JSONObject message, Channel channel) { String imChannelId = message.getString("channelId"); ChannelGroup cg = ChannelsHolder.getChannelGroups().get(imChannelId); if(cg != null) { cg.remove(channel); } }
Example #22
Source File: ChannelGroupItem.java From nano-framework with Apache License 2.0 | 5 votes |
public ChannelGroup getGroup() { ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE); if(!CollectionUtils.isEmpty(items)) { items.forEach((itemId, channel) -> channelGroup.add(channel)); } return channelGroup; }
Example #23
Source File: ContextManager.java From nemo with Apache License 2.0 | 5 votes |
/** * Creates context manager for this channel. * @param blockManagerWorker provides handler for new contexts by remote executors * @param byteTransfer provides channel caching * @param channelGroup to cleanup this channel when closing {@link ByteTransport} * @param localExecutorId local executor id * @param channel the {@link Channel} to manage */ ContextManager(final BlockManagerWorker blockManagerWorker, final ByteTransfer byteTransfer, final ChannelGroup channelGroup, final String localExecutorId, final Channel channel) { this.blockManagerWorker = blockManagerWorker; this.byteTransfer = byteTransfer; this.channelGroup = channelGroup; this.localExecutorId = localExecutorId; this.channel = channel; }
Example #24
Source File: ClientTransportConfig.java From reactor-netty with Apache License 2.0 | 5 votes |
ClientTransportDoOn(@Nullable ChannelGroup channelGroup, @Nullable Consumer<? super Connection> doOnConnected, @Nullable Consumer<? super Connection> doOnDisconnected) { this.channelGroup = channelGroup; this.doOnConnected = doOnConnected; this.doOnDisconnected = doOnDisconnected; }
Example #25
Source File: BaseZuulChannelInitializer.java From zuul with Apache License 2.0 | 5 votes |
/** * Call {@link #BaseZuulChannelInitializer(String, ChannelConfig, ChannelConfig, ChannelGroup)} instead. */ @Deprecated protected BaseZuulChannelInitializer( int port, ChannelConfig channelConfig, ChannelConfig channelDependencies, ChannelGroup channels) { this(port, String.valueOf(port), channelConfig, channelDependencies, channels); }
Example #26
Source File: Throttler.java From gruffalo with Apache License 2.0 | 5 votes |
public Throttler(final ChannelGroup activeServerChannels, MetricFactory metricFactory) { this.activeServerChannels = Preconditions.checkNotNull(activeServerChannels, "activeServerChannels must not be null"); Preconditions.checkNotNull(metricFactory, "metricFactory must not be null"); metricFactory.registerGauge(getClass().getSimpleName(), "autoread", new Gauge<Integer>() { @Override public Integer getValue() { return serverChannel == null || !serverChannel.config().isAutoRead() ? 0 : 1; } }); }
Example #27
Source File: HttpRequestDispatcherHandler.java From ob1k with Apache License 2.0 | 5 votes |
HttpRequestDispatcherHandler(final String contextPath, final ServiceDispatcher dispatcher, final StaticPathResolver staticResolver, final RequestMarshallerRegistry marshallerRegistry, final ChannelGroup activeChannels, final boolean acceptKeepAlive, final long requestTimeoutMs, final Counter internalErrors, final Counter requestTimeoutErrors, final Counter notFoundErrors, final Counter unexpectedErrors, final Counter ioErrors ) { this.dispatcher = dispatcher; this.staticResolver = staticResolver; this.contextPath = contextPath; this.marshallerRegistry = marshallerRegistry; this.activeChannels = activeChannels; this.acceptKeepAlive = acceptKeepAlive; this.requestTimeoutMs = requestTimeoutMs; this.internalErrors = internalErrors; this.requestTimeoutErrors = requestTimeoutErrors; this.notFoundErrors = notFoundErrors; this.unexpectedErrors = unexpectedErrors; this.ioErrors = ioErrors; }
Example #28
Source File: IpCameraHandler.java From IpCamera with Eclipse Public License 2.0 | 5 votes |
public void sendMjpegFrame(byte[] jpg, ChannelGroup channelGroup) { final String BOUNDARY = "thisMjpegStream"; ByteBuf imageByteBuf = Unpooled.copiedBuffer(jpg); int length = imageByteBuf.readableBytes(); String header = "--" + BOUNDARY + "\r\n" + "content-type: image/jpeg" + "\r\n" + "content-length: " + length + "\r\n\r\n"; ByteBuf headerBbuf = Unpooled.copiedBuffer(header, 0, header.length(), StandardCharsets.UTF_8); ByteBuf footerBbuf = Unpooled.copiedBuffer("\r\n", 0, 2, StandardCharsets.UTF_8); streamToGroup(headerBbuf, channelGroup, false); streamToGroup(imageByteBuf, channelGroup, false); streamToGroup(footerBbuf, channelGroup, true); }
Example #29
Source File: BoundNode.java From simulacron with Apache License 2.0 | 5 votes |
private static CompletableFuture<Void> closeChannelGroup( ChannelGroup channelGroup, CloseType closeType) { switch (closeType) { case DISCONNECT: return completable(channelGroup.disconnect()); default: return CompletableFuture.allOf( channelGroup .stream() .map( c -> { CompletableFuture<Void> f; Function<SocketChannel, ChannelFuture> shutdownMethod = closeType == CloseType.SHUTDOWN_READ ? SocketChannel::shutdownInput : SocketChannel::shutdownOutput; if (c instanceof SocketChannel) { f = completable(shutdownMethod.apply((SocketChannel) c)); } else { logger.warn( "Got {} request for non-SocketChannel {}, disconnecting instead.", closeType, c); f = completable(c.disconnect()); } return f; }) .collect(Collectors.toList()) .toArray(new CompletableFuture[] {})); } }
Example #30
Source File: ChannelMediator.java From flashback with BSD 2-Clause "Simplified" License | 5 votes |
public ChannelMediator(Channel clientChannel, final ProxyModeControllerFactory proxyModeControllerFactory, final NioEventLoopGroup upstreamWorkerGroup, final int timeout, final ChannelGroup channelGroup) { _clientChannel = clientChannel; _proxyModeControllerFactory = proxyModeControllerFactory; _upstreamWorkerGroup = upstreamWorkerGroup; _serverConnectionIdleTimeoutMsec = timeout; _allChannelGroup = channelGroup; }