java - Using channel attributes in different context handlers -


i'm working on application server part of based on netty 4.0. however, ran problem of shared variables channel.
have read use such variables in other context handlers need use

  • context.channel().attr(key).set()

instead of just

  • context.attr(key).set()

to attach variable current channel.
here:

 public class hadlepackets extends channelinboundhandleradapter {  private static final logger log = loggerfactory.getlogger(hadlepackets.class); public attributekey<integer> clientidattrkey = new attributekey<integer>  ("clientid");  @override public void channelread(channelhandlercontext ctx, object message) throws exception {  ... ctx.channel().attr(clientidattrkey).set(idnum); //bind id channel  ... } } 

but, how retrieve value of variable in context handler? attribute key still needed , cant grab handler attached variable.

public class storagehandler extends channelinboundhandleradapter{ private static final logger log = loggerfactory.getlogger(storagesavehandler.class);  @override public void channelinactive(channelhandlercontext ctx) throws exception {     integer clientid = (integer)ctx.channel().attr(null).get(); //how can proper key channel attribute?     ctx.firechannelinactive();  } 

this should work since public static:

@override public void channelinactive(channelhandlercontext ctx) throws exception {     integer clientid = (integer) ctx.channel().attr(handlepackets.clientidattrkey).get();     ctx.firechannelinactive(); } 

here little example of how use it. code excerpt though:

public class nettyserver implements runnable { final static attributekey<long> checksumkey = attributekey.valueof("calcchecksum"); final static attributekey<command> commandkey = attributekey.valueof("command"); final static attributekey<long> filehandlekey = attributekey.valueof("filehandle"); final static attributekey<file> pathkey = attributekey.valueof("destpath"); final static attributekey<fileoutputstream> outputstream = attributekey.valueof("stream");  @override public void run() {     try {         eventloopgroup bossgroup = new nioeventloopgroup(boss_threads);         eventloopgroup workergroup = new nioeventloopgroup(calculatethreadcount());         try {             serverbootstrap bootstrap = new serverbootstrap();              // option allow binding bound ip:port             // combination.             // used able restart server if             // crashed/got killed             // (so while socket still in time_wait state).             bootstrap.option(channeloption.so_reuseaddr, true);             bootstrap.group(bossgroup, workergroup).channel(nioserversocketchannel.class).childhandler(new channelinitializer<socketchannel>() {                 @override                 public void initchannel(socketchannel ch) throws exception {                     ch.pipeline().addlast("objectdecoder", new objectdecoder(classresolvers.cachedisabled(null)));                     ch.pipeline().addlast("objectencoder", new objectencoder());                     ch.pipeline().addlast("commandhandler", new servercommandhandler(server, logger));                     ch.pipeline().addlast("filehandler", new serverfilehandler(server, logger));                     ch.pipeline().addlast("checksumhandler", new serverchecksumhandler(server, logger));                 }             });              try {                 // bind , start accept incoming connections.                 bootstrap.bind(port).sync().channel().closefuture().sync();             } catch (interruptedexception e) {                 logger.debug(message.cmdtransfer_interrupted_debug, e.getmessage());             }         } {             bossgroup.shutdowngracefully();             workergroup.shutdowngracefully();         }     } catch (exception e) {         logger.error(message.cmdtransfer_connect_error, e.getmessage());     } } } 

as can see have several handlers share attributes of channel.

public class servercommandhandler extends channelinboundhandleradapter {     @override     public void channelread(channelhandlercontext ctx, object msg) throws exception {         ctx.channel().attr(nettyserver.commandkey).set(clcommand);         ctx.firechannelread(msg);     } }  public class serverchecksumhandler extends channelinboundhandleradapter {     @override     public void channelread(channelhandlercontext ctx, object msg) throws exception {         if ctx.channel().attr(nettyserver.commandkey).getandremove() == referencecommand {             //do         }     } } 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -