Issue 62: Lifecycle support
http://code.google.com/p/google-guice/issues/detail?id=62

Comment #36 by james.strachan:
I've taken a stab at adding support for @PreDestroy and Spring's  
DisposableBean lifecycles; building above my
patch for issue 78:  
http://code.google.com/p/google-guice/issues/detail?id=78.

So far they only work with Singleton scoped objects; but I've also added  
the functionality into the REQUEST and
SESSION scoped Scopes too - though I've not yet added any code to the  
Servlet package to invoke the close()
methods yet.

I've introduced a Closeable interface which a Scope or Provider can  
implement; so if a Scope implements
Closeable you can then close it down cleanly; ensuring that all objects are  
closed and each exception thrown
is collected (rather like we do with the Errors class when binding objects).

Also I've added a close() method to Injector so you can close down any  
singleton objects.

e.g.

   Injector injector = Guice.createInjector(...);

   // do stuff

   // now lets cleanly close down the singletons in this injector
   injector.close();

There are many possible strategies we can use to actually close down an  
object held within a
Singleton/REQUEST/SESSION scope . e.g. respecting java.io.Closeable; or  
using @PreDestroy on a method or
Spring's DisposableBean or some developer-defined lifecycle API/annotation  
mechanism etc.

So I've introduced a strategy interface, Closer which is a strategy to  
close objects down. So you can bind as
many Closer implementations into your binder and this patch will invoke  
them all on each object in the scope
being closed. e.g. so you could install the JavaIO, JSR250 and Spring  
closers to respect those lifecycles. If you
look at some of the Closer implementations in this patch it will become a  
bit more clear.

Note this patch has no effect to objects created outside of a singleton  
(and is currently not invoked for
REQUEST/SESSION scopes).

The code InjectorImpl.close() and REQUEST/SESSION scopes is fairly  
straightforward. The more complex
change was dealing with the Singleton scope; as the singleton SINGLETON  
object creates Providers for each
key which internally stores the singleton object. So to be able to close  
all the singletons on an injector you
need to iterate through all the Providers in the bindings looking for  
providers which are Closeable. Since
there's a lot of wrapping of Providers along with InternalFactory; I made a  
number of internal wrappers and
adapters also implement Closeable. This should have no runtime effect when  
folks don't invoke the
Injector.close() method.



Attachments:
        complete_with_predestroy.patch  78.9 KB



-- 
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to