You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by zmirc <m_...@yahoo.com> on 2013/10/23 10:37:01 UTC

@Async call blocks in @PostConstruct

Hi!

Shouldn't async methods not block even if they are called in @PostConstruct?
Here is the sample code, which blocks if ran in Tomee 1.6.0 2013.10.23 / 05

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Asynchronous;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Singleton
@Startup
@Asynchronous
public class Async {
    
    @PostConstruct
    private void init() {
	// pretending to initialize, then to call one of the methods
	doingHardWork();
    }
    
    public void doingHardWork() {
	System.out.println("Working hard START");
	try {
	    Thread.sleep(1000 * 10);
	} catch (InterruptedException ex) {
	    Logger.getLogger(Async.class.getName()).log(Level.SEVERE, null, ex);
	}
	System.out.println("Working hard END");
    }
    
}



--
View this message in context: http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: @Async call blocks in @PostConstruct

Posted by Romain Manni-Bucau <rm...@gmail.com>.
It should too

Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2013/10/23 Thiago Veronezi <th...@veronezi.org>:
> Would it work if the startup bean called an Asynchronous method of one of
> its injected ejbs?
>  On 23 Oct 2013 06:36, "zmirc" <m_...@yahoo.com> wrote:
>
>> Nice trick!
>> Thanks a lot for the sample and for the quick answer.
>>
>>
>>
>> --
>> View this message in context:
>> http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703p4665707.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>>

Re: @Async call blocks in @PostConstruct

Posted by Thiago Veronezi <th...@veronezi.org>.
Would it work if the startup bean called an Asynchronous method of one of
its injected ejbs?
 On 23 Oct 2013 06:36, "zmirc" <m_...@yahoo.com> wrote:

> Nice trick!
> Thanks a lot for the sample and for the quick answer.
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703p4665707.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>

Re: @Async call blocks in @PostConstruct

Posted by zmirc <m_...@yahoo.com>.
Nice trick!
Thanks a lot for the sample and for the quick answer.



--
View this message in context: http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703p4665707.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: @Async call blocks in @PostConstruct

Posted by Mark Struberg <st...@yahoo.de>.
Oh and btw, even more important: don't use this neat trick for anything you really depend on in your app. Otherwise you will fail in production if the server gets booted or a cluster node gets added under heavy load.

What I mean: This trick is perfect for refreshing statistics or cleaning up old temp stuff on the disc. But it is NOT a good idea to use it to configure your application, provide important initial data for your services, etc. Why? Because it's simply not guaranteed that all those async stuff really did finish before your server gets hit by a bazillion user requests if you restart a node under load.

LieGrue,
strub




----- Original Message -----
> From: Mark Struberg <st...@yahoo.de>
> To: "users@tomee.apache.org" <us...@tomee.apache.org>
> Cc: OpenEJB Users <us...@openejb.apache.org>
> Sent: Thursday, 24 October 2013, 9:51
> Subject: Re: @Async call blocks in @PostConstruct
> 
> 
> 
> And as discussed with Romain on IRC a few minutes ago:
> 
> This works in OpenEJB, because it's smart ;)
> But I would not bet a dime that this is clear from the spec. So it is probably 
> not portable and possibly won't work on other EJB containers.
> 
> There is of course an easy alternative solution.
> Just move the @Asynchronous method to an own EJB and inject it. The only thing 
> you must guarantee is that you don't have an injection cycle between your 
> @Startup and your worker EJB.
> In this case you also don't need to deal with the SessionContext. Which 
> might be another issue: I'm not sure if it's save (again: works in 
> OpenEJB, but what about other EJB containers) to use the SessionContext during 
> @PostConstruct. Would need to ask this on the EJB EG (or grab deep in the spec).
> 
> To be more clear what the real issue is: @PostConstruct is part of the 
> 'initialisation' of any manged instance. Like @Inject fields, methods 
> and constructors are. And the EJB must only be made available to the user once 
> this initialisation process is 100% finished.
> 
> LieGrue,
> strub
> 
> 
> 
> 
> 
>> ________________________________
> 
>>  From: Romain Manni-Bucau <rm...@gmail.com>
>> To: "users@tomee.apache.org" <us...@tomee.apache.org> 
>> Cc: OpenEJB Users <us...@openejb.apache.org> 
>> Sent: Thursday, 24 October 2013, 9:31
>> Subject: Re: @Async call blocks in @PostConstruct
>> 
>> 
>> Hi
>> 
>> The spec mandates @PostConstruct to finish before business method are
>> called. That's what we do blocking to start the @Async method. If you
>> rmethod is not @Async it obviously doesn't work.
>> Romain Manni-Bucau
>> Twitter: @rmannibucau
>> Blog: http://rmannibucau.wordpress.com/
>> LinkedIn: http://fr.linkedin.com/in/rmannibucau
>> Github: https://github.com/rmannibucau
>> 
>> 
>> 
>> 
>> 2013/10/24 zmirc <m_...@yahoo.com>:
>>>  Hi!
>>> 
>>>  Mark might have a good point, which can be shown with the current 
> example
>>>  (see code).
>>>  By removing @Asynchronous from this EJB, Tomee does blocks for ever.
>>>  If @Asynchronous is not commented out, then it works perfectly, with 
> async
>>>  doingHardWork() method being executed after the @PostConstruct init(), 
> even
>>>  though it is called inside the @PostConstruct. That should be ok.
>>> 
>>>  It's maybe not so ok if @Asynchronous is removed. Shouldn't 
> SessionContext
>>>  throw an exception or something if someone tries to use it in this way 
> that
>>>  blocks Tomee deployment completely?
>>> 
>>>  Async.java 
> <http://openejb.979440.n4.nabble.com/file/n4665718/Async.java>
>>> 
>>> 
>>> 
>>>  --
>>>  View this message in context: 
> http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703p4665718.html
>>>  Sent from the OpenEJB User mailing list archive at Nabble.com.
>> 
>> 
>> 
> 

Re: @Async call blocks in @PostConstruct

Posted by Mark Struberg <st...@yahoo.de>.

And as discussed with Romain on IRC a few minutes ago:

This works in OpenEJB, because it's smart ;)
But I would not bet a dime that this is clear from the spec. So it is probably not portable and possibly won't work on other EJB containers.

There is of course an easy alternative solution.
Just move the @Asynchronous method to an own EJB and inject it. The only thing you must guarantee is that you don't have an injection cycle between your @Startup and your worker EJB.
In this case you also don't need to deal with the SessionContext. Which might be another issue: I'm not sure if it's save (again: works in OpenEJB, but what about other EJB containers) to use the SessionContext during @PostConstruct. Would need to ask this on the EJB EG (or grab deep in the spec).

To be more clear what the real issue is: @PostConstruct is part of the 'initialisation' of any manged instance. Like @Inject fields, methods and constructors are. And the EJB must only be made available to the user once this initialisation process is 100% finished.

LieGrue,
strub





>________________________________
> From: Romain Manni-Bucau <rm...@gmail.com>
>To: "users@tomee.apache.org" <us...@tomee.apache.org> 
>Cc: OpenEJB Users <us...@openejb.apache.org> 
>Sent: Thursday, 24 October 2013, 9:31
>Subject: Re: @Async call blocks in @PostConstruct
> 
>
>Hi
>
>The spec mandates @PostConstruct to finish before business method are
>called. That's what we do blocking to start the @Async method. If you
>rmethod is not @Async it obviously doesn't work.
>Romain Manni-Bucau
>Twitter: @rmannibucau
>Blog: http://rmannibucau.wordpress.com/
>LinkedIn: http://fr.linkedin.com/in/rmannibucau
>Github: https://github.com/rmannibucau
>
>
>
>
>2013/10/24 zmirc <m_...@yahoo.com>:
>> Hi!
>>
>> Mark might have a good point, which can be shown with the current example
>> (see code).
>> By removing @Asynchronous from this EJB, Tomee does blocks for ever.
>> If @Asynchronous is not commented out, then it works perfectly, with async
>> doingHardWork() method being executed after the @PostConstruct init(), even
>> though it is called inside the @PostConstruct. That should be ok.
>>
>> It's maybe not so ok if @Asynchronous is removed. Shouldn't SessionContext
>> throw an exception or something if someone tries to use it in this way that
>> blocks Tomee deployment completely?
>>
>> Async.java <http://openejb.979440.n4.nabble.com/file/n4665718/Async.java>
>>
>>
>>
>> --
>> View this message in context: http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703p4665718.html
>> Sent from the OpenEJB User mailing list archive at Nabble.com.
>
>
>

