How to use Guava ServiceManager with Guice injection

As mentioned here , the Guava ServiceManager can be obtained

ServiceManager manager = injector.getInstance(ServiceManager.class); 

To make this work, I added the following to my Guice module:

 @Provides public Set<Service> services(){ return ImmutableSet.<Service>of(MyService()); } 

In my main class

 ServiceManager manager = injector.getInstance(ServiceManager.class); manager.startAsync().awaitHealthy(); 

How to get instances of running services?

ps Setting up the services that will be @Singleton looks like a hack.

+3
source share
2 answers

In my opinion, installing @Singleton services @Singleton not a hack at all. I probably would have done it.

 @Provides @Singleton public MyService myService() { return new MyService(); } @Provides public Set<Service> services(MyService myService) { return ImmutableSet.<Service>of(myService); } 

Then you can simply enter any specific service instance that you want, anywhere.

0
source

ServiceManager.getServicesByState().get(RUNNING) returns the running services, and ServiceManager.getServicesByState().values() returns all the services managed by ServiceManager.

+2
source

Source: https://habr.com/ru/post/983018/


All Articles