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();
}
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");
}
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");
}
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);
}
Aggregations