In my opinion, you have a few options:
1) You can create a class (and inject it) that optionally injects all of
the things you might shutdown, a la something like this:

class TestShutdownHelper {
    @Inject(optional = true) public TimeoutManager timeoutManager = null;
    ...

    public void cleanUp() {
      AutoCloseables.tryCloseIfNotNull(timeoutManager);
      // ...
    }
}

2) Or you could change the way that you do injection for things you want to
be able to close and inject them with OptionalBinder:
http://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/multibindings/OptionalBinder.html

You could then pull out Optional<TimeoutManager> instead (this is likely to
be lots more overhead esp. if you're doing it only for tests).

3) And my last suggestion would be to use MulitBinder and have bind each of
your Closeable classes to a Set<Closeable> -- then pull out the set from
the injector, iterate, and close each bound instance.
https://github.com/google/guice/wiki/Multibindings

Nate


On Fri, Oct 10, 2014 at 9:48 AM, David Leonhartsberger <[email protected]>
wrote:

> I was wondering If I can check on the Injector if an instance of some
> class/interface was already or
> have a method that does only return the instance if it was already created.
>
> Why I need this:
> In the shutdown of my integration tests I clean up all instances in the
> Service that I am testing.
> In most of the tests however some dependencies (TimeoutManager)  are never
> instaniated but will now in the shutdown code only to destroy them right
> afterwards, this takes some time and causes exceptions and a lot of
> confusing output in the test log.
>
>
>      /**
>>
>>      * Called by junit after each integration test.
>>
>>      */
>>
>>     @After
>>
>>     public final void afterIntegrationTest() {
>>
>>
>>
>>      if (service != null) {
>>>
>>>          service.destroy();
>>>
>>>      }
>>>
>>>
>>>
>>>            // more shutdow
>>
>     }
>>
>>
> private void tryCloseInstances() {
>>
>>                // current code
>
>>         TimeoutManager timeoutManager =
>>> injector.getInstance(TimeoutManager.class);
>>
>>         AutoCloseables.tryClose(timeoutManager);
>>
>>
>               // what i would like to do
>
>>           TimeoutManager timeoutManager =
>> injector.getInstanceOnlyIfCreated(TimeoutManager.class);
>
>           AutoCloseables.tryCloseIfNotNull(timeoutManager);
>
>     }
>>
>>
> Br
> David L.
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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/9d7440eb-d99c-4853-a499-f466b85282fd%40googlegroups.com
> <https://groups.google.com/d/msgid/google-guice/9d7440eb-d99c-4853-a499-f466b85282fd%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/CAHNex9_N-P73GZd_aM8P6qqGeNsCYT3i3ZPoZQfjjNZXdS59Rg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to