Re: @Async call blocks in @PostConstruct

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

The spec mandates @PostConstruct to finish before business method are
called. That's what we do blocking to start the @Async method. If you
rmethod is not @Async it obviously doesn't work.
Romain Manni-Bucau
Twitter: @rmannibucau
Blog: http://rmannibucau.wordpress.com/
LinkedIn: http://fr.linkedin.com/in/rmannibucau
Github: https://github.com/rmannibucau



2013/10/24 zmirc <m_...@yahoo.com>:
> Hi!
>
> Mark might have a good point, which can be shown with the current example
> (see code).
> By removing @Asynchronous from this EJB, Tomee does blocks for ever.
> If @Asynchronous is not commented out, then it works perfectly, with async
> doingHardWork() method being executed after the @PostConstruct init(), even
> though it is called inside the @PostConstruct. That should be ok.
>
> It's maybe not so ok if @Asynchronous is removed. Shouldn't SessionContext
> throw an exception or something if someone tries to use it in this way that
> blocks Tomee deployment completely?
>
> Async.java <http://openejb.979440.n4.nabble.com/file/n4665718/Async.java>
>
>
>
> --
> View this message in context: http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703p4665718.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: @Async call blocks in @PostConstruct

Posted by zmirc <m_...@yahoo.com>.
Hi!

Mark might have a good point, which can be shown with the current example
(see code).
By removing @Asynchronous from this EJB, Tomee does blocks for ever.
If @Asynchronous is not commented out, then it works perfectly, with async
doingHardWork() method being executed after the @PostConstruct init(), even
though it is called inside the @PostConstruct. That should be ok.

It's maybe not so ok if @Asynchronous is removed. Shouldn't SessionContext
throw an exception or something if someone tries to use it in this way that
blocks Tomee deployment completely?

Async.java <http://openejb.979440.n4.nabble.com/file/n4665718/Async.java>  



--
View this message in context: http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703p4665718.html
Sent from the OpenEJB User mailing list archive at Nabble.com.

Re: @Async call blocks in @PostConstruct

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Mark it is not like @Dependent. Here you call an async task. The idea is to
submit in the executor service the invocation ASAP then return. Once done
no issue waiting for the instance being created in the @Async and no thread
safety issues.
Le 24 oct. 2013 00:44, "Mark Struberg" <st...@yahoo.de> a écrit :

