Standalone beans with init and tasks

91 views
Skip to first unread message

artur.kr...@capmedia.io

unread,
Sep 16, 2015, 10:20:56 AM9/16/15
to google-guice
Hi,

I am currently working with guice and dropwizard. I am NOT using the dropwizard-guice dependency since this is somewhat broken with the latest dropwizard version. Fortunatelly I found a nice tutorial explaining how to get this set up so I can use guice.  I however have a few issues now, where coming from spring, I am struggling to find the alternative/best practise for it. 

Scenario 1:
I have a bean that simply checks at startup that the schema of my database is present. I solved this by doing: 

@Provides @Singleton public bla() {
 // object
// init
}

The issue is, that I now have to run guice in production. I am feeling that I am abusing the eager singleton  creation in production and not doing the right thing here.
How do I create standalone beans that get created and do some work. In spring I would create this and then call an init method on it. In guice I am not sure how to solve this.


Scenario 2:
Timer Tasks that are scheduled or simply threads. E.g. a Monitoring thread that gets metric in and pokes them to a reporting server once a minute. I have the same issue here,
i can create the bean, but I can't call run on it. Also, if I am not in production, since the bean itself is not used anywhere, it won't get created and won't do any work.

It'd be great if someone could give me some pointer for this :) 

Thanks,

Artur

avs...@gmail.com

unread,
Sep 16, 2015, 4:59:36 PM9/16/15
to google-guice
Hi

(1) Perhaps you can extract the @Provides method to a class with an init() method, inject it in a listener and initialize it there.

Regards
--
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/2cf2b64e-38fb-416d-a1ff-dd56618b9b99%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Stephan Classen

unread,
Sep 16, 2015, 6:12:00 PM9/16/15
to google...@googlegroups.com
Guice is a dependency injection framework. Its nothing more.

The scenario 1 can be summarized as:
"Run some code once at application startup."
Then do it. Run the code at application startup. Don't try to make Guice run the code for you.
i.e. in the dropwizard application class there is a method initialize().

For your scenario 2 you want some timer task.
You won't find this in Guice. But there are extensions which provide such functionality.
One would be: http://onami.apache.org/scheduler/
There are more out there. Pick the one that suits your needs best.

Vyacheslav Rusakov

unread,
Sep 17, 2015, 12:11:44 AM9/17/15
to google-guice
I'm using dw Manged object to start db services and synchronize schema. It's the default dw point for lifecycle-related staff.
Also, I wrote https://github.com/xvik/dropwizard-guicey which registers managed objects for me, so I'm always on "guice side" (no direct injector usage required for manual integration)

четверг, 17 сентября 2015 г., 4:12:00 UTC+6 пользователь scl написал:

Artur kronenberg

unread,
Sep 17, 2015, 4:40:16 AM9/17/15
to google...@googlegroups.com
Hi,

thanks for the help. The managed bean approach looks interesting.

I tried guicy, however I ran into a few problems with dropwizard 0.8.4.
 * The environment and configuration is not injectable at the time the GuiceModule is registered
 * I couldn't register Features/Filters automatically with jersey so I had to do this programatically in the end as well 
 
This is the reason I went back to regular guice and simply use the injector in the run method to register the things I need. Other than that guicy was actually quite nice to use. 

So I think I saw guice wrong, where I expected more similarities with Springs lifecycle management etc.

Thanks!


--
You received this message because you are subscribed to a topic in the Google Groups "google-guice" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-guice/p52elK3pjrw/unsubscribe.
To unsubscribe from this group and all its topics, 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.

Vyacheslav Rusakov

unread,
Sep 17, 2015, 5:10:54 AM9/17/15
to google-guice
Environment  and Configuration are not available in time of bundle registration (due to initialization phase), but they are available inside guice module (in time of guice context configuration) if module implements (*AwareModule interfaces, e.g. EnvironmentAwareModule).
Features/Filters support was added in the last version.

Sorry for offtopic, but I can't avoid mentioning that :)


I love guice philosophy "I'm DI and that's all" because it brings many benefits. 
For example, it simplifies tests because you always have just context and could init(run) just what you need.
Usually, you have a single service/whatever_place to run all required services which makes init process much more clear and predictable.

четверг, 17 сентября 2015 г., 14:40:16 UTC+6 пользователь Artur kronenberg написал:

Artur kronenberg

unread,
Sep 17, 2015, 7:25:59 AM9/17/15
to google...@googlegroups.com
Hey,

so I was a bit confused before, now I am using your suggested solution - it works great and out of the box the way I want it to :) Even the feature stuff! I will build on top of that - keep up the good work! I really appreciated it! 

Thanks,

artur

Reply all
Reply to author
Forward
0 new messages