i using netty 4.1.0.final , facing issue message not passing through outbound handler. posting sample program there 1 inbound , 1 outbound handler. inbound handler uses writeandflush in channelhandlercontext , understanding should forward message first available outbound handler available in pipeline. memory management ignored simplicity.
bootstrap code
eventloopgroup bossgroup = new nioeventloopgroup(); eventloopgroup workergroup = new nioeventloopgroup(); serverbootstrap bootstrap = new serverbootstrap(); bootstrap.group(bossgroup, workergroup) .channel(nioserversocketchannel.class) .localaddress(12021) .childhandler(new channelinitializer<socketchannel>() { @override protected void initchannel(socketchannel ch) throws exception { ch.pipeline().addlast(new testhandler1(), new testouthandler1()); } }); channelfuture future = bootstrap.bind().sync(); system.out.println("server started..."); future.channel().closefuture().sync(); bossgroup.shutdowngracefully(); workergroup.shutdowngracefully(); system.out.println("server shutdown");
inbound handler code
public class testhandler1 extends channelinboundhandleradapter { private static log logger = logfactory.getlog(testhandler1.class); @override public void channelread(channelhandlercontext ctx, object msg) throws exception { logger.info("channelread"); ctx.writeandflush(msg); }
}
outbound handler code
public class testouthandler1 extends channeloutboundhandleradapter { private static log logger = logfactory.getlog(testouthandler1.class); @override public void write(channelhandlercontext ctx, object msg, channelpromise promise) throws exception { logger.info("write"); ctx.writeandflush(msg); }
}
output
info testhandler1: channelread
if change inbound handler below doing writeandflush() on channel instead of channelhandlercontext, getting expected output
modified inbound hanndler
public class testhandler1 extends channelinboundhandleradapter { private static log logger = logfactory.getlog(testhandler1.class); @override public void channelread(channelhandlercontext ctx, object msg) throws exception { logger.info("channelread"); ctx.channel().writeandflush(msg); }
}
output
info testhandler1: channelread
info testouthandler1:write
as per norman's explanation in below link, understand channelhandlercontext.write(...) should pass through channeloutboundhandlers in front of it, in case outbound handler.
let me know if understanding wrong or missing anything.
its because add channeloutboundhandler
before channelinboundhandler
channelpipeline.
channelhandlercontext.writeandflush(...)
start on point in channelpipeline
channelhandler
added. add channeloutboundhandler
before work expected.
Comments
Post a Comment