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);
}
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);
}
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();
}
}
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());
});
}
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);
}
Aggregations