use of io.netty.channel.EventLoop in project netty by netty.
the class UptimeClientHandler method channelUnregistered.
@Override
public void channelUnregistered(final ChannelHandlerContext ctx) throws Exception {
println("Sleeping for: " + UptimeClient.RECONNECT_DELAY + 's');
final EventLoop loop = ctx.channel().eventLoop();
loop.schedule(new Runnable() {
@Override
public void run() {
println("Reconnecting to: " + UptimeClient.HOST + ':' + UptimeClient.PORT);
UptimeClient.connect(UptimeClient.configureBootstrap(new Bootstrap(), loop));
}
}, UptimeClient.RECONNECT_DELAY, TimeUnit.SECONDS);
}
use of io.netty.channel.EventLoop in project netty by netty.
the class NioSocketChannelTest method testChannelReRegisterRead.
private static void testChannelReRegisterRead(final boolean sameEventLoop) throws Exception {
final EventLoopGroup group = new NioEventLoopGroup(2);
final CountDownLatch latch = new CountDownLatch(1);
// Just some random bytes
byte[] bytes = new byte[1024];
PlatformDependent.threadLocalRandom().nextBytes(bytes);
Channel sc = null;
Channel cc = null;
ServerBootstrap b = new ServerBootstrap();
try {
b.group(group).channel(NioServerSocketChannel.class).childOption(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new SimpleChannelInboundHandler<ByteBuf>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) {
// We was able to read something from the Channel after reregister.
latch.countDown();
}
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
final EventLoop loop = group.next();
if (sameEventLoop) {
deregister(ctx, loop);
} else {
loop.execute(new Runnable() {
@Override
public void run() {
deregister(ctx, loop);
}
});
}
}
private void deregister(ChannelHandlerContext ctx, final EventLoop loop) {
// As soon as the channel becomes active re-register it to another
// EventLoop. After this is done we should still receive the data that
// was written to the channel.
ctx.deregister().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture cf) {
Channel channel = cf.channel();
assertNotSame(loop, channel.eventLoop());
group.next().register(channel);
}
});
}
});
}
});
sc = b.bind(0).syncUninterruptibly().channel();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class);
bootstrap.handler(new ChannelInboundHandlerAdapter());
cc = bootstrap.connect(sc.localAddress()).syncUninterruptibly().channel();
cc.writeAndFlush(Unpooled.wrappedBuffer(bytes)).syncUninterruptibly();
latch.await();
} finally {
if (cc != null) {
cc.close();
}
if (sc != null) {
sc.close();
}
group.shutdownGracefully();
}
}
use of io.netty.channel.EventLoop in project grpc-java by grpc.
the class NettyHandlerTestBase method newMockContext.
protected final ChannelHandlerContext newMockContext() {
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
when(ctx.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
EventLoop eventLoop = Mockito.mock(EventLoop.class);
when(ctx.executor()).thenReturn(eventLoop);
return ctx;
}
use of io.netty.channel.EventLoop in project camel by apache.
the class ClientModeTCPNettyServerBootstrapFactory method scheduleReconnect.
private void scheduleReconnect(final ChannelFuture channelFuture) {
final EventLoop loop = channelFuture.channel().eventLoop();
loop.schedule(new Runnable() {
@Override
public void run() {
try {
LOG.trace("Re-connecting to {} if needed", configuration.getAddress());
doReconnectIfNeeded();
} catch (Exception e) {
LOG.warn("Error during re-connect to " + configuration.getAddress() + ". Will attempt again in " + configuration.getReconnectInterval() + " millis. This exception is ignored.", e);
}
}
}, configuration.getReconnectInterval(), TimeUnit.MILLISECONDS);
}
use of io.netty.channel.EventLoop in project riposte by Nike-Inc.
the class AsyncHttpClientHelper method getCircuitBreaker.
protected Optional<CircuitBreaker<Response>> getCircuitBreaker(RequestBuilderWrapper requestBuilderWrapper) {
if (requestBuilderWrapper.disableCircuitBreaker)
return Optional.empty();
// custom one is not specified.
if (requestBuilderWrapper.customCircuitBreaker.isPresent())
return requestBuilderWrapper.customCircuitBreaker;
// No custom circuit breaker. Use the default for the given request's host.
Uri uri = Uri.create(requestBuilderWrapper.url);
String host = uri.getHost();
EventLoop nettyEventLoop = requestBuilderWrapper.getCtx() == null ? null : requestBuilderWrapper.getCtx().channel().eventLoop();
CircuitBreaker<Integer> defaultStatusCodeCircuitBreaker = getDefaultHttpStatusCodeCircuitBreakerForKey(host, Optional.ofNullable(nettyEventLoop), Optional.ofNullable(nettyEventLoop));
return Optional.of(new CircuitBreakerDelegate<>(defaultStatusCodeCircuitBreaker, response -> (response == null ? null : response.getStatusCode())));
}
Aggregations