EJB and asynchronous processing
I need to execute some stuff asynchronously after a EJB method is called. This need seems to be fulfilled in EJB 3.1 but unfortunately we're not there yet and have to use 3.0 version.
What would you suggest as the simplest way and are there caveats? I can think of the following approaches:
- JMS (maybe overkill)
- TimerService (looks simple enough)
- Spring TaskExecutor (how is this configured)
This needs to work in a single Weblogic server. There won't be any huge load or mas开发者_运维技巧sive parallel processing, I just want it nice and simple.
You must first decide whether you want the asynchronous thing to be transacted or not. That is, if the regular EJB method transaction fails, does the asynchronous thing still gets fired or not.
JMS and TimerService
are transactional. A Spring TaskExecutor
, probably isn't (I should double check that).
With a Timer
you can delay the execution (even with 0) of the thing so that after the EJB method returns, the asynchronous thing runs then immediately in the background. If the asynchronous thing fails, the timer service tries to re-run it a few times.
With JMS you don't have strict control over when the thing will be processed. Once the message is sent, you know that it will eventually be processed, but the broker is theoretically free to postpone it's processing to manage the load.
I don't know your particular use case, but using TimerService
seems a good fit if the load is not a concern, plus it's easy to use.
The WorkManager is designed for this purpose and is natively supported by WebSphere and WebLogic. There are other implementations as well for other app servers.
It is designed to do asynchronous work while still allowing the server to manage the threads.
精彩评论