use of javax.jms.XAConnection in project activemq-artemis by apache.
the class SimpleOpenWireTest method testCommitCloseConsumeXA.
@Test
public void testCommitCloseConsumeXA() throws Exception {
Queue queue;
{
connection.start();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
queue = session.createQueue(queueName);
MessageProducer producer = session.createProducer(queue);
for (int i = 0; i < 10; i++) {
TextMessage msg = session.createTextMessage("testXX" + i);
msg.setStringProperty("count", "str " + i);
producer.send(msg);
}
session.commit();
}
try (XAConnection xaconnection = xaFactory.createXAConnection()) {
xaconnection.start();
XASession xasession = xaconnection.createXASession();
Xid xid = newXID();
xasession.getXAResource().start(xid, XAResource.TMNOFLAGS);
MessageConsumer consumer = xasession.createConsumer(queue);
for (int i = 0; i < 5; i++) {
TextMessage txt = (TextMessage) consumer.receive(5000);
Assert.assertEquals("testXX" + i, txt.getText());
}
consumer.close();
xasession.getXAResource().end(xid, XAResource.TMSUCCESS);
xasession.getXAResource().prepare(xid);
xasession.getXAResource().commit(xid, false);
xaconnection.close();
}
{
connection.start();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
try (MessageConsumer consumer = session.createConsumer(queue)) {
for (int i = 5; i < 10; i++) {
TextMessage txt = (TextMessage) consumer.receive(5000);
Assert.assertEquals("testXX" + i, txt.getText());
}
}
}
}
use of javax.jms.XAConnection in project activemq-artemis by apache.
the class SimpleOpenWireTest method testXAResourceRolledBackSuspendedNotRemoved.
@Test
public void testXAResourceRolledBackSuspendedNotRemoved() throws Exception {
Queue queue = null;
Xid xid = newXID();
try (XAConnection xaconnection = xaFactory.createXAConnection()) {
XASession session = xaconnection.createXASession();
queue = session.createQueue(queueName);
session.getXAResource().start(xid, XAResource.TMNOFLAGS);
session.getXAResource().end(xid, XAResource.TMSUSPEND);
XidImpl xid1 = new XidImpl(xid);
Transaction transaction = server.getResourceManager().getTransaction(xid1);
// directly suspend the tx
transaction.suspend();
session.getXAResource().rollback(xid);
} catch (XAException ex) {
// ignore
} finally {
XidImpl xid1 = new XidImpl(xid);
Transaction transaction = server.getResourceManager().getTransaction(xid1);
assertNotNull(transaction);
}
}
use of javax.jms.XAConnection in project activemq-artemis by apache.
the class SimpleOpenWireTest method testXASameConsumerRollback.
@Test
public void testXASameConsumerRollback() throws Exception {
Queue queue;
try (Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE)) {
queue = session.createQueue(queueName);
System.out.println("Queue:" + queue);
MessageProducer producer = session.createProducer(queue);
for (int i = 0; i < 10; i++) {
TextMessage msg = session.createTextMessage("test" + i);
msg.setStringProperty("myobj", "test" + i);
producer.send(msg);
}
session.close();
}
try (XAConnection xaconnection = xaFactory.createXAConnection()) {
Xid xid = newXID();
XASession session = xaconnection.createXASession();
session.getXAResource().start(xid, XAResource.TMNOFLAGS);
MessageConsumer consumer = session.createConsumer(queue);
xaconnection.start();
for (int i = 0; i < 5; i++) {
TextMessage message = (TextMessage) consumer.receive(5000);
Assert.assertNotNull(message);
Assert.assertEquals("test" + i, message.getText());
}
session.getXAResource().end(xid, XAResource.TMSUCCESS);
session.getXAResource().rollback(xid);
xid = newXID();
session.getXAResource().start(xid, XAResource.TMNOFLAGS);
for (int i = 0; i < 10; i++) {
TextMessage message = (TextMessage) consumer.receive(5000);
Assert.assertNotNull(message);
Assert.assertEquals("test" + i, message.getText());
}
checkDuplicate(consumer);
session.getXAResource().end(xid, XAResource.TMSUCCESS);
session.getXAResource().commit(xid, true);
}
}
use of javax.jms.XAConnection in project activemq-artemis by apache.
the class XATest method testMultipleSessionsOneTxCommitSend1PCOptimization.
@Test
public void testMultipleSessionsOneTxCommitSend1PCOptimization() throws Exception {
// Since both resources have some RM, TM will probably use 1PC optimization
XAConnection conn = null;
Connection conn2 = null;
try {
conn = xacf.createXAConnection();
conn.start();
tm.begin();
// Create 2 sessions and enlist them
XASession sess1 = conn.createXASession();
XAResource res1 = sess1.getXAResource();
XASession sess2 = conn.createXASession();
XAResource res2 = sess2.getXAResource();
Transaction tx = tm.getTransaction();
tx.enlistResource(res1);
tx.enlistResource(res2);
// Send 2 messages - one from each session
MessageProducer prod1 = sess1.createProducer(queue1);
MessageProducer prod2 = sess2.createProducer(queue1);
prod1.send(sess1.createTextMessage("echidna1"));
prod2.send(sess2.createTextMessage("echidna2"));
tx.delistResource(res1, XAResource.TMSUCCESS);
tx.delistResource(res2, XAResource.TMSUCCESS);
// commit
tm.commit();
// Messages should be in queue
conn2 = cf.createConnection();
Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = sess.createConsumer(queue1);
conn2.start();
TextMessage r1 = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r1);
Assert.assertEquals("echidna1", r1.getText());
TextMessage r2 = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r2);
Assert.assertEquals("echidna2", r2.getText());
} finally {
if (conn != null) {
conn.close();
}
if (conn2 != null) {
conn2.close();
}
}
}
use of javax.jms.XAConnection in project activemq-artemis by apache.
the class XATest method test2PCSendCommit.
@Test
public void test2PCSendCommit() throws Exception {
XAConnection conn = null;
Connection conn2 = null;
try {
conn = xacf.createXAConnection();
tm.begin();
XASession sess = conn.createXASession();
XAResource res = sess.getXAResource();
XAResource res2 = new DummyXAResource();
// To prevent 1PC optimization being used
// res.setForceNotSameRM(true);
Transaction tx = tm.getTransaction();
tx.enlistResource(res);
tx.enlistResource(res2);
MessageProducer prod = sess.createProducer(queue1);
prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
Message m = sess.createTextMessage("XATest1");
prod.send(m);
m = sess.createTextMessage("XATest2");
prod.send(m);
tx.delistResource(res, XAResource.TMSUCCESS);
tx.delistResource(res2, XAResource.TMSUCCESS);
tm.commit();
conn2 = cf.createConnection();
conn2.start();
Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = sessReceiver.createConsumer(queue1);
TextMessage m2 = (TextMessage) cons.receive(5000);
Assert.assertNotNull(m2);
Assert.assertEquals("XATest1", m2.getText());
m2 = (TextMessage) cons.receive(5000);
Assert.assertNotNull(m2);
Assert.assertEquals("XATest2", m2.getText());
} finally {
if (conn != null) {
conn.close();
}
if (conn2 != null) {
conn2.close();
}
}
}
Aggregations