I haven't tested this yet, but I believe this will work. For each custom scope/context you want in your application, you simply need to add that context via an extension when the container is initialized:
public void afterBeanDiscovery(@Observes AfterBeanDiscover afterBeanDiscovery, BeanManager beanManager)
{
CustomContext customContext = new CustomContext();
afterBeanDiscovery.addContext(customContext);
beanManager ...
}
Now, the trick is, you need to hold a reference to that context so then when you want to start or stop it, you can. That would be something like:
@Inject
protected HttpRequestLifecycle httpRequestLifecycle;
public void doSomething()
{
startContext();
doStuff();
stopContext();
}
public void startContext()
{
httpRequestContextLifecycle.getHttpRequestContext().activate();
}
That should do it, there isn't a wealth of documentation out there, so I hope this helps.
Anyone interested, check out the source here:
http://github.com/walterjwhite/server.web.application
Walter