users@glassfish.java.net

Re: Best practice for a startup module in Glassfish V3

From: David Van Couvering <David.Vancouvering_at_Sun.COM>
Date: Thu, 04 Sep 2008 09:06:39 -0700

Hi, all. Thanks for your answers. Sounds good.

David

Jerome Dochez wrote:
> David Van Couvering wrote:
>> Hi, all. I have written a v3 module that I'd like to run on
>> startup. What's the recommended way to do this?
>>
>> Right now I do this.
>>
>> import org.glassfish.api.Startup;
>> import org.jvnet.hk2.annotations.Service;
>>
>> @Service(name="twibber")
>> public class Twibber implements Startup, Runnable
>> {
>> public Lifecycle getLifecycle() {
>> return Lifecycle.SERVER;
>> }
>> public Twibber() {
>> new Thread(this).start();
>> }
>> public void run() {
>> // ... Do the actual work here
>> }
>> }
>>
>>
>> It works, but I am concerned it's not the intended approach. What's
>> the *right* way to run the service? Is it really to spawn the thread
>> in the constructor? And how do I catch a shutdown event?
> Sahoo was right when saying you should use postContruct rather than
> the constructor, the reason is that anything injected is not available
> at the constructor time but at the postConstruct. Also if you just
> want to have your service to be asynchronous, the simplest way to do
> that is :
>
> @Service(name="twibber")
> @Async
> public class Twibber implements Startup, PostConstruct
> {
> public Lifecycle getLifecycle() {
> return Lifecycle.SERVER;
> }
> public void postConstruct() {
> // ... Do the actual work here
> }
> }
>
> As for getting notified when the serve shutsdown.
>
> look at org.glassfish.api.eveni.Events in glassfish-api module, this
> will allow you to receive shutdown notification.
> however, the simplest way is just to implement the preDestroy() method...
>
> jerome
>>
>> I suspect there's something I'm missing...
>>
>> Thanks,
>>
>> David
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>