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 <[email protected]>
> To: "[email protected]" <[email protected]>; Mark Struberg 
> <[email protected]>
> 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 <[email protected]>
> 
>>  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 <[email protected]>
>>  > To: "[email protected]" <[email protected]>
>>  > Cc: OpenEJB Users <[email protected]>
>>  > 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 <[email protected]>
>>  >
>>  >>  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.
>>  >>
>>  >
>> 
>

Reply via email to