My Requirement:
I want to detect if the channel is idle for reading for some amount of time and want to timeout based on that. My Netty client is sending requests to 1000 servers.
Problem: My Netty Client is never showing that if there is an idle channel for some time even if I am using some IPs that will always timeout.
I doubt that I am not implementing IdleStateHandler correctly. I have tried decreasing the read timeout of IdleStateHandler but no luck. I have spent hours figuring this out. Any help will be really appreciated.
So, below is my code:
My Netty Client:
public void connect(final InetAddress remoteAddress){
new Bootstrap()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.handler(httpNettyClientChannelInitializer)
.connect(remoteAddress, serverPort)
.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
future.cancel(!future.isSuccess());
}
});
}
My Netty Channel Initalizer:
public class HttpNettyClientChannelInitializer extends ChannelInitializer<SocketChannel> {
private final Provider<HttpNettyClientChannelHandler> handlerProvider;
private final int timeout;
private int maxContentLength;
@Inject
public HttpNettyClientChannelInitializer(Provider<HttpNettyClientChannelHandler> handlerProvider,
@Named("readResponseTimeout") int timeout, @Named("maxContentLength") int maxContentLength) {
this.handlerProvider = handlerProvider;
this.timeout = timeout;
this.maxContentLength = maxContentLength;
}
@Override
protected void initChannel(SocketChannel socketChannel) {
ChannelPipeline pipelineNettyClientChannel = socketChannel.pipeline();
pipelineNettyClientChannel.addLast("codec", new HttpClientCodec());
pipelineNettyClientChannel.addLast("idleStateHandler", new IdleStateHandler(timeout,0,0, TimeUnit.MILLISECONDS));
pipelineNettyClientChannel.addLast("aggregator", new HttpObjectAggregator(maxContentLength));
//handlerProvider.get() will provide new instance of channel handler
pipelineNettyClientChannel.addLast("handler", handlerProvider.get());
}
}
My Netty Channel Handler
@ChannelHandler.Sharable
public class HttpNettyClientChannelHandler extends SimpleChannelInboundHandler<HttpObject> {
@Override
public void channelActive(ChannelHandlerContext channelHandlerContext){...}
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject){...}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent e = (IdleStateEvent) evt;
if (e.state() == IdleState.READER_IDLE) {
System.out.println("Idle channel");
ctx.close();
}
}
}
}
Some of the Not matched .jar libraries causing this kind of errors. did you check the versions and compatibilities.
https://github.com/netty/netty
-> tests -> Check the issues may be your problem declared before ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With