best way to attach metadata to a channel

Steve Coughlan shadders.del at gmail.com
Wed Sep 21 22:36:39 EDT 2011


I'm building a proxy for a binary protocol that will multiple chanels
on either side.  Each channel can be obtained either by a server or a
client and once they are obtained I will treat them identically.

I need to attach metadata to each channel that is easily accessable
from the handler.messageReceived(ChannelHandlerContext ctx,
MessageEvent e) method.  My metadata class is NodeChannel and contains
a reference to the channel itself.

I'd prefer to avoid subclassing Channel as that then ties me to a
particular transport implementation.

It would seem the logical way is use
ChannelHandlerContext.getAttachment but I am not entirely clear if
there's a one to one relationship between ChannelHandlerContext and
Channel.  If I keep a reference to ChannelHandlerContext in
NodeChannel rather than the channel can I garuntee that it's
getChannel will always return the same channel object?  And I can I
garuntee that will only ever be one instance of ChannelHandlerContext
that will return that channel?

I'd really prefer to avoid doing a map lookup with channel as key to
obtain the metadata.  Many message will simply be ignored so this
would be a serious waste of resources.

Also can someone tell me when ctx is populated with a channel.  I'm
trying to obtain a reference within channelOpen but it's returning
null at that stage.


More information about the netty-users mailing list