Search in sources :

Example 11 with XAConnection

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());
            }
        }
    }
}
Also used : Xid(javax.transaction.xa.Xid) MessageConsumer(javax.jms.MessageConsumer) XASession(javax.jms.XASession) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) XASession(javax.jms.XASession) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.ActiveMQSession) TopicSession(javax.jms.TopicSession) QueueSession(javax.jms.QueueSession) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 12 with XAConnection

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);
    }
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) XASession(javax.jms.XASession) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 13 with XAConnection

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);
    }
}
Also used : Xid(javax.transaction.xa.Xid) MessageConsumer(javax.jms.MessageConsumer) XASession(javax.jms.XASession) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) XASession(javax.jms.XASession) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.ActiveMQSession) TopicSession(javax.jms.TopicSession) QueueSession(javax.jms.QueueSession) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 14 with XAConnection

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();
        }
    }
}
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 15 with XAConnection

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();
        }
    }
}
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)

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