[英]MDB activation for JMS message before the JTA transaction is committed
I need synchronize JTA transaction with send JMS message - MDB should be activated after client JTA transaction commit. 我需要将JTA事务与send JMS消息同步 - 在客户端JTA事务提交后应该激活MDB。 This should by possible when use XAConnectionFactory, but doesn't work in my example.
这应该在使用XAConnectionFactory时可能,但在我的示例中不起作用。
Example scenario: 示例场景:
Scenario is repeat until code < 10. I expect result: 场景重复直到代码<10。我期待结果:
START: 0
SEND: 1
END: 0
START: 1
SEND: 2
END: 1
START: 2
SEND: 3
END: 2
etc..
but currently I get: 但目前我得到:
...
START: 4
SEND: 5
END: 3
START: 5
SEND: 6
END: 4
START: 6
SEND: 7
END: 5
END: 6
My code: 我的代码:
Webservice client Web服务客户端
@WebMethod @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void publish() { TestQueueUtil.sendToQueue(0); }
TestQueueUtil (JMS client) TestQueueUtil(JMS客户端)
public static void sendToQueue(Integer code) { InitialContext initialContext; XAQueueConnection queueConnection = null; XAQueueSession queueSession = null; try { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); initialContext = new InitialContext(env); XAConnectionFactory queueConnectionFactory = (XAConnectionFactory) initialContext.lookup("jms/dsk/ConnectionFactoryXA"); queueConnection = (XAQueueConnection) queueConnectionFactory.createXAConnection(); queueConnection.start(); queueSession = queueConnection.createXAQueueSession(); Queue queue = (Queue) initialContext.lookup("jms/dsk/TestQueue"); //QueueSender sender = MessageProducer producer = queueSession.createProducer(queue); Message jmsMessage = queueSession.createMessage(); jmsMessage.setIntProperty("code", code); producer.send(jmsMessage); producer.close(); queueConnection.stop(); } catch (Exception e) { throw new RuntimeException("sendToQueue", e); } finally { if (queueSession != null) { try { queueSession.close(); } catch (Exception e) { //ignore } } if (queueConnection != null) { try { queueConnection.close(); } catch (Exception e) { //ignore } } } }
TestQueueMDB TestQueueMDB
@MessageDriven(mappedName = "jms/dsk/TestQueue", activationConfig = { @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }) public class TestQueueMDB implements MessageListener { @Resource protected MessageDrivenContext messageDrivenContext; @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void onMessage(Message message) { Integer code = null; try { code = message.getIntProperty("code"); System.out.println("START: " + code); if (code < 10) { Integer newcode = code + 1; System.out.println("SEND: " + newcode); TestQueueUtil.sendToQueue(newcode); Thread.sleep(2000); } } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("END: " + code); } } }
What I do wrong ? 我做错了什么?
I found that I haven't transaction context on MDB !!! 我发现MDB上没有事务上下文!
When I checked transaction ID by calling 当我通过调用检查事务ID
weblogic.transaction.TxHelper.getTransactionId()
received null, and when call messageDrivenContext.getRollbackOnly() get exception 收到null,并在调用messageDrivenContext.getRollbackOnly()时获得异常
java.lang.IllegalStateException: [EJB:010156]Illegal attempt to call EJBContext.getRollbackOnly() from an EJB that was not participating in a transaction.
Reason of that was annotation 原因是注释
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
After remove it or change on 删除或更改后
@TransactionAttribute(TransactionAttributeType.REQUIRED)
all working fine. 一切正常。
:) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.