Netty - how to transfer information between processors in one pipeline

I would like to create a pipeline of handlers, for example:

public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( new ObjectEncoder(), new ObjectDecoder(), new AuthenticationServerHandler(), new BusinessLogicServerHandler()); } 

The key point here is that I would like the AuthenticationServerHandler to pass login information to the BusinessLogicServerHandler .

I understand that you can use Attachment , but it only stores information for this handler, other handlers in the pipeline cannot access it. I also noticed that there is something called ChannelLocal that could do the trick, however I cannot find any real information on how to use it. All I saw are people who create a static instance for it, but how do you retrieve and access information in another handler? Assuming the correct method.

My question is: how do you pass information between handlers in the same pipeline. In the example above, how do I pass login credentials from AuthenticationServerHandler to BusinessLogicServerHandler ?

+6
source share
3 answers

ChannelLocal is the path to atm. Just create a static instance somewhere and then access it from your handlers by passing the pipe to the set / get method. This way you can share things between your channels.

+2
source

I was not a fan of the ChannelLocal implementation with no internal static map, so what I ended up doing now put my object in a Channel binding:

 ctx.getChannel().setAttachment(myobj); 

Then I make "myobj" basically a POJO context that contains all the information collected by request.

 public class RequestContext { private String foo = ""; public String getFoo(){ return foo; } public void setFoo(String foo){ this.foo = foo; } } RequestContext reqCtx = new RequestContext(); reqCtx.setFoo("Bar"); ctx.getChannel().setAttachment(reqCtx); reqCtx = (RequestContext)ctx.getChannel().getAttachment(); 

It is not elegant, but it works ...

+2
source

I pass information from one handler to the next, using dedicated instances to compose the pipeline for each channel, and through the fact that the handlers reference each other in each pipeline.

The passage of information is done in the old way, very simple, without any problems.

0
source

Source: https://habr.com/ru/post/906690/


All Articles