Search in sources :

Example 76 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project reactor-netty by reactor.

the class TcpResourcesTest method before.

@Before
public void before() {
    loopDisposed = new AtomicBoolean();
    poolDisposed = new AtomicBoolean();
    loopResources = new LoopResources() {

        @Override
        public EventLoopGroup onServer(boolean useNative) {
            return null;
        }

        @Override
        public Mono<Void> disposeLater() {
            return Mono.<Void>empty().doOnSuccess(c -> loopDisposed.set(true));
        }

        @Override
        public boolean isDisposed() {
            return loopDisposed.get();
        }
    };
    poolResources = new PoolResources() {

        @Override
        public ChannelPool selectOrCreate(SocketAddress address, Supplier<? extends Bootstrap> bootstrap, Consumer<? super Channel> onChannelCreate, EventLoopGroup group) {
            return null;
        }

        public Mono<Void> disposeLater() {
            return Mono.<Void>empty().doOnSuccess(c -> poolDisposed.set(true));
        }

        @Override
        public boolean isDisposed() {
            return poolDisposed.get();
        }
    };
    tcpResources = new TcpResources(loopResources, poolResources);
}
Also used : EventLoopGroup(io.netty.channel.EventLoopGroup) SocketAddress(java.net.SocketAddress) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) PoolResources(reactor.ipc.netty.resources.PoolResources) InetSocketAddress(java.net.InetSocketAddress) Supplier(java.util.function.Supplier) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) Bootstrap(io.netty.bootstrap.Bootstrap) Flux(reactor.core.publisher.Flux) Duration(java.time.Duration) NettyContext(reactor.ipc.netty.NettyContext) ChannelPool(io.netty.channel.pool.ChannelPool) SocketUtils(reactor.ipc.netty.SocketUtils) LoopResources(reactor.ipc.netty.resources.LoopResources) Before(org.junit.Before) ChannelPool(io.netty.channel.pool.ChannelPool) Mono(reactor.core.publisher.Mono) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EventLoopGroup(io.netty.channel.EventLoopGroup) PoolResources(reactor.ipc.netty.resources.PoolResources) LoopResources(reactor.ipc.netty.resources.LoopResources) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Before(org.junit.Before)

Example 77 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project reactor-netty by reactor.

the class DefaultPoolResourcesTest method disposeOnlyOnce.

@Test
public void disposeOnlyOnce() {
    DefaultPoolResources.Pool pool = new DefaultPoolResources.Pool(new Bootstrap(), (b, handler, checker) -> channelPool, null, new DefaultEventLoopGroup());
    DefaultPoolResources poolResources = new DefaultPoolResources("test", (b, handler, checker) -> channelPool);
    // "register" our fake Pool
    poolResources.channelPools.put(new DefaultPoolResources.SocketAddressHolder(InetSocketAddress.createUnresolved("localhost", 80)), pool);
    poolResources.dispose();
    assertThat(closed.get()).as("pool closed by dispose()").isEqualTo(1);
    Mono<Void> disposer = poolResources.disposeLater();
    disposer.subscribe();
    poolResources.disposeLater().subscribe();
    poolResources.dispose();
    assertThat(closed.get()).as("pool closed only once").isEqualTo(1);
}
Also used : Bootstrap(io.netty.bootstrap.Bootstrap) ChannelPool(io.netty.channel.pool.ChannelPool) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Test(org.junit.Test)

Example 78 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project reactor-netty by reactor.

the class DefaultPoolResourcesTest method fixedPoolTwoAcquire.

@Test
public void fixedPoolTwoAcquire() throws ExecutionException, InterruptedException, IOException {
    final ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
    int echoServerPort = SocketUtils.findAvailableTcpPort();
    TcpClientTests.EchoServer echoServer = new TcpClientTests.EchoServer(echoServerPort);
    List<Channel> createdChannels = new ArrayList<>();
    try {
        final InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", echoServerPort);
        ChannelPool pool = PoolResources.fixed("fixedPoolTwoAcquire", 2).selectOrCreate(address, () -> new Bootstrap().remoteAddress(address).channelFactory(NioSocketChannel::new).group(new NioEventLoopGroup(2)), createdChannels::add, new NioEventLoopGroup(2));
        // fail a couple
        assertThatExceptionOfType(Throwable.class).isThrownBy(pool.acquire()::get).withMessageContaining("Connection refused");
        assertThatExceptionOfType(Throwable.class).isThrownBy(pool.acquire()::get).withMessageContaining("Connection refused");
        // start the echo server
        service.submit(echoServer);
        Thread.sleep(100);
        // acquire 2
        final Channel channel1 = pool.acquire().get();
        final Channel channel2 = pool.acquire().get();
        // make room for 1 more
        channel2.close().get();
        final Channel channel3 = pool.acquire().get();
        // next one will block until a previous one is released
        long start = System.currentTimeMillis();
        service.schedule(() -> pool.release(channel1), 500, TimeUnit.MILLISECONDS);
        final Channel channel4 = pool.acquire().get();
        long end = System.currentTimeMillis();
        assertThat(end - start).as("channel4 acquire blocked until channel1 released").isGreaterThanOrEqualTo(500);
        pool.release(channel3).get();
        pool.release(channel4).get();
        assertThat(pool).isInstanceOf(DefaultPoolResources.Pool.class);
        DefaultPoolResources.Pool defaultPool = (DefaultPoolResources.Pool) pool;
        assertThat(defaultPool.activeConnections.get()).as("activeConnections fully released").isZero();
    } finally {
        echoServer.close();
    }
}
Also used : ChannelPool(io.netty.channel.pool.ChannelPool) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TcpClientTests(reactor.ipc.netty.tcp.TcpClientTests) InetSocketAddress(java.net.InetSocketAddress) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelPool(io.netty.channel.pool.ChannelPool) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 79 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project reactor-netty by reactor.

the class UdpServer method newHandler.

@Override
public Mono<? extends NettyContext> newHandler(BiFunction<? super UdpInbound, ? super UdpOutbound, ? extends Publisher<Void>> handler) {
    final BiFunction<? super UdpInbound, ? super UdpOutbound, ? extends Publisher<Void>> targetHandler = null == handler ? ChannelOperations.noopHandler() : handler;
    return Mono.create(sink -> {
        Bootstrap b = options.get();
        SocketAddress adr = options.getAddress();
        if (adr == null) {
            sink.error(new NullPointerException("Provided UdpServerOptions do not " + "define any address to bind to "));
            return;
        }
        b.localAddress(adr);
        ContextHandler<DatagramChannel> c = doHandler(targetHandler, sink, adr);
        b.handler(c);
        c.setFuture(b.bind());
    });
}
Also used : DatagramChannel(io.netty.channel.socket.DatagramChannel) Bootstrap(io.netty.bootstrap.Bootstrap) SocketAddress(java.net.SocketAddress)

Example 80 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project openflowplugin by opendaylight.

the class SimpleClient method run.

/**
 * Starting class of {@link SimpleClient}.
 */
@Override
public void run() {
    group = new NioEventLoopGroup();
    SimpleClientInitializer clientInitializer = new SimpleClientInitializer(isOnlineFuture, securedClient);
    clientInitializer.setScenario(scenarioHandler);
    try {
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class).handler(clientInitializer);
        bootstrap.connect(host, port).sync();
        synchronized (scenarioHandler) {
            LOG.debug("WAITING FOR SCENARIO");
            while (!scenarioHandler.isScenarioFinished()) {
                scenarioHandler.wait();
            }
        }
    } catch (InterruptedException ex) {
        LOG.error(ex.getMessage(), ex);
    } finally {
        LOG.debug("shutting down");
        try {
            group.shutdownGracefully().get();
            LOG.debug("shutdown succesful");
        } catch (InterruptedException | ExecutionException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    scenarioDone.set(true);
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ExecutionException(java.util.concurrent.ExecutionException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)429 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)207 Channel (io.netty.channel.Channel)194 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)189 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)169 ChannelFuture (io.netty.channel.ChannelFuture)152 EventLoopGroup (io.netty.channel.EventLoopGroup)139 SocketChannel (io.netty.channel.socket.SocketChannel)123 InetSocketAddress (java.net.InetSocketAddress)119 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)113 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)108 Test (org.junit.jupiter.api.Test)89 ChannelPipeline (io.netty.channel.ChannelPipeline)76 LocalChannel (io.netty.channel.local.LocalChannel)74 LocalServerChannel (io.netty.channel.local.LocalServerChannel)71 LocalAddress (io.netty.channel.local.LocalAddress)66 CountDownLatch (java.util.concurrent.CountDownLatch)62 IOException (java.io.IOException)60 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)56 ChannelInitializer (io.netty.channel.ChannelInitializer)45