use of io.netty.channel.group.DefaultChannelGroup in project netty by netty.
the class NioDatagramChannelTest method testBindMultiple.
/**
* Test try to reproduce issue #1335
*/
@Test
public void testBindMultiple() throws Exception {
DefaultChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
NioEventLoopGroup group = new NioEventLoopGroup();
try {
for (int i = 0; i < 100; i++) {
Bootstrap udpBootstrap = new Bootstrap();
udpBootstrap.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).handler(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
// Discard
ReferenceCountUtil.release(msg);
}
});
DatagramChannel datagramChannel = (DatagramChannel) udpBootstrap.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
channelGroup.add(datagramChannel);
}
Assert.assertEquals(100, channelGroup.size());
} finally {
channelGroup.close().sync();
group.shutdownGracefully().sync();
}
}
use of io.netty.channel.group.DefaultChannelGroup in project camel by apache.
the class NettyProducer method doStart.
@Override
protected void doStart() throws Exception {
super.doStart();
if (configuration.getWorkerGroup() == null) {
// create new pool which we should shutdown when stopping as its not shared
workerGroup = new NettyWorkerPoolBuilder().withNativeTransport(configuration.isNativeTransport()).withWorkerCount(configuration.getWorkerCount()).withName("NettyClientTCPWorker").build();
}
if (configuration.isProducerPoolEnabled()) {
// setup pool where we want an unbounded pool, which allows the pool to shrink on no demand
GenericObjectPool.Config config = new GenericObjectPool.Config();
config.maxActive = configuration.getProducerPoolMaxActive();
config.minIdle = configuration.getProducerPoolMinIdle();
config.maxIdle = configuration.getProducerPoolMaxIdle();
// we should test on borrow to ensure the channel is still valid
config.testOnBorrow = true;
// only evict channels which are no longer valid
config.testWhileIdle = true;
// run eviction every 30th second
config.timeBetweenEvictionRunsMillis = 30 * 1000L;
config.minEvictableIdleTimeMillis = configuration.getProducerPoolMinEvictableIdle();
config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_FAIL;
pool = new GenericObjectPool<ChannelFuture>(new NettyProducerPoolableObjectFactory(), config);
if (LOG.isDebugEnabled()) {
LOG.debug("Created NettyProducer pool[maxActive={}, minIdle={}, maxIdle={}, minEvictableIdleTimeMillis={}] -> {}", new Object[] { config.maxActive, config.minIdle, config.maxIdle, config.minEvictableIdleTimeMillis, pool });
}
} else {
pool = new SharedSingletonObjectPool<ChannelFuture>(new NettyProducerPoolableObjectFactory());
if (LOG.isDebugEnabled()) {
LOG.info("Created NettyProducer shared singleton pool -> {}", pool);
}
}
// setup pipeline factory
ClientInitializerFactory factory = configuration.getClientInitializerFactory();
if (factory != null) {
pipelineFactory = factory.createPipelineFactory(this);
} else {
pipelineFactory = new DefaultClientInitializerFactory(this);
}
// setup channel group
if (configuration.getChannelGroup() == null) {
allChannels = new DefaultChannelGroup("NettyProducer", ImmediateEventExecutor.INSTANCE);
} else {
allChannels = configuration.getChannelGroup();
}
if (!configuration.isLazyChannelCreation()) {
// ensure the connection can be established when we start up
ChannelFuture channelFuture = pool.borrowObject();
channelFuture.get();
pool.returnObject(channelFuture);
}
}
use of io.netty.channel.group.DefaultChannelGroup in project reactor-netty by reactor.
the class NettyOptionsTest method afterChannelInitAfterChannelGroup.
@Test
public void afterChannelInitAfterChannelGroup() {
// this test only differs from afterChannelInitThenChannelGroup in the order of the options
ChannelGroup group = new DefaultChannelGroup(null);
List<Channel> initializedChannels = new ArrayList<>();
NettyContext nettyContext = HttpServer.create(opt -> opt.channelGroup(group).afterChannelInit(initializedChannels::add)).start((req, resp) -> resp.sendNotFound()).getContext();
HttpClientResponse resp = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
assertThat((Iterable<Channel>) group).hasSize(1).hasSameElementsAs(initializedChannels).doesNotContain(nettyContext.channel());
resp.dispose();
nettyContext.dispose();
}
use of io.netty.channel.group.DefaultChannelGroup in project reactor-netty by reactor.
the class NettyOptionsTest method afterChannelInitThenChannelGroup.
@Test
public void afterChannelInitThenChannelGroup() {
ChannelGroup group = new DefaultChannelGroup(null);
List<Channel> initializedChannels = new ArrayList<>();
NettyContext nettyContext = HttpServer.create(opt -> opt.afterChannelInit(initializedChannels::add).channelGroup(group)).start((req, resp) -> resp.sendNotFound()).getContext();
HttpClientResponse resp = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
assertThat((Iterable<Channel>) group).hasSize(1).hasSameElementsAs(initializedChannels).doesNotContain(nettyContext.channel());
resp.dispose();
nettyContext.dispose();
}
use of io.netty.channel.group.DefaultChannelGroup in project reactor-netty by reactor.
the class ITTracingHttpClientDecoratorTest method newClient.
@Override
@SuppressWarnings("deprecation")
protected HttpClient newClient(int port) {
ReactorNettyHttpTracing reactorNettyHttpTracing = ReactorNettyHttpTracing.create(httpTracing, s -> null);
group = new DefaultChannelGroup(executor);
return reactorNettyHttpTracing.decorateHttpClient(HttpClient.create().host("127.0.0.1").port(port).wiretap(true).followRedirect(true).disableRetry(true).channelGroup(group));
}
Aggregations