> Romain, the point is: it *must not* work according to the spec!
>
> Of course the SessionContext is available, but your own contextual
> instance (same in EJB) only gets put into the SessionContext _after_ the
> @PostConstruct method gets invoked and it is really fully initialized.
>
> And before that time you cannot reference yourself as this would
> recursively create a new instance of yourself (endlessly). This is like
> injecting a @Depenent instance in itself. All the self-reference injection
> only works if only a proxy gets injected, and this proxy doesn't get used
> until the  contextual instance / EJB is fully initialized and put into the
> respective context.
>
> See JSR-250 common-annotations paragraph 2.5 (which is binding for both
> CDI and EJB):
>
> "The PostConstruct annotation is used on a method that needs to be
> executed after
> dependency injection is done to perform any initialization. This method
> MUST be
> invoked before the class is put into service."
>
> that uppercase letters are really in the spec ;)
>
> I already clarified this in the EJB and CDI Expert Groups when working on
> the CDI section about recursive injections.
>
> If this works in OpenEJB, then you might not be thread safe while creating
> your instances. At least it's non-portable behaviour.
> If you like, you can bring this to the EJB EG as question and we can
> clarify this over there.
>
> LieGrue,
> strub
>
>
>
>
> ----- Original Message -----
> > From: Romain Manni-Bucau <rm...@gmail.com>
> > To: "users@tomee.apache.org" <us...@tomee.apache.org>; Mark Struberg <
> struberg@yahoo.de>
> > Cc:
> > Sent: Wednesday, 23 October 2013, 11:29
> > Subject: Re: @Async call blocks in @PostConstruct
> >
> > Mark: it was not a question, it works :p
> >
> > SessionContext is usable in the @PostConstruct so this is doable.
> >
> > here is a sample
> >
> https://github.com/rmannibucau/JeBlog/blob/master/src/main/java/com/github/rmannibucau/blog/init/DBSetup.java
> >
> > *Romain Manni-Bucau*
> > *Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
> > *Blog:
> > **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
> > *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> > *Github: https://github.com/rmannibucau*
> >
> >
> >
> >
> > 2013/10/23 Mark Struberg <st...@yahoo.de>
> >
> >>  Romain, I fear this will not work.
> >>
> >>
> >>  Please note that the commons-annotation spec defines that
> @PostConstruct
> >>  methods must be invoked _before_ the instance is made available.
> >>
> >>
> >>  LieGrue,
> >>  strub
> >>
> >>
> >>
> >>
> >>  ----- Original Message -----
> >>  > From: Romain Manni-Bucau <rm...@gmail.com>
> >>  > To: "users@tomee.apache.org" <us...@tomee.apache.org>
> >>  > Cc: OpenEJB Users <us...@openejb.apache.org>
> >>  > Sent: Wednesday, 23 October 2013, 11:15
> >>  > Subject: Re: @Async call blocks in @PostConstruct
> >>  >
> >>  > Hi
> >>  >
> >>  > to do so simply inject the sessioncontext get the business facade (on
> >>  > yourself) to invoke the @Async method.
> >>  >
> >>  > PostContruct are not async by default AFAIK
> >>  >
> >>  > *Romain Manni-Bucau*
> >>  > *Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
> >>  > *Blog:
> >>  >
> > **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
> >>  > *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> >>  > *Github: https://github.com/rmannibucau*
> >>  >
> >>  >
> >>  >
> >>  >
> >>  > 2013/10/23 zmirc <m_...@yahoo.com>
> >>  >
> >>  >>  Hi!
> >>  >>
> >>  >>  Shouldn't async methods not block even if they are called in
> >>  >>  @PostConstruct?
> >>  >>  Here is the sample code, which blocks if ran in Tomee 1.6.0
> > 2013.10.23
> >>  / 05
> >>  >>
> >>  >>  import java.util.logging.Level;
> >>  >>  import java.util.logging.Logger;
> >>  >>  import javax.annotation.PostConstruct;
> >>  >>  import javax.ejb.Asynchronous;
> >>  >>  import javax.ejb.Singleton;
> >>  >>  import javax.ejb.Startup;
> >>  >>
> >>  >>  @Singleton
> >>  >>  @Startup
> >>  >>  @Asynchronous
> >>  >>  public class Async {
> >>  >>
> >>  >>      @PostConstruct
> >>  >>      private void init() {
> >>  >>          // pretending to initialize, then to call one of the
> > methods
> >>  >>          doingHardWork();
> >>  >>      }
> >>  >>
> >>  >>      public void doingHardWork() {
> >>  >>          System.out.println("Working hard START");
> >>  >>          try {
> >>  >>              Thread.sleep(1000 * 10);
> >>  >>          } catch (InterruptedException ex) {
> >>  >>
> > Logger.getLogger(Async.class.getName()).log(Level.SEVERE,
> >>  >>  null, ex);
> >>  >>          }
> >>  >>          System.out.println("Working hard END");
> >>  >>      }
> >>  >>
> >>  >>  }
> >>  >>
> >>  >>
> >>  >>
> >>  >>  --
> >>  >>  View this message in context:
> >>  >>
> >>  >
> >>
> >
> http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703.html
> >>  >>  Sent from the OpenEJB User mailing list archive at Nabble.com.
> >>  >>
> >>  >
> >>
> >
>

Re: @Async call blocks in @PostConstruct

Posted by Mark Struberg <st...@yahoo.de>.
Romain, the point is: it *must not* work according to the spec!

Of course the SessionContext is available, but your own contextual instance (same in EJB) only gets put into the SessionContext _after_ the @PostConstruct method gets invoked and it is really fully initialized. 

And before that time you cannot reference yourself as this would recursively create a new instance of yourself (endlessly). This is like injecting a @Depenent instance in itself. All the self-reference injection only works if only a proxy gets injected, and this proxy doesn't get used until the  contextual instance / EJB is fully initialized and put into the respective context.

See JSR-250 common-annotations paragraph 2.5 (which is binding for both CDI and EJB):

"The PostConstruct annotation is used on a method that needs to be executed after
dependency injection is done to perform any initialization. This method MUST be
invoked before the class is put into service."

that uppercase letters are really in the spec ;)

I already clarified this in the EJB and CDI Expert Groups when working on the CDI section about recursive injections.

If this works in OpenEJB, then you might not be thread safe while creating your instances. At least it's non-portable behaviour.
If you like, you can bring this to the EJB EG as question and we can clarify this over there.

LieGrue,
strub




----- Original Message -----
> From: Romain Manni-Bucau <rm...@gmail.com>
> To: "users@tomee.apache.org" <us...@tomee.apache.org>; Mark Struberg <st...@yahoo.de>
> Cc: 
> Sent: Wednesday, 23 October 2013, 11:29
> Subject: Re: @Async call blocks in @PostConstruct
> 
> Mark: it was not a question, it works :p
> 
> SessionContext is usable in the @PostConstruct so this is doable.
> 
> here is a sample
> https://github.com/rmannibucau/JeBlog/blob/master/src/main/java/com/github/rmannibucau/blog/init/DBSetup.java
> 
> *Romain Manni-Bucau*
> *Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
> *Blog: 
> **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
> *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> *Github: https://github.com/rmannibucau*
> 
> 
> 
> 
> 2013/10/23 Mark Struberg <st...@yahoo.de>
> 
>>  Romain, I fear this will not work.
>> 
>> 
>>  Please note that the commons-annotation spec defines that @PostConstruct
>>  methods must be invoked _before_ the instance is made available.
>> 
>> 
>>  LieGrue,
>>  strub
>> 
>> 
>> 
>> 
>>  ----- Original Message -----
>>  > From: Romain Manni-Bucau <rm...@gmail.com>
>>  > To: "users@tomee.apache.org" <us...@tomee.apache.org>
>>  > Cc: OpenEJB Users <us...@openejb.apache.org>
>>  > Sent: Wednesday, 23 October 2013, 11:15
>>  > Subject: Re: @Async call blocks in @PostConstruct
>>  >
>>  > Hi
>>  >
>>  > to do so simply inject the sessioncontext get the business facade (on
>>  > yourself) to invoke the @Async method.
>>  >
>>  > PostContruct are not async by default AFAIK
>>  >
>>  > *Romain Manni-Bucau*
>>  > *Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
>>  > *Blog:
>>  > 
> **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
>>  > *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
>>  > *Github: https://github.com/rmannibucau*
>>  >
>>  >
>>  >
>>  >
>>  > 2013/10/23 zmirc <m_...@yahoo.com>
>>  >
>>  >>  Hi!
>>  >>
>>  >>  Shouldn't async methods not block even if they are called in
>>  >>  @PostConstruct?
>>  >>  Here is the sample code, which blocks if ran in Tomee 1.6.0 
> 2013.10.23
>>  / 05
>>  >>
>>  >>  import java.util.logging.Level;
>>  >>  import java.util.logging.Logger;
>>  >>  import javax.annotation.PostConstruct;
>>  >>  import javax.ejb.Asynchronous;
>>  >>  import javax.ejb.Singleton;
>>  >>  import javax.ejb.Startup;
>>  >>
>>  >>  @Singleton
>>  >>  @Startup
>>  >>  @Asynchronous
>>  >>  public class Async {
>>  >>
>>  >>      @PostConstruct
>>  >>      private void init() {
>>  >>          // pretending to initialize, then to call one of the 
> methods
>>  >>          doingHardWork();
>>  >>      }
>>  >>
>>  >>      public void doingHardWork() {
>>  >>          System.out.println("Working hard START");
>>  >>          try {
>>  >>              Thread.sleep(1000 * 10);
>>  >>          } catch (InterruptedException ex) {
>>  >>              
> Logger.getLogger(Async.class.getName()).log(Level.SEVERE,
>>  >>  null, ex);
>>  >>          }
>>  >>          System.out.println("Working hard END");
>>  >>      }
>>  >>
>>  >>  }
>>  >>
>>  >>
>>  >>
>>  >>  --
>>  >>  View this message in context:
>>  >>
>>  >
>> 
> http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703.html
>>  >>  Sent from the OpenEJB User mailing list archive at Nabble.com.
>>  >>
>>  >
>> 
> 

Re: @Async call blocks in @PostConstruct

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Mark: it was not a question, it works :p

SessionContext is usable in the @PostConstruct so this is doable.

here is a sample
https://github.com/rmannibucau/JeBlog/blob/master/src/main/java/com/github/rmannibucau/blog/init/DBSetup.java

*Romain Manni-Bucau*
*Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
*Github: https://github.com/rmannibucau*



2013/10/23 Mark Struberg <st...@yahoo.de>

> Romain, I fear this will not work.
>
>
> Please note that the commons-annotation spec defines that @PostConstruct
> methods must be invoked _before_ the instance is made available.
>
>
> LieGrue,
> strub
>
>
>
>
> ----- Original Message -----
> > From: Romain Manni-Bucau <rm...@gmail.com>
> > To: "users@tomee.apache.org" <us...@tomee.apache.org>
> > Cc: OpenEJB Users <us...@openejb.apache.org>
> > Sent: Wednesday, 23 October 2013, 11:15
> > Subject: Re: @Async call blocks in @PostConstruct
> >
> > Hi
> >
> > to do so simply inject the sessioncontext get the business facade (on
> > yourself) to invoke the @Async method.
> >
> > PostContruct are not async by default AFAIK
> >
> > *Romain Manni-Bucau*
> > *Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
> > *Blog:
> > **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
> > *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> > *Github: https://github.com/rmannibucau*
> >
> >
> >
> >
> > 2013/10/23 zmirc <m_...@yahoo.com>
> >
> >>  Hi!
> >>
> >>  Shouldn't async methods not block even if they are called in
> >>  @PostConstruct?
> >>  Here is the sample code, which blocks if ran in Tomee 1.6.0 2013.10.23
> / 05
> >>
> >>  import java.util.logging.Level;
> >>  import java.util.logging.Logger;
> >>  import javax.annotation.PostConstruct;
> >>  import javax.ejb.Asynchronous;
> >>  import javax.ejb.Singleton;
> >>  import javax.ejb.Startup;
> >>
> >>  @Singleton
> >>  @Startup
> >>  @Asynchronous
> >>  public class Async {
> >>
> >>      @PostConstruct
> >>      private void init() {
> >>          // pretending to initialize, then to call one of the methods
> >>          doingHardWork();
> >>      }
> >>
> >>      public void doingHardWork() {
> >>          System.out.println("Working hard START");
> >>          try {
> >>              Thread.sleep(1000 * 10);
> >>          } catch (InterruptedException ex) {
> >>              Logger.getLogger(Async.class.getName()).log(Level.SEVERE,
> >>  null, ex);
> >>          }
> >>          System.out.println("Working hard END");
> >>      }
> >>
> >>  }
> >>
> >>
> >>
> >>  --
> >>  View this message in context:
> >>
> >
> http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703.html
> >>  Sent from the OpenEJB User mailing list archive at Nabble.com.
> >>
> >
>

Re: @Async call blocks in @PostConstruct

Posted by Mark Struberg <st...@yahoo.de>.
Romain, I fear this will not work. 


Please note that the commons-annotation spec defines that @PostConstruct methods must be invoked _before_ the instance is made available.


LieGrue,
strub




----- Original Message -----
> From: Romain Manni-Bucau <rm...@gmail.com>
> To: "users@tomee.apache.org" <us...@tomee.apache.org>
> Cc: OpenEJB Users <us...@openejb.apache.org>
> Sent: Wednesday, 23 October 2013, 11:15
> Subject: Re: @Async call blocks in @PostConstruct
> 
> Hi
> 
> to do so simply inject the sessioncontext get the business facade (on
> yourself) to invoke the @Async method.
> 
> PostContruct are not async by default AFAIK
> 
> *Romain Manni-Bucau*
> *Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
> *Blog: 
> **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
> *LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
> *Github: https://github.com/rmannibucau*
> 
> 
> 
> 
> 2013/10/23 zmirc <m_...@yahoo.com>
> 
>>  Hi!
>> 
>>  Shouldn't async methods not block even if they are called in
>>  @PostConstruct?
>>  Here is the sample code, which blocks if ran in Tomee 1.6.0 2013.10.23 / 05
>> 
>>  import java.util.logging.Level;
>>  import java.util.logging.Logger;
>>  import javax.annotation.PostConstruct;
>>  import javax.ejb.Asynchronous;
>>  import javax.ejb.Singleton;
>>  import javax.ejb.Startup;
>> 
>>  @Singleton
>>  @Startup
>>  @Asynchronous
>>  public class Async {
>> 
>>      @PostConstruct
>>      private void init() {
>>          // pretending to initialize, then to call one of the methods
>>          doingHardWork();
>>      }
>> 
>>      public void doingHardWork() {
>>          System.out.println("Working hard START");
>>          try {
>>              Thread.sleep(1000 * 10);
>>          } catch (InterruptedException ex) {
>>              Logger.getLogger(Async.class.getName()).log(Level.SEVERE,
>>  null, ex);
>>          }
>>          System.out.println("Working hard END");
>>      }
>> 
>>  }
>> 
>> 
>> 
>>  --
>>  View this message in context:
>> 
> http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703.html
>>  Sent from the OpenEJB User mailing list archive at Nabble.com.
>> 
> 

Re: @Async call blocks in @PostConstruct

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

to do so simply inject the sessioncontext get the business facade (on
yourself) to invoke the @Async method.

PostContruct are not async by default AFAIK

*Romain Manni-Bucau*
*Twitter: @rmannibucau <https://twitter.com/rmannibucau>*
*Blog: **http://rmannibucau.wordpress.com/*<http://rmannibucau.wordpress.com/>
*LinkedIn: **http://fr.linkedin.com/in/rmannibucau*
*Github: https://github.com/rmannibucau*



2013/10/23 zmirc <m_...@yahoo.com>

> Hi!
>
> Shouldn't async methods not block even if they are called in
> @PostConstruct?
> Here is the sample code, which blocks if ran in Tomee 1.6.0 2013.10.23 / 05
>
> import java.util.logging.Level;
> import java.util.logging.Logger;
> import javax.annotation.PostConstruct;
> import javax.ejb.Asynchronous;
> import javax.ejb.Singleton;
> import javax.ejb.Startup;
>
> @Singleton
> @Startup
> @Asynchronous
> public class Async {
>
>     @PostConstruct
>     private void init() {
>         // pretending to initialize, then to call one of the methods
>         doingHardWork();
>     }
>
>     public void doingHardWork() {
>         System.out.println("Working hard START");
>         try {
>             Thread.sleep(1000 * 10);
>         } catch (InterruptedException ex) {
>             Logger.getLogger(Async.class.getName()).log(Level.SEVERE,
> null, ex);
>         }
>         System.out.println("Working hard END");
>     }
>
> }
>
>
>
> --
> View this message in context:
> http://openejb.979440.n4.nabble.com/Async-call-blocks-in-PostConstruct-tp4665703.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>