Pat R
Pat R

Reputation: 186

Netty - Share an Attachment Between Channels

I'm looking to share an object between approximately 20 channels in Netty via attachment. ex..

SharedStatsObj sso ...
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
ChannelHandlerContext c = future.getChannel().getPipeline().getContext("handler")
c.setAttachment(sso);

This object will keep statistics like commands being sent, how long we have been connected, and will also be used to signal individual channels to preform specific actions. Does anyone have any experience with this? Are there any threading concerns other than using synchronized blocks when updating data / accessing data? Any advice would be greatly appreciated!

Upvotes: 0

Views: 423

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

If you want to share an Object between channels then inject it directly into the ChannelHandler or use a static ChannelLocal instance.

Be sure that your object is thread-safe!

Upvotes: 1

Related Questions