正如您将看到的,我是netty的新手。我想知道为什么在将这个类添加到管道后没有任何反应。我使用的是4.0.25.Final版本。我使用的入站处理程序很好,并且在我测试时可以正常工作。
public class SipChannelInboundOutboundHandlerAdapter implements
ChannelOutboundHandler, ChannelInboundHandler {
private Logger mainLogger = ServLogger.getMainLogger();
private Logger consoleLogger = ServLogger.getConsoleLogger();
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
AbstractResponse response;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
consoleLogger.info("channelRead");
if (msg instanceof Unauthorized)
response = (Unauthorized) msg;
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress,
ChannelPromise promise) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
SocketAddress localAddress, ChannelPromise promise)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void read(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void write(ChannelHandlerContext ctx, Object msg,
ChannelPromise promise) throws Exception {
ctx.write(response);
ctx.flush();
if(!promise.await(2000))
consoleLogger.info("not written to Outbound handler");
else
consoleLogger.info("written to Outbound handler");
}
@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
}
}
注意:记录器在入站处理程序中正常工作
是否由于未向其中一个覆盖方法添加所需的代码?
回答评论: 包含此类实例的.addLast方法永远不会返回。启动我的服务器的Junit测试(setupBeforeClass方法)就停止了。
ChannelPipeline cp = ch.pipeline();
cp.addFirst("logger", new LoggingHandler());
cp.addLast("parser", new SipSimpleChannelInboundHandlerAdapter());
cp.addLast("javaee", new SipChannelInboundHandlerAdapter());
consoleLogger.info("adding inboundoutbound");
cp.addLast("test_return_message", new SipChannelInboundOutboundHandlerAdapter());
控制台上发生的最后一件事是"添加inboundoutbound"然后我必须使用红色按钮(eclipse)停止测试。
注释掉最后一个addLast方法会使测试按预期进行。调用所有入站处理程序中的ReadComplete方法(在必要时在prevous处理程序中触发),但是当尝试添加最后一个处理程序(不是扩展适配器但同时实现ChannelInboundHandler和ChannelOutbounHandler)时,我会遇到这种情况。