Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>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 <code>ChannelEvent</code>s by the <code>ChannelHandler</code>s on a pipeline is synchronous and sequential. For instance, if you use Netty 3 and have an <code>ExecutionHandler</code> on the pipeline, the scope handler should be upstream of the <code>ExecutionHandler</code>; for Netty 4, see Trustin Lee's comment.</p> <p>Thus, you can put a handler near the beginning of your pipeline that manages the session scope, for example:</p> <pre><code>public class ScopeHandler implements ChannelUpstreamHandler { @Override public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) { if (e instanceof WriteCompletionEvent || e instanceof ExceptionEvent) ctx.sendUpstream(e); Session session = ...; // get session, presumably using e.getChannel() scope.enter(); try { scope.seed(Key.get(Session.class), session); ctx.sendUpstream(e); } finally { scope.exit(); } } private SessionScope scope; } </code></pre> <p>A couple of quick remarks:</p> <ul> <li>You will want to filter some event types out, especially <code>WriteCompletionEvent</code> and <code>ExceptionEvent</code> which 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 consider <code>UpstreamMessageEvent</code>s.</li> <li>The try/finally construct is not actually necessary as Netty will catch any <code>Throwable</code>s and fire an ExceptionEvent, but it feels more idiomatic this way.</li> </ul> <p>HTH</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload