Search in sources :

Example 51 with XidImpl

use of org.apache.activemq.artemis.core.transaction.impl.XidImpl in project activemq-artemis by apache.

the class XaTimeoutTest method testSimpleTimeoutOnReceive.

@Test
public void testSimpleTimeoutOnReceive() throws Exception {
    Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    ClientMessage m1 = createTextMessage(clientSession, "m1");
    ClientMessage m2 = createTextMessage(clientSession, "m2");
    ClientMessage m3 = createTextMessage(clientSession, "m3");
    ClientMessage m4 = createTextMessage(clientSession, "m4");
    ClientSession clientSession2 = sessionFactory.createSession(false, true, true);
    ClientProducer clientProducer2 = clientSession2.createProducer(atestq);
    clientProducer2.send(m1);
    clientProducer2.send(m2);
    clientProducer2.send(m3);
    clientProducer2.send(m4);
    clientSession2.close();
    clientSession.setTransactionTimeout(2);
    clientSession.start(xid, XAResource.TMNOFLAGS);
    clientSession.start();
    ClientMessage m = clientConsumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m1");
    m = clientConsumer.receive(500);
    Assert.assertNotNull(m);
    m.acknowledge();
    Assert.assertEquals(m.getBodyBuffer().readString(), "m2");
    m = clientConsumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m3");
    m = clientConsumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m4");
    clientSession.end(xid, XAResource.TMSUCCESS);
    CountDownLatch latch = new CountDownLatch(1);
    server.getResourceManager().getTransaction(xid).addOperation(new RollbackCompleteOperation(latch));
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    try {
        clientSession.commit(xid, true);
    } catch (XAException e) {
        Assert.assertTrue(e.errorCode == XAException.XAER_NOTA);
    }
    clientSession.setTransactionTimeout(0);
    clientConsumer.close();
    clientSession2 = sessionFactory.createSession(false, true, true);
    ClientConsumer consumer = clientSession2.createConsumer(atestq);
    clientSession2.start();
    m = consumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m1");
    m = consumer.receive(500);
    Assert.assertNotNull(m);
    m.acknowledge();
    Assert.assertEquals(m.getBodyBuffer().readString(), "m2");
    m = consumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m3");
    m = consumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m4");
    clientSession2.close();
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 52 with XidImpl

use of org.apache.activemq.artemis.core.transaction.impl.XidImpl in project activemq-artemis by apache.

the class XaTimeoutTest method testChangeXID.

/**
 * In case a timeout happens the server's object may still have the previous XID.
 * for that reason a new start call is supposed to clean it up with a log.warn
 * but it should still succeed
 *
 * @throws Exception
 */
@Test
public void testChangeXID() throws Exception {
    Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    Xid xid2 = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    clientSession.start(xid, XAResource.TMNOFLAGS);
    clientSession.start(xid2, XAResource.TMNOFLAGS);
}
Also used : Xid(javax.transaction.xa.Xid) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) Test(org.junit.Test)

Example 53 with XidImpl

use of org.apache.activemq.artemis.core.transaction.impl.XidImpl in project activemq-artemis by apache.

the class XaTimeoutTest method testSimpleTimeoutOnSendAndReceive.

@Test
public void testSimpleTimeoutOnSendAndReceive() throws Exception {
    Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    ClientMessage m1 = createTextMessage(clientSession, "m1");
    ClientMessage m2 = createTextMessage(clientSession, "m2");
    ClientMessage m3 = createTextMessage(clientSession, "m3");
    ClientMessage m4 = createTextMessage(clientSession, "m4");
    ClientMessage m5 = createTextMessage(clientSession, "m5");
    ClientMessage m6 = createTextMessage(clientSession, "m6");
    ClientMessage m7 = createTextMessage(clientSession, "m7");
    ClientMessage m8 = createTextMessage(clientSession, "m8");
    ClientSession clientSession2 = sessionFactory.createSession(false, true, true);
    ClientProducer clientProducer2 = clientSession2.createProducer(atestq);
    clientProducer2.send(m1);
    clientProducer2.send(m2);
    clientProducer2.send(m3);
    clientProducer2.send(m4);
    clientSession2.close();
    clientSession.setTransactionTimeout(2);
    clientSession.start(xid, XAResource.TMNOFLAGS);
    clientSession.start();
    clientProducer.send(m5);
    clientProducer.send(m6);
    clientProducer.send(m7);
    clientProducer.send(m8);
    ClientMessage m = clientConsumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m1");
    m = clientConsumer.receive(500);
    Assert.assertNotNull(m);
    m.acknowledge();
    Assert.assertEquals(m.getBodyBuffer().readString(), "m2");
    m = clientConsumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m3");
    m = clientConsumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m4");
    clientSession.end(xid, XAResource.TMSUCCESS);
    CountDownLatch latch = new CountDownLatch(1);
    server.getResourceManager().getTransaction(xid).addOperation(new RollbackCompleteOperation(latch));
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    try {
        clientSession.commit(xid, true);
    } catch (XAException e) {
        Assert.assertTrue(e.errorCode == XAException.XAER_NOTA);
    }
    clientSession.setTransactionTimeout(0);
    clientConsumer.close();
    clientSession2 = sessionFactory.createSession(false, true, true);
    ClientConsumer consumer = clientSession2.createConsumer(atestq);
    clientSession2.start();
    m = consumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m1");
    m = consumer.receive(500);
    Assert.assertNotNull(m);
    m.acknowledge();
    Assert.assertEquals(m.getBodyBuffer().readString(), "m2");
    m = consumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m3");
    m = consumer.receive(500);
    m.acknowledge();
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m4");
    m = consumer.receiveImmediate();
    Assert.assertNull(m);
    clientSession2.close();
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 54 with XidImpl

use of org.apache.activemq.artemis.core.transaction.impl.XidImpl in project activemq-artemis by apache.

the class XaTimeoutTest method testMultipleTransactionsTimedOut.

@Test
public void testMultipleTransactionsTimedOut() throws Exception {
    Xid[] xids = new XidImpl[100];
    for (int i = 0; i < xids.length; i++) {
        xids[i] = new XidImpl(("xa" + i).getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    }
    ClientSession[] clientSessions = new ClientSession[xids.length];
    for (int i = 0; i < clientSessions.length; i++) {
        clientSessions[i] = sessionFactory.createSession(true, false, false);
        clientSessions[i].setTransactionTimeout(i < 50 ? 2 : 5000);
    }
    ClientProducer[] clientProducers = new ClientProducer[xids.length];
    for (int i = 0; i < clientProducers.length; i++) {
        clientProducers[i] = clientSessions[i].createProducer(atestq);
    }
    ClientMessage[] messages = new ClientMessage[xids.length];
    for (int i = 0; i < messages.length; i++) {
        messages[i] = createTextMessage(clientSession, "m" + i);
    }
    for (int i = 0; i < clientSessions.length; i++) {
        clientSessions[i].start(xids[i], XAResource.TMNOFLAGS);
    }
    for (int i = 0; i < clientProducers.length; i++) {
        clientProducers[i].send(messages[i]);
    }
    for (int i = 0; i < clientSessions.length; i++) {
        clientSessions[i].end(xids[i], XAResource.TMSUCCESS);
    }
    CountDownLatch[] latches = new CountDownLatch[xids.length];
    for (int i1 = 0; i1 < latches.length; i1++) {
        latches[i1] = new CountDownLatch(1);
        server.getResourceManager().getTransaction(xids[i1]).addOperation(new RollbackCompleteOperation(latches[i1]));
    }
    for (int i1 = 0; i1 < latches.length / 2; i1++) {
        Assert.assertTrue(latches[i1].await(5, TimeUnit.SECONDS));
    }
    for (int i = 0; i < clientSessions.length / 2; i++) {
        try {
            clientSessions[i].commit(xids[i], true);
        } catch (XAException e) {
            Assert.assertTrue(e.errorCode == XAException.XAER_NOTA);
        }
    }
    for (int i = 50; i < clientSessions.length; i++) {
        clientSessions[i].commit(xids[i], true);
    }
    for (ClientSession session : clientSessions) {
        session.close();
    }
    clientSession.start();
    for (int i = 0; i < clientSessions.length / 2; i++) {
        ClientMessage m = clientConsumer.receiveImmediate();
        Assert.assertNotNull(m);
    }
    ClientMessage m = clientConsumer.receiveImmediate();
    Assert.assertNull(m);
}
Also used : XAException(javax.transaction.xa.XAException) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Xid(javax.transaction.xa.Xid) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 55 with XidImpl

use of org.apache.activemq.artemis.core.transaction.impl.XidImpl in project activemq-artemis by apache.

the class XaTimeoutTest method testSimpleTimeoutOnSendOnCommit.

@Test
public void testSimpleTimeoutOnSendOnCommit() throws Exception {
    Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    ClientMessage m1 = createTextMessage(clientSession, "m1");
    ClientMessage m2 = createTextMessage(clientSession, "m2");
    ClientMessage m3 = createTextMessage(clientSession, "m3");
    ClientMessage m4 = createTextMessage(clientSession, "m4");
    clientSession.setTransactionTimeout(1);
    clientSession.start(xid, XAResource.TMNOFLAGS);
    clientProducer.send(m1);
    clientProducer.send(m2);
    clientProducer.send(m3);
    clientProducer.send(m4);
    clientSession.end(xid, XAResource.TMSUCCESS);
    CountDownLatch latch = new CountDownLatch(1);
    server.getResourceManager().getTransaction(xid).addOperation(new RollbackCompleteOperation(latch));
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    try {
        clientSession.commit(xid, true);
    } catch (XAException e) {
        Assert.assertTrue(e.errorCode == XAException.XAER_NOTA);
    }
    clientSession.start();
    ClientMessage m = clientConsumer.receiveImmediate();
    Assert.assertNull(m);
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

XidImpl (org.apache.activemq.artemis.core.transaction.impl.XidImpl)59 Xid (javax.transaction.xa.Xid)57 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)44 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)44 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)40 Test (org.junit.Test)39 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)35 XAException (javax.transaction.xa.XAException)21 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)21 CountDownLatch (java.util.concurrent.CountDownLatch)10 Queue (javax.jms.Queue)6 XASession (javax.jms.XASession)6 XAConnection (javax.jms.XAConnection)5 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)5 Transaction (org.apache.activemq.artemis.core.transaction.Transaction)5 MessageProducer (javax.jms.MessageProducer)4 TemporaryQueue (javax.jms.TemporaryQueue)4 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)4 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)3 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)3