Monday, May 6, 2013

async cdi


today i implemented a small lib (~100 lines of code) which allows to use asynchronous cdi beans (including asynchronous observer methods).

if you don't use ejbs (for whatever reason), you can't use javax.ejb.Asynchronous.

however, sometimes you might need some asynchronous calls. as i illustrated in 'akka-and-cdi-are-good-friends', you can use actor-frameworks for doing it. however, sometimes that's just not needed and a simple asynchronous call is enough (which in addition doesn't have to follow the rules introduced by actor-frameworks).

in such cases you can use @Async.


//...
public class AsyncObserver
{
@Async
public void observeAsync(@Observes MyEvent myEvent)
{
//...
}
}
@Async
//...
public class MyAsyncService1
{
public void processIt1(/*...*/)
{
//...
}
public java.util.concurrent.Future processIt2(/*...*/)
{
//...
}
}
//...
public class MyAsyncService2
{
@Async
public void processIt1(/*...*/)
{
//...
}
@Async
public java.util.concurrent.Future processIt2(/*...*/)
{
//...
}
}

a working example (with owb as well as tomee with owb 1.2.x) can be found here. currently it doesn't work with weld.