简体   繁体   English

JMS MessageListener可以启动XA事务吗?

[英]Can JMS MessageListener start XA transactions?

Let's say I write the following code (pure standalone Java with Atomikos, no Spring, no JavaEE, no beans): 假设我编写了以下代码(带有Atomikos的纯独立Java,没有Spring,没有JavaEE,没有bean):

XASession session = conn.createXASession();
MessageConsumer consumer = session.createConsumer(session.createQueue("QNAME"));
consumer.setMessageListener(new MessageListener() {
    @Override
    public void onMessage(Message message) {
        //some logic involving other XA resources
    }
});

It's obvious I haven't told my XASession about my TransactionManager or vice versa, so the message received doesn't belong to any transaction. 很明显,我没有告诉XASession我的TransactionManager ,反之亦然,所以收到的消息不属于任何事务。 Can I somehow change that? 我可以以某种方式改变它吗? I thought about doing this: 我考虑过这样做:

XASession session = conn.createXASession();
MessageConsumer consumer = session.createConsumer(session.createQueue("QNAME"));
Transaction tx;
tm.begin(); //tm is TransactionManager
tx = tm.getTransaction();
tx.enlistResource(session.getXAResource());
consumer.setMessageListener(new MessageListener() {
    @Override
    public void onMessage(Message message) {
        //some logic involving other XA resources
        tm.commit();
        tm.begin();
        tx = tm.getTransaction();
        tx.enlistResource(session.getXAResource());
    }
});

But I am worried that 但我担心

  • cross-thread XA transactions are not a thing 跨线程XA事务不是问题
  • if the message doesn't come for a long time the broker will time out the transaction 如果消息很长时间没有出现,则经纪人将使交易超时

I believe you'd need to implement some kind of wrapper (similar to what's done in Java EE and Spring) in order to coordinate with the transaction manager behind the scenes for every message received before your onMessage is invoked and then after onMessage is done. 我相信您需要实现某种包装器(类似于Java EE和Spring中的包装器),以便在调用onMessage 之前然后 onMessage完成之后与幕后的事务管理器协调每条收到的消息。 Interleaving the ending and beginning of different transactions in a single invocation of onMessage seems unlikely to turn out well if even function at all. 即使完全起作用,在onMessage的单次调用中交错不同事务的结束和开始似乎也不是一件容易的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM