Search in sources :

Example 56 with XidImpl

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

the class XaTimeoutTest method testTimeoutOnConsumerResend.

@Test
public void testTimeoutOnConsumerResend() throws Exception {
    int numberOfMessages = 100;
    String outQueue = "outQueue";
    {
        ClientSession simpleTXSession = sessionFactory.createTransactedSession();
        ClientProducer producerTX = simpleTXSession.createProducer(atestq);
        for (int i = 0; i < numberOfMessages; i++) {
            ClientMessage m = createTextMessage(clientSession, "m-" + i);
            m.putIntProperty("msg", i);
            producerTX.send(m);
        }
        simpleTXSession.commit();
        // This test needs 2 queues
        simpleTXSession.createQueue(outQueue, RoutingType.MULTICAST, outQueue);
        simpleTXSession.close();
    }
    final ClientSession outProducerSession = sessionFactory.createSession(true, false, false);
    final ClientProducer outProducer = outProducerSession.createProducer(outQueue);
    final AtomicInteger errors = new AtomicInteger(0);
    final AtomicInteger msgCount = new AtomicInteger(0);
    // since the first message will be rolled back
    final CountDownLatch latchReceives = new CountDownLatch(numberOfMessages + 1);
    outProducerSession.setTransactionTimeout(2);
    clientSession.setTransactionTimeout(2);
    MessageHandler handler = new MessageHandler() {

        @Override
        public void onMessage(ClientMessage message) {
            try {
                latchReceives.countDown();
                Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
                Xid xidOut = new XidImpl("xa2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
                clientSession.start(xid, XAResource.TMNOFLAGS);
                outProducerSession.start(xidOut, XAResource.TMNOFLAGS);
                message.acknowledge();
                int msgInt = message.getIntProperty("msg");
                ClientMessage msgOut = createTextMessage(outProducerSession, "outMsg=" + msgInt);
                msgOut.putIntProperty("msg", msgInt);
                outProducer.send(msgOut);
                boolean rollback = false;
                if (msgCount.getAndIncrement() == 0) {
                    rollback = true;
                    System.out.println("Forcing first message to time out");
                    Thread.sleep(5000);
                }
                try {
                    clientSession.end(xid, XAResource.TMSUCCESS);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    outProducerSession.end(xidOut, XAResource.TMSUCCESS);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (rollback) {
                    try {
                        clientSession.rollback(xid);
                    } catch (Exception e) {
                        e.printStackTrace();
                        clientSession.rollback();
                    }
                    try {
                        outProducerSession.rollback(xidOut);
                    } catch (Exception e) {
                        e.printStackTrace();
                        outProducerSession.rollback();
                    }
                } else {
                    clientSession.prepare(xid);
                    outProducerSession.prepare(xidOut);
                    clientSession.commit(xid, false);
                    outProducerSession.commit(xidOut, false);
                }
            } catch (Exception e) {
                e.printStackTrace();
                errors.incrementAndGet();
            }
        }
    };
    clientConsumer.setMessageHandler(handler);
    clientSession.start();
    assertTrue(latchReceives.await(20, TimeUnit.SECONDS));
    clientConsumer.close();
    clientConsumer = clientSession.createConsumer(this.atestq);
    assertNull(clientConsumer.receiveImmediate());
    clientConsumer.close();
    clientConsumer = clientSession.createConsumer(outQueue);
    HashSet<Integer> msgsIds = new HashSet<>();
    for (int i = 0; i < numberOfMessages; i++) {
        ClientMessage msg = clientConsumer.receive(1000);
        assertNotNull(msg);
        msg.acknowledge();
        msgsIds.add(msg.getIntProperty("msg"));
    }
    assertNull(clientConsumer.receiveImmediate());
    for (int i = 0; i < numberOfMessages; i++) {
        assertTrue(msgsIds.contains(i));
    }
    outProducerSession.close();
}
Also used : MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) XAException(javax.transaction.xa.XAException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Xid(javax.transaction.xa.Xid) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 57 with XidImpl

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

the class LVQRecoveryTest method testMultipleMessagesAfterRecovery.

@Test
public void testMultipleMessagesAfterRecovery() throws Exception {
    Xid xid = new XidImpl("bq1".getBytes(), 4, "gtid1".getBytes());
    ClientProducer producer = clientSessionXa.createProducer(address);
    SimpleString messageId1 = new SimpleString("SMID1");
    SimpleString messageId2 = new SimpleString("SMID2");
    clientSessionXa.start(xid, XAResource.TMNOFLAGS);
    ClientMessage m1 = createTextMessage(clientSession, "m1");
    m1.putStringProperty(Message.HDR_LAST_VALUE_NAME, messageId1);
    ClientMessage m2 = createTextMessage(clientSession, "m2");
    m2.putStringProperty(Message.HDR_LAST_VALUE_NAME, messageId2);
    ClientMessage m3 = createTextMessage(clientSession, "m3");
    m3.putStringProperty(Message.HDR_LAST_VALUE_NAME, messageId1);
    ClientMessage m4 = createTextMessage(clientSession, "m4");
    m4.putStringProperty(Message.HDR_LAST_VALUE_NAME, messageId2);
    producer.send(m1);
    producer.send(m2);
    producer.send(m3);
    producer.send(m4);
    clientSessionXa.end(xid, XAResource.TMSUCCESS);
    clientSessionXa.prepare(xid);
    clientSession.close();
    clientSessionXa.close();
    restartServer();
    clientSessionXa.commit(xid, false);
    ClientConsumer consumer = clientSession.createConsumer(qName1);
    clientSession.start();
    ClientMessage m = consumer.receive(1000);
    Assert.assertNotNull(m);
    m.acknowledge();
    Assert.assertEquals(m.getBodyBuffer().readString(), "m3");
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    m.acknowledge();
    Assert.assertEquals(m.getBodyBuffer().readString(), "m4");
}
Also used : Xid(javax.transaction.xa.Xid) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 58 with XidImpl

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

the class ActiveMQServerControlTest method testCommitPreparedTransactions.

@Test
public void testCommitPreparedTransactions() throws Exception {
    SimpleString recQueue = new SimpleString("BasicXaTestqRec");
    SimpleString sendQueue = new SimpleString("BasicXaTestqSend");
    byte[] globalTransactionId = UUIDGenerator.getInstance().generateStringUUID().getBytes();
    Xid xid = new XidImpl("xa1".getBytes(), 1, globalTransactionId);
    Xid xid2 = new XidImpl("xa2".getBytes(), 1, globalTransactionId);
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory csf = createSessionFactory(locator);
    ClientSession clientSession = csf.createSession(true, false, false);
    clientSession.createQueue(recQueue, recQueue, null, true);
    clientSession.createQueue(sendQueue, sendQueue, null, true);
    ClientMessage m1 = createTextMessage(clientSession, "");
    m1.putStringProperty("m1", "m1");
    ClientProducer clientProducer = clientSession.createProducer(recQueue);
    clientProducer.send(m1);
    locator.close();
    ServerLocator receiveLocator = createInVMNonHALocator();
    ClientSessionFactory receiveCsf = createSessionFactory(receiveLocator);
    ClientSession receiveClientSession = receiveCsf.createSession(true, false, false);
    ClientConsumer consumer = receiveClientSession.createConsumer(recQueue);
    ServerLocator sendLocator = createInVMNonHALocator();
    ClientSessionFactory sendCsf = createSessionFactory(sendLocator);
    ClientSession sendClientSession = sendCsf.createSession(true, false, false);
    ClientProducer producer = sendClientSession.createProducer(sendQueue);
    receiveClientSession.start(xid, XAResource.TMNOFLAGS);
    receiveClientSession.start();
    sendClientSession.start(xid2, XAResource.TMNOFLAGS);
    ClientMessage m = consumer.receive(5000);
    assertNotNull(m);
    producer.send(m);
    receiveClientSession.end(xid, XAResource.TMSUCCESS);
    sendClientSession.end(xid2, XAResource.TMSUCCESS);
    receiveClientSession.prepare(xid);
    sendClientSession.prepare(xid2);
    ActiveMQServerControl serverControl = createManagementControl();
    sendLocator.close();
    receiveLocator.close();
    boolean success = serverControl.commitPreparedTransaction(XidImpl.toBase64String(xid));
    success = serverControl.commitPreparedTransaction(XidImpl.toBase64String(xid));
    System.out.println("ActiveMQServerControlTest.testCommitPreparedTransactions");
}
Also used : Xid(javax.transaction.xa.Xid) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 59 with XidImpl

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

the class SimpleOpenWireTest method testXAResourceRolledBackRemoved.

@Test
public void testXAResourceRolledBackRemoved() 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().rollback(xid);
    }
    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)

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