Search in sources :

Example 31 with XidImpl

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

the class DuplicateDetectionTest method testXADuplicateDetection4.

@Test
public void testXADuplicateDetection4() 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.commit(xid, false);
    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);
    try {
        session.prepare(xid2);
        fail("Should throw an exception here!");
    } catch (XAException expected) {
        assertTrue(expected.getCause().toString().contains("DUPLICATE_ID_REJECTED"));
    }
    session.rollback(xid2);
    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) XAException(javax.transaction.xa.XAException) 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 32 with XidImpl

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

the class DuplicateDetectionTest method testXADuplicateDetectionPrepareAndRollbackStopServer.

@Test
public void testXADuplicateDetectionPrepareAndRollbackStopServer() 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, true);
    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();
    server.stop();
    waitForServerToStop(server);
    server.start();
    sf = createSessionFactory(locator);
    session = sf.createSession(true, false, false);
    session.start(xid, XAResource.TMJOIN);
    session.end(xid, XAResource.TMSUCCESS);
    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 33 with XidImpl

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

the class DuplicateDetectionTest method testXADuplicateDetection1.

@Test
public void testXADuplicateDetection1() 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.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)

Example 34 with XidImpl

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

the class DuplicateDetectionTest method testNoPersistXA1.

@Test
public void testNoPersistXA1() throws Exception {
    server.stop();
    config = createDefaultInVMConfig().setIDCacheSize(cacheSize).setPersistIDCache(false);
    server = createServer(config);
    server.start();
    sf = createSessionFactory(locator);
    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);
    ClientConsumer consumer = session.createConsumer(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.prepare(xid);
    session.commit(xid, false);
    session.close();
    sf.close();
    server.stop();
    waitForServerToStop(server);
    server.start();
    sf = createSessionFactory(locator);
    session = 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);
    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 35 with XidImpl

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

the class DuplicateDetectionTest method testPersistXA1.

@Test
public void testPersistXA1() throws Exception {
    ClientSession session = addClientSession(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);
    ClientConsumer consumer = session.createConsumer(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.prepare(xid);
    session.commit(xid, false);
    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);
    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);
    try {
        session.prepare(xid2);
        fail("Should throw an exception here!");
    } catch (XAException expected) {
    }
    session.rollback(xid2);
    Xid xid3 = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    session.start(xid3, XAResource.TMNOFLAGS);
    ClientMessage message2 = consumer.receiveImmediate();
    Assert.assertNull(message2);
    message2 = consumer.receiveImmediate();
    Assert.assertNull(message2);
}
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) 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