javajerseycdihk2

HK2 Operation independent from Thread Id


I'm using HK2 Operations to manage a custom scope in a Java SE application which contains a REST server component. The REST part is provided by Jersey.

Jersey allows a resource to return a (possibly slow or long-running) StreamingOutput 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.

HK2 says:

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.”

Unfortunately, this is where it collides. When an incoming request is routed to the same thread that is already/still serving a StreamingOutput to somewhere else, the operation cannot be started :

java.lang.IllegalStateException: The operation OperationHandleImpl(OperationIdentifier(326193,my.TransactionScope),254339200) is active on 62

Any tips on what to do?


Solution

  • Operations are gr8, but must follow the rule of "one-operation-on-a-thread-at-a-time"

    There are really only two things to do.

    1. Ask Eclipse for a feature that expands how Operations work
    2. It may be possible to use the operation api itself to detect if an operation is running... and if it is... instead fork a different thread and continue your operation there (total hack but... probably would work).