Hi Thomas,
I think it's a good idea. Weld has a similar API [1], except it's
focused solely on the "thread-local-based context". We are also
considering the use of an interceptor to active/deactivate the
ThreadContext per business method invocation [2].
I wonder, whether this should be standardized in CDI 2.0. It seems to me
that CDI SE lacks some built-in contexts. Unlike Java EE where the
built-in scopes align with the lifecycle of EE components
(@RequestScoped, @SessionScoped, etc.).
Martin
[1]
http://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#_thread_context
[2]
https://issues.jboss.org/browse/WELD-1905
Dne 10.9.2015 v 18:02 Thomas Andraschko napsal(a):
Hi,
it's often required to manual activate scopes in a async threads/runnables
if you don't use JavaEE 7.
I already used this in 3-4 projects in the last 3 years.
My current API looks like:
CdiManagedRunnable - A abstract class with implements Runnable and starts
the RequestScoped via ContextControl and a new scope called ThreadScope
RequestScoped is optional, CdiManagedRunnable#isRequestScopedSupported
return false per default and can be overwritten by the user.
ThreadContext - A AbstractContext implementation which a static static
ThreadLocal<ContextualStorage>
ThreadContextExtension
ThreadScoped
WDYT? Is this useful for other people too?
Regards,
Thomas
--
Martin Kouba
Software Engineer
Red Hat, Czech Republic