服务器处理程序。对于每条收到的消息,都会有多个响应。业务逻辑涉及将请求放入队列、从队列中删除、处理请求和响应。
我如何异步处理请求,异步响应,同时保持队列的完整性?
下面的代码是同步运行的。
public class TestHandler extends SimpleChannelHandler {
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
// Send greeting for a new connection.
e.getChannel().write(
"Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n");
e.getChannel().write("It is " + new Date() + " now.\r\n");
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
String XMLFromClient = (String) e.getMessage() ;
ReadXML2 rx = new ReadXML2();
String response = null;
response = rx.processInput(XMLFromClient);
ChannelFuture future = e.getChannel().write(response);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
e.getCause().printStackTrace();
Channel ch = e.getChannel();
ch.close();
}
}