JMS sync request-reply from stateless EJB
I have a system based on JMS queues that enable the whole system to work in开发者_JS百科 async fashion.
Now, for some periodic tasks I need to execute some non time consuming tasks in sync way, so that I can do something with the result right away, without using a database for storing request and pairing it with result (as with async operation).
The code itself is not a problem, but the fact that EJBs run container transactions, which means that the producer.send() code and consumer.receive() will not execute sequentially (i.e. message is not sent, so nothing to receive in the receive() few lines bellow).
How can I have JMS message sent and response received in sync fashion in stateless EJB3 environment?
Thanks for answers,
Bozo
You could switch your bean to use Bean-Managed Transactions.
@MessageDriven
@TransactionManagement(BEAN)
public class MyMDB implements MessageListener {
// this is optional
@Resource
private UserTransaction ut;
//...
}
You should be aware of transaction contexts in EJBs that affect sending request/reply messages via JMS.
"The solution would be to suspend the transaction, create a new transaction, send the request, commit the new transaction, unsuspend the first transaction, and continue."
In the URL below, you will find the problem statement and a solution. BTW, this applies to both container managed and bean managed transactions ..
http://blogs.oracle.com/fkieviet/entry/request_reply_from_an_ejb
精彩评论