Search in sources :

Example 16 with XidImpl

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

the class LVQRecoveryTest method testManyMessagesReceivedWithRollback.

@Test
public void testManyMessagesReceivedWithRollback() throws Exception {
    Xid xid = new XidImpl("bq1".getBytes(), 4, "gtid1".getBytes());
    ClientProducer producer = clientSession.createProducer(address);
    ClientConsumer consumer = clientSessionXa.createConsumer(qName1);
    SimpleString rh = new SimpleString("SMID1");
    ClientMessage m1 = createTextMessage(clientSession, "m1");
    m1.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m1.setDurable(true);
    ClientMessage m2 = createTextMessage(clientSession, "m2");
    m2.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m2.setDurable(true);
    ClientMessage m3 = createTextMessage(clientSession, "m3");
    m3.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m3.setDurable(true);
    ClientMessage m4 = createTextMessage(clientSession, "m4");
    m4.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m4.setDurable(true);
    ClientMessage m5 = createTextMessage(clientSession, "m5");
    m5.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m5.setDurable(true);
    ClientMessage m6 = createTextMessage(clientSession, "m6");
    m6.putStringProperty(Message.HDR_LAST_VALUE_NAME, rh);
    m6.setDurable(true);
    clientSessionXa.start(xid, XAResource.TMNOFLAGS);
    clientSessionXa.start();
    producer.send(m1);
    ClientMessage m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m1");
    producer.send(m2);
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m2");
    producer.send(m3);
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m3");
    producer.send(m4);
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m4");
    producer.send(m5);
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m5");
    producer.send(m6);
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "m6");
    clientSessionXa.end(xid, XAResource.TMSUCCESS);
    clientSessionXa.prepare(xid);
    clientSession.close();
    clientSessionXa.close();
    restartServer();
    clientSessionXa.rollback(xid);
    consumer = clientSession.createConsumer(qName1);
    clientSession.start();
    m = consumer.receive(1000);
    Assert.assertNotNull(m);
    m.acknowledge();
    Assert.assertEquals(m.getBodyBuffer().readString(), "m6");
    m = consumer.receiveImmediate();
    Assert.assertNull(m);
}
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 17 with XidImpl

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

the class DuplicateDetectionTest method testXADuplicateDetection3.

@Test
public void testXADuplicateDetection3() throws Exception {
    ClientSession session = sf.createSession(true, false, false);
    Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session.start(xid, XAResource.TMNOFLAGS);
    session.start();
    final SimpleString queueName = new SimpleString("DuplicateDetectionTestQueue");
    session.createQueue(queueName, queueName, null, false);
    ClientProducer producer = session.createProducer(queueName);
    ClientMessage message = createMessage(session, 0);
    SimpleString dupID = new SimpleString("abcdefg");
    message.putBytesProperty(Message.HDR_DUPLICATE_DETECTION_ID, dupID.getData());
    producer.send(message);
    session.end(xid, XAResource.TMSUCCESS);
    session.prepare(xid);
    session.close();
    Xid xid2 = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session = sf.createSession(true, false, false);
    session.start(xid2, XAResource.TMNOFLAGS);
    session.start();
    producer = session.createProducer(queueName);
    ClientConsumer consumer = session.createConsumer(queueName);
    // Should NOT be able to resend it
    message = createMessage(session, 1);
    message.putBytesProperty(Message.HDR_DUPLICATE_DETECTION_ID, dupID.getData());
    producer.send(message);
    session.end(xid2, XAResource.TMSUCCESS);
    session.prepare(xid2);
    session.commit(xid2, false);
    Xid xid3 = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session.start(xid3, XAResource.TMNOFLAGS);
    consumer.receive(250);
    message = consumer.receiveImmediate();
    Assert.assertNull(message);
    DuplicateDetectionTest.log.info("ending session");
    session.end(xid3, XAResource.TMSUCCESS);
    DuplicateDetectionTest.log.info("preparing session");
    session.prepare(xid3);
    DuplicateDetectionTest.log.info("committing session");
    session.commit(xid3, false);
}
Also used : Xid(javax.transaction.xa.Xid) 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) 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 18 with XidImpl

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

the class DuplicateDetectionTest method testNoPersistXA2.

