Search in sources :

Example 21 with XAConnection

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

the class XATest method test2PCReceiveCommit.

@Test
public void test2PCReceiveCommit() 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();
        // 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.commit();
        // New tx
        tm.begin();
        tx = tm.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(res2);
        Message m3 = cons.receive(100);
        Assert.assertNull(m3);
        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 22 with XAConnection

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

the class XATest method testMultipleSessionsOneTxCommitSend.

@Test
public void testMultipleSessionsOneTxCommitSend() 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();
        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);
        // 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();
        }
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) 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 23 with XAConnection

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

the class XATest method testOneSessionTwoTransactionsRollbackAcknowledge.

@Test
public void testOneSessionTwoTransactionsRollbackAcknowledge() 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());
        cons1.close();
        tx2.delistResource(res1, XAResource.TMSUCCESS);
        // rollback this transaction
        tm.rollback();
        // verify that second message is 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(5000);
        Assert.assertNotNull(r3);
        Assert.assertEquals("jellyfish2", r3.getText());
        r3 = (TextMessage) cons.receive(100);
        Assert.assertNull(r3);
        // rollback the other tx
        tm.resume(suspended);
        tm.rollback();
        // Verify the first message is now available
        r3 = (TextMessage) cons.receive(5000);
        Assert.assertNotNull(r3);
        Assert.assertEquals("jellyfish1", r3.getText());
        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 24 with XAConnection

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

the class XATest method testMultipleSessionsOneTxRollbackAcknowledgeForceFailureInCommit.

@Test
public void testMultipleSessionsOneTxRollbackAcknowledgeForceFailureInCommit() throws Exception {
    XAConnection conn = null;
    Connection conn2 = null;
    try {
        // First send 4 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();
        XASession sess1 = conn.createXASession();
        XAResource res1 = sess1.getXAResource();
        DummyXAResource res2 = new DummyXAResource(true);
        Transaction tx = tm.getTransaction();
        tx.enlistResource(res1);
        tx.enlistResource(res2);
        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());
        r1 = (TextMessage) cons1.receive(5000);
        Assert.assertNotNull(r1);
        Assert.assertEquals("jellyfish3", r1.getText());
        r1 = (TextMessage) cons1.receive(5000);
        Assert.assertNotNull(r1);
        Assert.assertEquals("jellyfish4", r1.getText());
        r1 = (TextMessage) cons1.receive(100);
        Assert.assertNull(r1);
        cons1.close();
        // try and commit - and we're going to make the dummyxaresource throw an exception on commit,
        // which should cause rollback to be called on the other resource
        tx.delistResource(res1, XAResource.TMSUCCESS);
        tx.delistResource(res2, XAResource.TMSUCCESS);
        // rollback will cause an attempt to deliver messages locally to the original consumers.
        // the original consumer has closed, so it will cancelled to the server
        // the server cancel is asynch, so we need to sleep for a bit to make sure it completes
        ExtrasTestLogger.LOGGER.trace("Forcing failure");
        try {
            tm.commit();
            Assert.fail("should not get here");
        } catch (Exception e) {
        // We should expect this
        }
        Thread.sleep(1000);
        Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer cons = sess.createConsumer(queue1);
        conn2.start();
        TextMessage 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(5000);
        Assert.assertNotNull(r);
        Assert.assertEquals("jellyfish3", r.getText());
        r = (TextMessage) cons.receive(5000);
        Assert.assertNotNull(r);
        Assert.assertEquals("jellyfish4", r.getText());
        r = (TextMessage) cons.receive(100);
        Assert.assertNull(r);
    } 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) XAException(javax.transaction.xa.XAException) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Example 25 with XAConnection

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

the class XATest method test2PCReceiveCommit1PCOptimization.

@Test
public void test2PCReceiveCommit1PCOptimization() throws Exception {
    // Since both resources have some RM, TM will probably use 1PC optimization
    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();
        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.commit();
        // New tx
        tm.begin();
        tx = tm.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(res2);
        Message m3 = cons.receive(100);
        Assert.assertNull(m3);
        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)

Aggregations

XAConnection (javax.jms.XAConnection)58 XASession (javax.jms.XASession)53 Test (org.junit.Test)42 MessageProducer (javax.jms.MessageProducer)40 MessageConsumer (javax.jms.MessageConsumer)35 Session (javax.jms.Session)35 TextMessage (javax.jms.TextMessage)34 XAResource (javax.transaction.xa.XAResource)29 Connection (javax.jms.Connection)28 Transaction (javax.transaction.Transaction)24 Message (javax.jms.Message)18 Xid (javax.transaction.xa.Xid)17 Queue (javax.jms.Queue)13 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)12 XAException (javax.transaction.xa.XAException)11 TemporaryQueue (javax.jms.TemporaryQueue)8 Destination (javax.jms.Destination)5 JMSException (javax.jms.JMSException)5 QueueSession (javax.jms.QueueSession)5 TopicSession (javax.jms.TopicSession)5