Let’s say I have a Closeable object injected through Guice using request scope:
@Provides @RequestScoped
public MyCloseableResource providesMyCloseableResource(){
return new MyCloseableResourceImpl();
}
Is it possible to hook-up a clean-up method that would automatically call close() on my resource when the scope exists, without resorting to custom scope implementation?
Looking at the custom scope implementation guide on the Guice wiki, it shows that scopes should be created and cleaned up like this:
/**
* Runs {@code runnable} in batch scope.
*/
public void scopeRunnable(Runnable runnable) {
scope.enter();
try {
// explicitly seed some seed objects...
scope.seed(Key.get(SomeObject.class), someObject);
// create and access scoped objects
runnable.run();
} finally {
scope.exit();
}
}
I am wondering if there is way to hook-up some custom clean-up code in the finally of the built-in scopes (especially session and request scopes).
If it isn’t possible, might there be issues that would discourage this kind of automatic clean-up?
I have found ways of achieving the same effect in servlet containers by implementing a Filter to create and clean-up a resource per request, which works great but I am curious if it is possibly with pure Guice.
I faced a similar problem myself and finally rolled a
Disposableinterface which offers nothing but apublic void dispose()method. I find this especially valuable for classes that register listeners somewhere and need to unregister them at a defined time. What I already had was myAttributeHolderScopewhich I blogged about so I won’t repeat that part here. The only thing that is missing now is theAbstractAttributeHolderwhich looks like this:This class itself implements
Disposableso you can have nested scopes and when you dispose an outer scope, all nested scopes and, more importantly, all injected instances that implementDisposableget cleaned up. And to precisely answer you question: I don’t think that this is possible with theScopeimplementations provided by Guice itself, but it can be done. Everytime I look at this code I ask myself if this can’t be done in a more concise way, but then it works beautifully (at least for me).