How to raise ReadTiemoutException when not receive the response in 2 seconds after sending the request to Server
Binh Duong
binhduongthanh at yahoo.com
Fri May 14 06:57:12 EDT 2010
I have updated the PingHandler to add the ReadTimeoutHandler before sending
the message and remove the ReadTimeoutHandler right after received the
message or received the ReadTimeoutException and noticed that the
ReadTimeoutHandler is never called because the method
ReadTimeoutHandler.initialize() is not called.
Then I manually call the method ReadTimeoutHandler.beforeAdd(ctx) to make
the initialize() called and I noticed that the ReadTimeoutTask is run but I
got the NullPointerException at the line 192 (if
(!ctx.getChannel().isOpen())).
Please help me review the code below and tell me what's wrong with the code:
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
PingPong pptmp = (PingPong) e.getMessage();
logger.log(Level.INFO, "messageReceived=" + pptmp.toString());
if (pptmp != null) {
pp = pptmp;
isPing.incrementAndGet();
//
removeReadTimeoutHandler();
generatePingTraffic(e);
}
}
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
{
if (e.getCause() instanceof IOException) {
logger.log(Level.WARNING, "IOException from downstream.");
} else {
logger.log(Level.WARNING, "Unexpected exception from
downstream.",
e.getCause());
}
// Offer default object
//answer.offer(pp);
//Channels.close(e.getChannel());
//
removeReadTimeoutHandler();
generatePingTraffic(e);
}
private void generatePingTraffic(ExceptionEvent e) {
logger.info("Begin generatePingTraffic in case read time out");
Channel channel = e.getChannel();
sendPingTraffic(channel);
logger.info("End generatePingTraffic in case read time out");
}
private void sendPingTraffic(Channel channel) {
if ((channel.getInterestOps() & Channel.OP_WRITE) == 0) {
.....
//
addReadTimeOutHandler();
//
Channels.write(channel, sendpp);
}
}
private void addReadTimeOutHandler() {
logger.log(Level.INFO, "Add read timeout handler");
//
ReadTimeoutHandler timeout = new ReadTimeoutHandler(timer,
READ_TIMEOUT);
bootstrap.getPipeline().addBefore("handler", "timeout", timeout);
try {
//timeout.beforeAdd(ctx);
timeout.beforeAdd(bootstrap.getPipeline().getContext("timeout"));
} catch (Exception e) {
e.printStackTrace();
}
}
private void removeReadTimeoutHandler() {
logger.log(Level.INFO, "Remove read timeout handler");
try {
bootstrap.getPipeline().remove("timeout");
} catch (RuntimeException e) {
logger.info(e.getMessage());
}
}
Please help me, I am almost lost :-)
Binh
--
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/How-to-raise-ReadTiemoutException-when-not-receive-the-response-in-2-seconds-after-sending-the-requer-tp5040649p5050423.html
Sent from the Netty User Group mailing list archive at Nabble.com.
More information about the netty-users
mailing list