I did this for a Guice-based framework I built of top of Netty 
- https://github.com/timboudreau/acteur - as follows:

First, some base classes for custom scopes:
https://github.com/timboudreau/scopes

And basically, since my code controls thread dispatch, you simply wrap any 
Runnable/Callable that is going to expect to be able to inject 
request-specific objects in another Runnable/Callable that first re-enters 
the scope with the same objects it had before.  You can make this even more 
transparent by wrapping an ExecutorService with one which:

 - Whenever new work is scheduled *from* a thread that is already in-scope, 
it is wrapped in such a Runnable/Callable

Now, whether Play has the entry points you'd need to control dispatch in 
this way, I don't know.  But basically, when a request comes in, you fill 
up a bag with the objects you want to be injectable, and "enter" the scope 
with them.  When the request processing code schedules something, you give 
it a copy of that bag, and before the code to be scheduled is invoked, you 
re-enter the scope with its former contents.

pseudocode example:

ReentrantScope scope = ...

public void schedule(Runnable r) { // assume this is a thread pool, 
whatever thread mgmt mechanism you or Play uses
    super.schedule (new ScopedRunnable(r));
}
...

class ScopedRunnable implements Runnable {
     final Object[] contents;
     final Runnable delegate;
     ScopedRunnable(Runnable delegate) {
        this.contents = scope.contents();
        this delegate = delegate;
     }

    public void run() {
        scope.enter(contents);
        try {
           delegate.run();
        } finally {
            scope.exit();
        }
}

(that's fairly rough code, but hopefully you get the idea - there are some 
convenience methods in the scopes library to make this sort of thing 
simple).

-Tim

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-guice+unsubscr...@googlegroups.com.
To post to this group, send email to google-guice@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-guice/da536895-2895-4133-bbc8-ea10c6a47403%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to