@Test
public void testNoPersistXA2() throws Exception {
    ClientSession session = sf.createSession(true, false, false);
    Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session.start(xid, XAResource.TMNOFLAGS);
    session.start();
    final SimpleString queueName = new SimpleString("DuplicateDetectionTestQueue");
    session.createQueue(queueName, queueName, null, false);
    ClientProducer producer = session.createProducer(queueName);
    ClientMessage message = createMessage(session, 1);
    SimpleString dupID = new SimpleString("abcdefg");
    message.putBytesProperty(Message.HDR_DUPLICATE_DETECTION_ID, dupID.getData());
    producer.send(message);
    message = createMessage(session, 2);
    SimpleString dupID2 = new SimpleString("hijklmnopqr");
    message.putBytesProperty(Message.HDR_DUPLICATE_DETECTION_ID, dupID2.getData());
    producer.send(message);
    session.end(xid, XAResource.TMSUCCESS);
    session.close();
    sf.close();
    server.stop();
    waitForServerToStop(server);
    server.start();
    sf = createSessionFactory(locator);
    session = addClientSession(sf.createSession(true, false, false));
    Xid xid2 = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session.start(xid2, XAResource.TMNOFLAGS);
    session.start();
    session.createQueue(queueName, queueName, null, false);
    producer = session.createProducer(queueName);
    ClientConsumer consumer = session.createConsumer(queueName);
    message = createMessage(session, 1);
    message.putBytesProperty(Message.HDR_DUPLICATE_DETECTION_ID, dupID.getData());
    producer.send(message);
    message = createMessage(session, 2);
    message.putBytesProperty(Message.HDR_DUPLICATE_DETECTION_ID, dupID2.getData());
    producer.send(message);
    session.end(xid2, XAResource.TMSUCCESS);
    session.prepare(xid2);
    session.commit(xid2, false);
    Xid xid3 = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session.start(xid3, XAResource.TMNOFLAGS);
    ClientMessage message2 = consumer.receive(200);
    Assert.assertEquals(1, message2.getObjectProperty(propKey));
    message2 = consumer.receive(200);
    Assert.assertEquals(2, message2.getObjectProperty(propKey));
}
Also used : Xid(javax.transaction.xa.Xid) 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) 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 19 with XidImpl

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

the class DuplicateDetectionTest method testXADuplicateDetectionPrepareAndRollback.

@Test
public void testXADuplicateDetectionPrepareAndRollback() throws Exception {
    ClientSession session = sf.createSession(true, false, false);
    Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session.start(xid, XAResource.TMNOFLAGS);
    session.start();
    final SimpleString queueName = new SimpleString("DuplicateDetectionTestQueue");
    session.createQueue(queueName, queueName, null, false);
    ClientProducer producer = session.createProducer(queueName);
    ClientMessage message = createMessage(session, 0);
    SimpleString dupID = new SimpleString("abcdefg");
    message.putBytesProperty(Message.HDR_DUPLICATE_DETECTION_ID, dupID.getData());
    producer.send(message);
    session.end(xid, XAResource.TMSUCCESS);
    session.prepare(xid);
    session.rollback(xid);
    session.close();
    Xid xid2 = new XidImpl("xa2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session = sf.createSession(true, false, false);
    session.start(xid2, XAResource.TMNOFLAGS);
    session.start();
    producer = session.createProducer(queueName);
    producer.send(message);
    session.end(xid2, XAResource.TMSUCCESS);
    session.prepare(xid2);
    session.commit(xid2, false);
    session.close();
    session = sf.createSession(false, false, false);
    session.start();
    ClientConsumer consumer = session.createConsumer(queueName);
    ClientMessage msgRec = consumer.receive(5000);
    assertNotNull(msgRec);
    msgRec.acknowledge();
    session.commit();
}
Also used : Xid(javax.transaction.xa.Xid) 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) 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 20 with XidImpl

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

the class DuplicateDetectionTest method testXADuplicateDetection2.

@Test
public void testXADuplicateDetection2() throws Exception {
    ClientSession session = sf.createSession(true, false, false);
    Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session.start(xid, XAResource.TMNOFLAGS);
    session.start();
    final SimpleString queueName = new SimpleString("DuplicateDetectionTestQueue");
    session.createQueue(queueName, queueName, null, false);
    ClientProducer producer = session.createProducer(queueName);
    ClientMessage message = createMessage(session, 0);
    SimpleString dupID = new SimpleString("abcdefg");
    message.putBytesProperty(Message.HDR_DUPLICATE_DETECTION_ID, dupID.getData());
    producer.send(message);
    session.end(xid, XAResource.TMSUCCESS);
    session.rollback(xid);
    session.close();
    Xid xid2 = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session = sf.createSession(true, false, false);
    session.start(xid2, XAResource.TMNOFLAGS);
    session.start();
    producer = session.createProducer(queueName);
    ClientConsumer consumer = session.createConsumer(queueName);
    // Should be able to resend it and not get rejected since transaction didn't commit
    message = createMessage(session, 1);
    message.putBytesProperty(Message.HDR_DUPLICATE_DETECTION_ID, dupID.getData());
    producer.send(message);
    session.end(xid2, XAResource.TMSUCCESS);
    session.prepare(xid2);
    session.commit(xid2, false);
    Xid xid3 = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session.start(xid3, XAResource.TMNOFLAGS);
    message = consumer.receive(250);
    Assert.assertEquals(1, message.getObjectProperty(propKey));
    message = consumer.receiveImmediate();
    Assert.assertNull(message);
    DuplicateDetectionTest.log.info("ending session");
    session.end(xid3, XAResource.TMSUCCESS);
    DuplicateDetectionTest.log.info("preparing session");
    session.prepare(xid3);
    DuplicateDetectionTest.log.info("committing session");
    session.commit(xid3, false);
}
Also used : Xid(javax.transaction.xa.Xid) 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) 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)

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