Search in sources :

Example 41 with XASession

use of javax.jms.XASession in project activemq-artemis by apache.

the class XATest method test2PCReceiveRollback.

@Test
public void test2PCReceiveRollback() throws Exception {
    XAConnection conn = null;
    Connection conn2 = null;
    try {
        conn2 = cf.createConnection();
        Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer prod = sessProducer.createProducer(queue1);
        Message m = sessProducer.createTextMessage("XATest1");
        prod.send(m);
        m = sessProducer.createTextMessage("XATest2");
        prod.send(m);
        conn = xacf.createXAConnection();
        conn.start();
        tm.begin();
        XASession sess = conn.createXASession();
        XAResource res = sess.getXAResource();
        // res.setForceNotSameRM(true);
        XAResource res2 = new DummyXAResource();
        Transaction tx = tm.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(res2);
        MessageConsumer cons = sess.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());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(res2, XAResource.TMSUCCESS);
        tm.rollback();
        // Message should be redelivered
        // New tx
        tm.begin();
        tx = tm.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(res2);
        TextMessage m3 = (TextMessage) cons.receive(5000);
        Assert.assertNotNull(m3);
        Assert.assertEquals("XATest1", m3.getText());
        m3 = (TextMessage) cons.receive(5000);
        Assert.assertNotNull(m3);
        Assert.assertEquals("XATest2", m3.getText());
        Assert.assertTrue(m3.getJMSRedelivered());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(res2, XAResource.TMSUCCESS);
        tm.commit();
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (conn2 != null) {
            conn2.close();
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Transaction(javax.transaction.Transaction) XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Example 42 with XASession

use of javax.jms.XASession in project activemq-artemis by apache.

the class XATest method test1PCReceiveCommit.

@Test
public void test1PCReceiveCommit() throws Exception {
    XAConnection conn = null;
    Connection conn2 = null;
    try {
        conn2 = cf.createConnection();
        conn2.start();
        Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer prod = sessProducer.createProducer(queue1);
        Message m = sessProducer.createTextMessage("XATest1");
        prod.send(m);
        m = sessProducer.createTextMessage("XATest2");
        prod.send(m);
        conn = xacf.createXAConnection();
        conn.start();
        tm.begin();
        XASession sess = conn.createXASession();
        XAResource res = sess.getXAResource();
        Transaction tx = tm.getTransaction();
        tx.enlistResource(res);
        MessageConsumer cons = sess.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());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tm.commit();
        // New tx
        tm.begin();
        tx = tm.getTransaction();
        tx.enlistResource(res);
        Message m3 = cons.receive(100);
        Assert.assertNull(m3);
        tx.delistResource(res, XAResource.TMSUCCESS);
        tm.commit();
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (conn2 != null) {
            conn2.close();
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Transaction(javax.transaction.Transaction) XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Example 43 with XASession

use of javax.jms.XASession in project activemq-artemis by apache.

the class XATest method test1PCSendCommit.

@Test
public void test1PCSendCommit() throws Exception {
    XAConnection conn = null;
    Connection conn2 = null;
    try {
        conn = xacf.createXAConnection();
        tm.begin();
        XASession sess = conn.createXASession();
        XAResource res = sess.getXAResource();
        Transaction tx = tm.getTransaction();
        tx.enlistResource(res);
        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);
        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();
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) MessageConsumer(javax.jms.MessageConsumer) Transaction(javax.transaction.Transaction) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Example 44 with XASession

use of javax.jms.XASession in project activemq-artemis by apache.

the class XATest method test2PCSendFailOnPrepare.

@Test
public void test2PCSendFailOnPrepare() 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(true);
        XAResource res3 = new DummyXAResource();
        XAResource res4 = new DummyXAResource();
        Transaction tx = tm.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(res2);
        tx.enlistResource(res3);
        tx.enlistResource(res4);
        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);
        tx.delistResource(res3, XAResource.TMSUCCESS);
        tx.delistResource(res4, XAResource.TMSUCCESS);
        try {
            tm.commit();
            Assert.fail("should not get here");
        } catch (Exception e) {
        // We should expect this
        }
        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();
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) MessageConsumer(javax.jms.MessageConsumer) Transaction(javax.transaction.Transaction) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) XAException(javax.transaction.xa.XAException) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Example 45 with XASession

use of javax.jms.XASession in project activemq-artemis by apache.

the class XATest method testOneSessionTwoTransactionsCommitAcknowledge.

@Test
public void testOneSessionTwoTransactionsCommitAcknowledge() 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();
        // Create a session
        XASession sess1 = conn.createXASession();
        XAResource res1 = sess1.getXAResource();
        conn.start();
        MessageConsumer cons1 = sess1.createConsumer(queue1);
        tm.begin();
        Transaction tx1 = tm.getTransaction();
        tx1.enlistResource(res1);
        // Receive one message in one tx
        TextMessage r1 = (TextMessage) cons1.receive(5000);
        Assert.assertNotNull(r1);
        Assert.assertEquals("jellyfish1", r1.getText());
        tx1.delistResource(res1, XAResource.TMSUCCESS);
        // suspend the tx
        Transaction suspended = tm.suspend();
        tm.begin();
        Transaction tx2 = tm.getTransaction();
        tx2.enlistResource(res1);
        // Receive 2nd message in a different tx
        TextMessage r2 = (TextMessage) cons1.receive(5000);
        Assert.assertNotNull(r2);
        Assert.assertEquals("jellyfish2", r2.getText());
        tx2.delistResource(res1, XAResource.TMSUCCESS);
        // commit this transaction
        tm.commit();
        // verify that no messages are available
        conn2.close();
        conn2 = cf.createConnection();
        Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        conn2.start();
        MessageConsumer cons = sess.createConsumer(queue1);
        TextMessage r3 = (TextMessage) cons.receive(100);
        Assert.assertNull(r3);
        // now resume the first tx and then commit it
        tm.resume(suspended);
        tm.commit();
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (conn2 != null) {
            conn2.close();
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Transaction(javax.transaction.Transaction) XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

XASession (javax.jms.XASession)58 XAConnection (javax.jms.XAConnection)51 MessageProducer (javax.jms.MessageProducer)41 Test (org.junit.Test)41 MessageConsumer (javax.jms.MessageConsumer)36 Session (javax.jms.Session)35 TextMessage (javax.jms.TextMessage)35 XAResource (javax.transaction.xa.XAResource)34 Connection (javax.jms.Connection)26 Transaction (javax.transaction.Transaction)24 Message (javax.jms.Message)19 Xid (javax.transaction.xa.Xid)19 Queue (javax.jms.Queue)14 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)12 XAException (javax.transaction.xa.XAException)11 TemporaryQueue (javax.jms.TemporaryQueue)8 JMSException (javax.jms.JMSException)7 XidImpl (org.apache.activemq.artemis.core.transaction.impl.XidImpl)6 QueueSession (javax.jms.QueueSession)5 TopicSession (javax.jms.TopicSession)5