use of javax.jms.XAConnection in project activemq-artemis by apache.
the class XATest method test2PCSendRollback.
@Test
public void test2PCSendRollback() throws Exception {
XAConnection conn = null;
Connection conn2 = null;
try {
conn = xacf.createXAConnection();
tm.begin();
XASession sess = conn.createXASession();
XAResource res = sess.getXAResource();
// prevent 1Pc optimisation
// res.setForceNotSameRM(true);
XAResource res2 = new DummyXAResource();
Transaction tx = tm.getTransaction();
tx.enlistResource(res);
tx.enlistResource(res2);
MessageProducer prod = sess.createProducer(null);
prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
Message m = sess.createTextMessage("XATest1");
prod.send(queue1, m);
m = sess.createTextMessage("XATest2");
prod.send(queue1, m);
tx.delistResource(res, XAResource.TMSUCCESS);
tx.delistResource(res2, XAResource.TMSUCCESS);
tm.rollback();
conn2 = cf.createConnection();
conn2.start();
Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = sessReceiver.createConsumer(queue1);
Message m2 = cons.receive(100);
Assert.assertNull(m2);
} 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 testMultipleSessionsOneTxRollbackAcknowledge.
@Test
public void testMultipleSessionsOneTxRollbackAcknowledge() throws Exception {
XAConnection conn = null;
Connection conn2 = null;
try {
// First send 2 messages
conn2 = cf.createConnection();
Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = sessProducer.createProducer(queue1);
Message m = sessProducer.createTextMessage("jellyfish1");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish2");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish3");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish4");
prod.send(m);
conn = xacf.createXAConnection();
conn.start();
tm.begin();
// Create 2 sessions and enlist them
XASession sess1 = conn.createXASession();
ClientSessionInternal res1 = (ClientSessionInternal) sess1.getXAResource();
XASession sess2 = conn.createXASession();
ClientSessionInternal res2 = (ClientSessionInternal) sess2.getXAResource();
res1.setForceNotSameRM(true);
res2.setForceNotSameRM(true);
Transaction tx = tm.getTransaction();
tx.enlistResource(res1);
tx.enlistResource(res2);
// Receive the messages, two on each consumer
MessageConsumer cons1 = sess1.createConsumer(queue1);
TextMessage r1 = (TextMessage) cons1.receive(5000);
Assert.assertNotNull(r1);
Assert.assertEquals("jellyfish1", r1.getText());
r1 = (TextMessage) cons1.receive(5000);
Assert.assertNotNull(r1);
Assert.assertEquals("jellyfish2", r1.getText());
cons1.close();
// Cancel is asynch
Thread.sleep(500);
MessageConsumer cons2 = sess2.createConsumer(queue1);
TextMessage r2 = (TextMessage) cons2.receive(5000);
Assert.assertNotNull(r2);
Assert.assertEquals("jellyfish3", r2.getText());
r2 = (TextMessage) cons2.receive(5000);
Assert.assertNotNull(r2);
Assert.assertEquals("jellyfish4", r2.getText());
// rollback
cons2.close();
tx.delistResource(res1, XAResource.TMSUCCESS);
tx.delistResource(res2, XAResource.TMSUCCESS);
tm.rollback();
// Rollback causes cancel which is asynch
Thread.sleep(1000);
// We cannot assume anything about the order in which the transaction manager rollsback
// the sessions - this is implementation dependent
Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = sess.createConsumer(queue1);
conn2.start();
TextMessage r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
boolean session1First = false;
if (r.getText().equals("jellyfish1")) {
session1First = true;
} else if (r.getText().equals("jellyfish3")) {
session1First = false;
} else {
Assert.fail("Unexpected message");
}
if (session1First) {
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish2", r.getText());
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish3", r.getText());
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish4", r.getText());
} else {
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish4", r.getText());
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish1", r.getText());
r = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r);
Assert.assertEquals("jellyfish2", r.getText());
}
r = (TextMessage) cons.receive(100);
Assert.assertNull(r);
} 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 testMultipleSessionsOneTxCommitAcknowledge.
@Test
public void testMultipleSessionsOneTxCommitAcknowledge() throws Exception {
XAConnection conn = null;
Connection conn2 = null;
try {
// First send 2 messages
conn2 = cf.createConnection();
Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prod = sessProducer.createProducer(queue1);
Message m = sessProducer.createTextMessage("jellyfish1");
prod.send(m);
m = sessProducer.createTextMessage("jellyfish2");
prod.send(m);
conn = xacf.createXAConnection();
conn.start();
tm.begin();
// Create 2 sessions and enlist them
XASession sess1 = conn.createXASession();
ClientSessionInternal res1 = (ClientSessionInternal) sess1.getXAResource();
XASession sess2 = conn.createXASession();
ClientSessionInternal res2 = (ClientSessionInternal) sess2.getXAResource();
res1.setForceNotSameRM(true);
res2.setForceNotSameRM(true);
Transaction tx = tm.getTransaction();
tx.enlistResource(res1);
tx.enlistResource(res2);
// Receive the messages, one on each consumer
MessageConsumer cons1 = sess1.createConsumer(queue1);
TextMessage r1 = (TextMessage) cons1.receive(5000);
Assert.assertNotNull(r1);
Assert.assertEquals("jellyfish1", r1.getText());
cons1.close();
MessageConsumer cons2 = sess2.createConsumer(queue1);
TextMessage r2 = (TextMessage) cons2.receive(5000);
Assert.assertNotNull(r2);
Assert.assertEquals("jellyfish2", r2.getText());
tx.delistResource(res1, XAResource.TMSUCCESS);
tx.delistResource(res2, XAResource.TMSUCCESS);
// commit
tm.commit();
Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer cons = sess.createConsumer(queue1);
conn2.start();
TextMessage r3 = (TextMessage) cons.receive(100);
Assert.assertNull(r3);
} 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 testOneSessionTwoTransactionsCommitSend.
@Test
public void testOneSessionTwoTransactionsCommitSend() throws Exception {
XAConnection conn = null;
Connection conn2 = null;
try {
conn = xacf.createXAConnection();
// Create a session
XASession sess1 = conn.createXASession();
XAResource res1 = sess1.getXAResource();
MessageProducer prod1 = sess1.createProducer(queue1);
tm.begin();
Transaction tx1 = tm.getTransaction();
tx1.enlistResource(res1);
// Send a message
prod1.send(sess1.createTextMessage("kangaroo1"));
tx1.delistResource(res1, XAResource.TMSUCCESS);
// suspend the tx
Transaction suspended = tm.suspend();
tm.begin();
// Send another message in another tx using the same session
Transaction tx2 = tm.getTransaction();
tx2.enlistResource(res1);
// Send a message
prod1.send(sess1.createTextMessage("kangaroo2"));
tx2.delistResource(res1, XAResource.TMSUCCESS);
// commit this transaction
tm.commit();
// verify only kangaroo2 message is sent
conn2 = cf.createConnection();
Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
conn2.start();
MessageConsumer cons = sess.createConsumer(queue1);
TextMessage r1 = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r1);
Assert.assertEquals("kangaroo2", r1.getText());
TextMessage r2 = (TextMessage) cons.receive(100);
Assert.assertNull(r2);
// now resume the first tx and then commit it
tm.resume(suspended);
tm.commit();
// verify that the first text message is received
TextMessage r3 = (TextMessage) cons.receive(5000);
Assert.assertNotNull(r3);
Assert.assertEquals("kangaroo1", r3.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 testIsSamRM.
@Test
public void testIsSamRM() throws Exception {
XAConnection conn = null;
conn = xacf.createXAConnection();
// Create a session
XASession sess1 = conn.createXASession();
XAResource res1 = sess1.getXAResource();
// Create a session
XASession sess2 = conn.createXASession();
XAResource res2 = sess2.getXAResource();
Assert.assertTrue(res1.isSameRM(res2));
}
Aggregations