title: HK2 Operation independent from Thread Id
body: <p>I'm using HK2 <a href="https://javaee.github.io/hk2/operations.html" rel="nofollow noreferrer">Operations</a> to manage a custom scope in a Java SE application which contains a REST server component. The REST part is provided by Jersey.</p>
  <p>Jersey allows a resource to return a (possibly slow or long-running) <a href="https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/StreamingOutput.html" rel="nofollow noreferrer">StreamingOutput</a> instead of a discrete entity. As I understand, this provides a form of asynchronous behaviour handled a lower layers. What I observe is that while the streaming output is running, the thread that handled the initial Jersey request is free again to take on new Jersey requests, which require their own scope.</p>
  <p>HK2 says:</p>
  <blockquote>
  <p>HK2 Operations provide a convenient set of tools for building scopes/context pairs that follow the general rule of “one operation on a thread at a time.”</p>
  </blockquote>
  <p>Unfortunately, this is where it collides. When an incoming request is routed to the same thread that is already/still serving a <code>StreamingOutput</code> to somewhere else, the operation cannot be started :</p>
  <pre><code>java.lang.IllegalStateException: The operation OperationHandleImpl(OperationIdentifier(326193,my.TransactionScope),254339200) is active on 62
  </code></pre>
  <p>Any tips on what to do?</p>