On Jul 21, 2012, at 4:28 PM, zeeman wrote:

> The only extra thing I have is DB population, which should run in background
> non-blocking, right?
> 
> My class looks like this:
> 
> @Singleton
> @Startup
> public class DBLoader implements Serializable {
>       @Asynchronous
>       @TransactionAttribute
>       @PostConstruct
>       public void setup() {
>        //populate DB, index data with Lucene, should not block
> }
> }
> 
> The only explanation is that the code is not running Async. I know on Jboss
> there was a bug in @Async implementation, had to get jBoss guys to fix it.


When we (the EJB 3.1 EG) added @Asynchronous methods we were definitely 
exclusively thinking about method invocations from clients, not lifecycle 
callbacks made by the container.

It gets very sketchy when you get into @PreDestroy, @BeforeCompletion, 
@PrePassivate and other such callbacks.  Even @PostConstruct is sketchy as it 
has an effect on @DependsOn ordering which is supposed to be serial.

So be aware that this is outside the outside the EJB 3.1 spec, there are no TCK 
tests to cover this and your app is not portable.

What I might recommend instead and what will be portable is to just create a 
job and fire it off via the TimerService.

The following uses the @PostConstruct to fire off a non-persistent timer for 
"now".

    @Singleton
    @Startup
    public class DBLoader implements Serializable {
    
        @Resource
        private TimerService timerService;
    
        @TransactionAttribute
        @PostConstruct
        public void setup() {
            timerService.createSingleActionTimer(0, new TimerConfig(null, 
false));
        }
    
        @Timeout
        public void loadDb(Timer timer) {
            // do your thing
        }
    }

Reply via email to