Custom channel identifier alternative

Frederic Bregier fredbregier at free.fr
Sun Jul 26 04:46:14 EDT 2009


Hi,

Like you said, you can use a map to match Channel Id and your business Id.
Other ways are possible, some not so different, some so and depending on
your implementations:

- Using ChannelLocal, you can store specific data for a Channel, so storing
your business Id and retrieving it from the channel. It is almost as if you
do it by hand using a map but already in Netty.

- If your handler is "unique" by channel, that is to say when the
ChannelPipeline is created you make a new instance for each channel of you
handler, using a ChannelPipelineFactory, (at least the last one handler is
newly created which is often the business one, the previous ones could be
shared among the channels if they are supposed to do so and compatible,
generaly stateless handlers), then you can add a private variable for this
handler (like a simpleChannelHandler) that will store for you the business
ID. 
So your effort will be first at startup of the connection, when the
ChannelPipelineFactory will create a new ChannelPipeline to include at the
end a new business handler.
Then in this handler, in the channelConnected method for instance or
elsewhere depending on your logic on how you assign and compute your
business Id, assign this Id to your own handler attribute.
Then you can use it in all methods, refer to it by a getter...

I personnaly prefer the second method since all my "business" handlers are
stateful so unique by channel. But if I have to do a stateless handler, I
should consider the first method.

Of course those methods are not the only ones, but that's a start...

HTH,
Frederic


fatzopilot wrote:
> 
> Hi,
> 
> I am switching from another NIO based framework to Netty.  What I like
> with the old framework is the possibility to set a custom identifier for a
> channel. I use this to have a clear mapping of my clients (which have a
> fixed and unique id) to individual channels. That way, I can easily
> identify which clients are logged in etc. by querying the channel registry
> (ChannelGroup in case of Netty). I could mimic this behaviour but it would
> mean to introduce an additional map that maps from client IDs to Netty IDs
> which would require additional program logic and make the whole thing more
> complex and error prone. Is there a better (built in) way to accomplish
> this?
> 
> Thanks
> fatzopilot
> 


-----
Hardware/Software Architect
-- 
View this message in context: http://n2.nabble.com/Custom-channel-identifier-alternative-tp3326889p3328432.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list