Is there a way to close/destruct object in guice 4?

4,383 views
Skip to first unread message

klc

unread,
May 14, 2015, 5:25:52 AM5/14/15
to google...@googlegroups.com
Hi Is there a way to destruct object in guice 4?

I see guice fruit but i think it support guice 2. The object i'm closing are db, queue and etc.

Stephan Classen

unread,
May 14, 2015, 5:31:20 AM5/14/15
to google...@googlegroups.com
Yes, but you have to be more specific if you want some concrete help

klc

unread,
May 14, 2015, 5:34:02 AM5/14/15
to google...@googlegroups.com
Sorry

What i have now is a provider of let say a queue (Singleton scope). If the queue has been created, i will get from the injector and close it. (or is there a way to do this automatically?)
This is on guice-servlet.

klc

klc

unread,
May 14, 2015, 5:50:16 AM5/14/15
to google...@googlegroups.com
So my questions are:
1. Is there a way to find out if a guice has been created?
2. Is there a way to let guice to run deconstruct when server is closing? (ie ServletContextListener.contextDestroyed)

klc

Stuart McCulloch

unread,
May 14, 2015, 6:56:47 AM5/14/15
to google...@googlegroups.com
On Thursday, 14 May 2015 at 10:50, klc wrote:
So my questions are:
1. Is there a way to find out if a guice has been created?
Previously in Guice 3 you could use a TypeListener to track when objects were directly instantiated by Guice (this doesn’t cover objects constructed inside of providers):


In Guice 4 you can also use the new ProvisionListener API to track when objects are provisioned (ie. either instantiated by Guice itself or returned from a provider)


but you might have to do some de-duplication here since the same object could be provisioned multiple times.

Another approach is to use a custom scope to record object use - you can then add a close method to the scope and call it when you want to dispose of those objects:

2. Is there a way to let guice to run deconstruct when server is closing? (ie ServletContextListener.contextDestroyed)
Assuming you’ve got a record of the objects needing to be destroyed (using one of the methods above) then it’s mainly a matter of going through the list in reverse order and calling the right destroy method.
klc

On Thursday, May 14, 2015 at 5:34:02 PM UTC+8, klc wrote:
Sorry

What i have now is a provider of let say a queue (Singleton scope). If the queue has been created, i will get from the injector and close it. (or is there a way to do this automatically?)
This is on guice-servlet.

klc

On Thursday, May 14, 2015 at 5:31:20 PM UTC+8, scl wrote:
Yes, but you have to be more specific if you want some concrete help

Am 14. Mai 2015 11:25:52 MESZ, schrieb klc <klchan...@gmail.com>:
Hi Is there a way to destruct object in guice 4?

I see guice fruit but i think it support guice 2. The object i'm closing are db, queue and etc.

--
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...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/4da7cf82-b95f-44e7-8c02-e6e81e0cc1ea%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vyacheslav Rusakov

unread,
May 14, 2015, 7:17:46 AM5/14/15
to google...@googlegroups.com
If you just want spring-like @PostConstruct and @PreDestroy methods support then there are many libs that implements it like https://github.com/mycila/guice, https://github.com/Netflix/governator etc.

In short, you should avoid implicit bootstrap logic and do some special lifecycle service with start/stop methods which should be called from outside world.
For example, in web application it could be servlet context listener which will call start / stop.


четверг, 14 мая 2015 г., 15:50:16 UTC+6 пользователь klc написал:
Reply all
Reply to author
Forward
0 new messages