Search in sources :

Example 36 with XASession

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

the class JMSXDeliveryCountTest method testDeliveryCountUpdatedOnCloseXA.

@Test
public void testDeliveryCountUpdatedOnCloseXA() throws Exception {
    XAConnection xaConn = null;
    Connection conn = null;
    TransactionManager mgr = new TransactionManagerImple();
    Transaction toResume = null;
    Transaction tx = null;
    try {
        toResume = mgr.suspend();
        conn = cf.createConnection();
        // Send a message
        Session producerSess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = producerSess.createProducer(queue1);
        TextMessage tm = producerSess.createTextMessage("message1");
        producer.send(tm);
        xaConn = xacf.createXAConnection();
        XASession consumerSess = xaConn.createXASession();
        MessageConsumer consumer = consumerSess.createConsumer(queue1);
        xaConn.start();
        DummyXAResource res = new DummyXAResource();
        mgr.begin();
        tx = mgr.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(consumerSess.getXAResource());
        TextMessage rm = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(rm);
        Assert.assertEquals(tm.getText(), rm.getText());
        Assert.assertEquals(1, rm.getIntProperty("JMSXDeliveryCount"));
        Assert.assertFalse(rm.getJMSRedelivered());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
        mgr.rollback();
        mgr.begin();
        tx = mgr.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(consumerSess.getXAResource());
        rm = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(rm);
        Assert.assertEquals(tm.getText(), rm.getText());
        Assert.assertEquals(2, rm.getIntProperty("JMSXDeliveryCount"));
        Assert.assertTrue(rm.getJMSRedelivered());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
        mgr.rollback();
        mgr.begin();
        tx = mgr.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(consumerSess.getXAResource());
        rm = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(rm);
        Assert.assertEquals(tm.getText(), rm.getText());
        Assert.assertEquals(3, rm.getIntProperty("JMSXDeliveryCount"));
        Assert.assertTrue(rm.getJMSRedelivered());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
        mgr.rollback();
        // Must close consumer first
        consumer.close();
        consumerSess.close();
        consumerSess = xaConn.createXASession();
        consumer = consumerSess.createConsumer(queue1);
        mgr.begin();
        tx = mgr.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(consumerSess.getXAResource());
        rm = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(rm);
        Assert.assertEquals(tm.getText(), rm.getText());
        Assert.assertEquals(4, rm.getIntProperty("JMSXDeliveryCount"));
        Assert.assertTrue(rm.getJMSRedelivered());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (tx != null) {
            try {
                mgr.commit();
            } catch (Exception ignore) {
            }
        }
        if (xaConn != null) {
            xaConn.close();
        }
        if (toResume != null) {
            try {
                mgr.resume(toResume);
            } catch (Exception ignore) {
            }
        }
    }
}
Also used : TransactionManagerImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple) MessageConsumer(javax.jms.MessageConsumer) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XAException(javax.transaction.xa.XAException) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Example 37 with XASession

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

the class XATest method testMultipleSessionsOneTxRollbackSend1PCOptimization.

@Test
public void testMultipleSessionsOneTxRollbackSend1PCOptimization() 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);
        // rollback
        tm.rollback();
        // Messages should not 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(100);
        Assert.assertNull(r1);
    } 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) 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 38 with XASession

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

the class XATest method testMultipleSessionsOneTxCommitAcknowledge1PCOptimization.

@Test
public void testMultipleSessionsOneTxCommitAcknowledge1PCOptimization() 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();
        XAResource res1 = sess1.getXAResource();
        XASession sess2 = conn.createXASession();
        XAResource res2 = sess2.getXAResource();
        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();
        }
    }
}
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 39 with XASession

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

the class XATest method test1PCReceiveRollback.

@Test
public void test1PCReceiveRollback() 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();
        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.rollback();
        // Message should be redelivered
        // New tx
        tm.begin();
        tx = tm.getTransaction();
        tx.enlistResource(res);
        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);
        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 40 with XASession

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

the class XATest method testMultipleSessionsOneTxRollbackAcknowledge1PCOptimization.

@Test
public void testMultipleSessionsOneTxRollbackAcknowledge1PCOptimization() 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();
        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();
        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());
        cons2.close();
        // rollback
        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();
        }
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) 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