Search in sources :

Example 11 with XASession

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

the class SimpleOpenWireTest method testXAResourceCommitSuspendedNotRemoved.

@Test
public void testXAResourceCommitSuspendedNotRemoved() 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);
        // amq5.x doesn't pass suspend flags to broker,
        // directly suspend the tx
        transaction.suspend();
        session.getXAResource().commit(xid, true);
    } 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 12 with XASession

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

the class SimpleOpenWireTest method testXAResourceCommittedRemoved.

@Test
public void testXAResourceCommittedRemoved() 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);
        MessageProducer producer = session.createProducer(queue);
        producer.send(session.createTextMessage("xa message"));
        session.getXAResource().end(xid, XAResource.TMSUCCESS);
        session.getXAResource().commit(xid, true);
    }
    XidImpl xid1 = new XidImpl(xid);
    Transaction transaction = server.getResourceManager().getTransaction(xid1);
    assertNull(transaction);
}
Also used : Xid(javax.transaction.xa.Xid) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) XASession(javax.jms.XASession) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) MessageProducer(javax.jms.MessageProducer) 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 XASession

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

the class SimpleOpenWireTest method testXASimple.

@Test
public void testXASimple() throws Exception {
    XAConnection connection = xaFactory.createXAConnection();
    Collection<Session> sessions = new LinkedList<>();
    for (int i = 0; i < 10; i++) {
        XASession session = connection.createXASession();
        session.getXAResource().start(newXID(), XAResource.TMNOFLAGS);
        sessions.add(session);
    }
    connection.close();
}
Also used : XASession(javax.jms.XASession) LinkedList(java.util.LinkedList) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.ActiveMQSession) TopicSession(javax.jms.TopicSession) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 14 with XASession

use of javax.jms.XASession 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 15 with XASession

use of javax.jms.XASession 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)

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