I'm currently working on a product that we've set up to use guice-servlet.
For each request that we receive, we spawn a bunch of worker threads, each of which go off and perform a "task". The results of those tasks are then merged to produce a response.
This is pretty common/straightforward, but something I haven't attempted to marry with Guice before. Unfortunately we've got ourselves into a situation where you have to draw the whole graph in advance.
i.e. We can't do any deferred binding within a child thread, as that will try to use request/session-scoped stuff that isn't there.
A couple of questions that are interrelated:
1) It'd be really nice to be able to have access to request-scoped objects from within our task threads. I was initially tempted by (and played around with) ServletScopes.transferRequest(), but was deflated by the following:
Because request-scoped objects are not typically thread-safe, the callable returned by this method must not be run on a different thread until the current request scope has terminated. The returned callable will block until the current thread has released the request scope.
It seems like this is more for post-processing in a separate thread (i.e. do some work after you've responded) and the concerns about thread-safety are valid.
Is the Guicy™ way of dealing with this to create a child-injector with a separate module for task-only stuff and seed it with things from the request-scope that are required/immutable?
Reading this back, it seems a little clumsy/heavy-handed, so I'm probably wrong.
2) As you can imagine, creating a custom task-scope for our worker threads would be nice.
I had something working when I was incorrectly mucking around with ServletScopes.transferRequest()) BUT the CustomScopes page advises against creating custom scopes:
It is generally recommended that users do not write their own custom scopes — the built-in scopes should be sufficient for most applications.
Is there a better approach for scoping things within our task-threads?
Thanks in advance!!