java - Migrating sendUpstream in Netty 4 -


i'm migrating netty 3 netty 4. have pipeline handler acts classic filter, intercepting/handling noncompliant messages on way, , shoveling compliant ones upstream.

based on documentation (http://netty.io/wiki/new-and-noteworthy.html), expected use ctx.fireinboundbufferupdated() in lieu of ctx.sendupstream() relay inbound. however, i've found doesn't work, channelhandlerutil.addtonextinboundbuffer() does. i'd love guidance to:

  1. my confusion on current docs assertion ctx.sendupstream -> ctx.fireinboundbufferupdated and,
  2. what best practice in case, if different i've done below.

the code:

//the pipeline  public class serverinitializer extends channelinitializer<socketchannel> {   @override  public void initchannel(socketchannel ch) throws exception {      channelpipeline p = ch.pipeline();      p.addlast("decoder", new httprequestdecoder());      p.addlast("encoder", new httpresponseencoder());      p.addlast("inbound", inboundhttprequestfilter.instance);      p.addlast("handler", handlerclass.newinstance());   } }  //the filter public class inboundhttprequestfilter extends         channelinboundmessagehandleradapter<object> {      @override     public void messagereceived(channelhandlercontext ctx, object msg)             throws exception {         ... discard/handle necessary …;         //ctx.fireinboundbufferupdated(); - doesn't propagate upstream         channelhandlerutil.addtonextinboundbuffer(ctx, msg); // sends upstream     } } 

try :

ctx.nextinboundmessagebuffer().add(msg) 

javadoc :

interface channelhandlercontext messagebuf<object>  nextinboundmessagebuffer()     return messagebuf of next channelinboundmessagehandler in pipeline. 

netty 4 multiple handler example :

multihandlerserver.java

import io.netty.bootstrap.serverbootstrap; import io.netty.channel.channelfuture; import io.netty.channel.channelinitializer; import io.netty.channel.nio.nioeventloopgroup; import io.netty.channel.socket.socketchannel; import io.netty.channel.socket.nio.nioserversocketchannel; import io.netty.handler.codec.linebasedframedecoder; import io.netty.handler.codec.string.stringdecoder; import org.slf4j.logger; import org.slf4j.loggerfactory;  import java.nio.charset.charset;  public class multihandlerserver {     private static final logger logger = loggerfactory.getlogger(multihandlerserver.class);      final int port;      public multihandlerserver(final int port) {         this.port = port;     }      public void run() throws interruptedexception {         final nioeventloopgroup bossgroup = new nioeventloopgroup();         final nioeventloopgroup workergroup = new nioeventloopgroup();         try {              final serverbootstrap serverbootstrap = new serverbootstrap()                     .group(bossgroup, workergroup)                     .channel(nioserversocketchannel.class)                     .childhandler(new channelinitializer<socketchannel>() {                         @override                         protected void initchannel(socketchannel ch) throws exception {                             ch.pipeline().addlast(                                     new linebasedframedecoder(8192),                                     new stringdecoder(charset.forname("utf-8")),                                     new multihandler01(), new multihandler02());                         }                     });              final channelfuture future = serverbootstrap.bind(port).sync();             future.channel().closefuture().sync();         } {             bossgroup.shutdowngracefully();             workergroup.shutdowngracefully();         }     }      public static void main(string[] args) throws interruptedexception {         final multihandlerserver client = new multihandlerserver(8080);         client.run();     } } 

multihandler01.java

import io.netty.channel.channelhandlercontext; import io.netty.channel.channelinboundmessagehandleradapter; import org.slf4j.logger; import org.slf4j.loggerfactory;  /**  */ class multihandler01 extends channelinboundmessagehandleradapter<string> {     private logger logger = loggerfactory.getlogger(multihandler01.class);      multihandler01() {     }      @override     public void messagereceived(channelhandlercontext ctx, string msg) throws exception {         logger.info(string.format("handler01 receive message: %s", msg));         ctx.nextinboundmessagebuffer().add(msg);         ctx.fireinboundbufferupdated();     }      @override     public void exceptioncaught(channelhandlercontext ctx, throwable cause) throws exception {         logger.error("exception caught: %s", ctx.channel().remoteaddress(), cause);         ctx.close();     } } 

multihandler02.java

import io.netty.channel.channelhandlercontext; import io.netty.channel.channelinboundmessagehandleradapter; import org.slf4j.logger; import org.slf4j.loggerfactory;  /**  */ class multihandler02 extends channelinboundmessagehandleradapter<string> {     private logger logger = loggerfactory.getlogger(multihandler02.class);      multihandler02() {     }      @override     public void messagereceived(channelhandlercontext ctx, string msg) throws exception {         logger.info(string.format("handler02 receive message: %s", msg));     }      @override     public void exceptioncaught(channelhandlercontext ctx, throwable cause) throws exception {         logger.error("exception caught: %s", ctx.channel().remoteaddress(), cause);         ctx.close();     } } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -