I'm using Netty to write a simple server application. It will accept long-running socket connections (i.e. telnet/ssh)... receiving string-based commands and sending string-based responses.
I need to track some session state, about the particular entity on the client end of that socket connection. I'm unclear on how to go about this.
A channel handler, through the ChannelHandlerContext
objects passed to its channelRead(...)
method, can access "attributes". However, this appears to be for setting state at the channel handler level, not the per socket connection level. The Javadocs for AttributeMap
clearly state: "Be aware that it is not be possible [sic] to have multiple keys with the same name". Most of the examples out there use this for simple "counter" illustrations.
There was a ChannelLocal
class in earlier versions of Netty, that might have been relevant. But it has been removed as of Netty 4.x, and I think that it too was for handler-level state rather than connection-level state.
IS there a way have per-connection state, while still using the @Sharable
annotation on your channel handler class, and taking full advantage of Netty's non-blocking benefits? Or is the approach for this use case simply to place instance variables on your channel handler, drop the @Shareable
annotation, and spawn a new connection handler (and therefore new thread?) for each incoming connection?