Let's say I write the following code (pure standalone Java with Atomikos, no Spring, no JavaEE, no beans):
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. 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
- if the message doesn't come for a long time the broker will time out the transaction