Search in sources :

Example 31 with DefaultChannelGroup

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();
    }
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) InetSocketAddress(java.net.InetSocketAddress) DatagramChannel(io.netty.channel.socket.DatagramChannel) NioDatagramChannel(io.netty.channel.socket.nio.NioDatagramChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 32 with DefaultChannelGroup

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);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool)

Example 33 with DefaultChannelGroup

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();
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpServer(reactor.ipc.netty.http.server.HttpServer) Channel(io.netty.channel.Channel) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 34 with DefaultChannelGroup

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();
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpServer(reactor.ipc.netty.http.server.HttpServer) Channel(io.netty.channel.Channel) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 35 with DefaultChannelGroup

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));
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup)

Aggregations

DefaultChannelGroup (io.netty.channel.group.DefaultChannelGroup)56 ChannelGroup (io.netty.channel.group.ChannelGroup)29 Channel (io.netty.channel.Channel)23 DefaultEventExecutor (io.netty.util.concurrent.DefaultEventExecutor)15 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)14 InetSocketAddress (java.net.InetSocketAddress)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 List (java.util.List)11 Test (org.junit.Test)11 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)10 ChannelOption (io.netty.channel.ChannelOption)9 Test (org.junit.jupiter.api.Test)9 ChannelInitializer (io.netty.channel.ChannelInitializer)8 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)8 Mono (reactor.core.publisher.Mono)8 ByteBuf (io.netty.buffer.ByteBuf)7 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)7 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)7 GlobalEventExecutor (io.netty.util.concurrent.GlobalEventExecutor)7 ArrayList (java.util.ArrayList)7