1

I am new to Google Guice after working a lot with Windsor Castle (.NET).

I would like to achieve in Google Guice something similar to Windsor Castle Startable Facility (http://docs.castleproject.org/Windsor.Startable-Facility.ashx).

This means that the injector will auto-create the instance of my singleton service eagerly as soon as it was binded and all dependencies are satisfied.

I would like to call a start() method on service creation, and stop() method on application shutdown. For example, start() would initialize a timer and stop() would typically cancel this timer.

I am aware of the Eager Singletons feature ".asEagerSingleton()" but I can think of few problems with it:

(1) If I do method injection in the service, the constructor of the service is called before method injected dependencies are set. Therefore I may not be able to fully use the service in the construction time.

(2) There is no auto shut-down. I have to manually get instance of the service in my application shutdown logic and call a stop() method. And worse, If I have many such services I have to manage shutdown order explicitly according to cross-dependencies.

Any suggestions?

dux2
  • 1,770
  • 1
  • 21
  • 27
  • I found LifeGycle (http://99soft.github.io/lifegycle) which should help managing injected objects lifetime. I will give it a try. – dux2 Dec 10 '13 at 09:32

2 Answers2

4

I would suggest you try Netflix's governator, which has classpath scanning and automatic binding, lifecycle management, configuration to field mapping, field validation and parallelized object warmup.

HMM
  • 2,987
  • 1
  • 20
  • 30
1

Try out the gwizard-services module in GWizard:

https://github.com/stickfigure/gwizard

This provides a small amount of glue around Guava Services such that it is easy to add services which start up, execute, and shut down in parallel.

stickfigure
  • 13,458
  • 5
  • 34
  • 50