I am coding a Java EE application that provides REST services via JAX-RS resource classes, the application makes extensive use of CDI. My resource classes are @RequestScoped, the application returns HTTP Status 202 Accepted to the caller and executes the work asynchronously.
I would like to return a "process ID" so the caller can periodically request the overall status of the long running "process" that I am executing on their behalf.
While processing these requests my application also interacts asynchronously with other systems via REST by supplying a "callback URL" that these remote servers POST back (along with results and status) to my application when the assigned task is complete.
I am looking for a way for all the beans involved in the execution of a "process" to share a common status for that "process id". My initial thought was to inject a status bean with a new @ProcessScoped CDI scope to form a common bean pool for any given process.
The crux of my problem is where to store these beans and how to group them? It seems like a chicken and egg situation because CDI is going to need to know the process id for any given bean in order to pool them together.
I have read articles on how CDI does this for it's standard scopes; and I have looked at sample code for creating custom CDI scopes. For example, @SessionContext uses the HttpSession from the Servlet specification to store the bean instances. Because our "process" is somewhat arbitrary, I am struggling to come up with something that is common that I can use to index or store these beans.
Ideally the @ProcessScope would work transparently to the application code, i.e. the process id can be queried but is created and maintained by the CDI extension alone.
Thanks for any help or advice or inspiration you can offer.
ProcessScope
very similar to aConversationScope
which already exists? If so you could either use that or at least get some ideas of how it is implemented. When implementing it yourself you'd need aProcessContext
anyways so if you are able to detect when a process is being started and when it is finished you'd store the related beans in that context.ConversationScoped
originates from JSF but it isn't limited to it (we've been using conversations and similar scopes with struts2 etc.). But think about it: what is a process? It's some finite unit of work that is started by someone or something and which one or more processes/services/systems are involved in. Now replace one of those "systems" by the user and you basically get a conversation. Thus the concepts are very similar.