How do I implement @SessionScoped in a Netty based TCP server? Creating Custom Scopes is documented in Guice manual, but it seems that the solution only works for thread based and not asynchronous IO servers.
Is it enough to create the Channel Pipeline between scope.enter() and scope.exit()?
Disclaimer : this answer is for Netty 3. I’ve not had the opportunity to try Netty 4 yet, so I don’t know if what follows can be applied to the newer version.
Netty is asynchronous on the network side, but unless you explicity submit tasks to Executors, or change threads by any other means, the handling of
ChannelEvents by theChannelHandlers on a pipeline is synchronous and sequential. For instance, if you use Netty 3 and have anExecutionHandleron the pipeline, the scope handler should be upstream of theExecutionHandler; for Netty 4, see Trustin Lee’s comment.Thus, you can put a handler near the beginning of your pipeline that manages the session scope, for example:
A couple of quick remarks:
WriteCompletionEventandExceptionEventwhich the framework will put at the downstream end of the pipeline during event processing and wil cause reentrancy issues if not excluded. In our application, we use this kind of handler but actually only considerUpstreamMessageEvents.Throwables and fire an ExceptionEvent, but it feels more idiomatic this way.HTH