use of javax.jms.XASession in project activemq-artemis by apache.
the class SimpleMessageListener method main.
public static void main(final String[] args) throws Exception {
AtomicBoolean result = new AtomicBoolean(true);
final ArrayList<String> receiveHolder = new ArrayList<>();
XAConnection connection = null;
InitialContext initialContext = null;
try {
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
// Step 2. Lookup on the queue
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
// Step 3. Perform a lookup on the XA Connection Factory
XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("XAConnectionFactory");
// Step 4.Create a JMS XAConnection
connection = cf.createXAConnection();
// Step 5. Start the connection
connection.start();
// Step 6. Create a JMS XASession
XASession xaSession = connection.createXASession();
// Step 7. Create a normal session
Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 8. Create a normal Message Consumer
MessageConsumer normalConsumer = normalSession.createConsumer(queue);
normalConsumer.setMessageListener(new SimpleMessageListener(receiveHolder, result));
// Step 9. Get the JMS Session
Session session = xaSession.getSession();
// Step 10. Create a message producer
MessageProducer producer = session.createProducer(queue);
// Step 11. Create two Text Messages
TextMessage helloMessage = session.createTextMessage("hello");
TextMessage worldMessage = session.createTextMessage("world");
// Step 12. create a transaction
Xid xid1 = new DummyXid("xa-example1".getBytes(StandardCharsets.UTF_8), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
// Step 13. Get the JMS XAResource
XAResource xaRes = xaSession.getXAResource();
// Step 14. Begin the Transaction work
xaRes.start(xid1, XAResource.TMNOFLAGS);
// Step 15. do work, sending two messages.
producer.send(helloMessage);
producer.send(worldMessage);
Thread.sleep(2000);
// Step 16. Check the result, it should receive none!
checkNoMessageReceived(receiveHolder);
// Step 17. Stop the work
xaRes.end(xid1, XAResource.TMSUCCESS);
// Step 18. Prepare
xaRes.prepare(xid1);
// Step 19. Roll back the transaction
xaRes.rollback(xid1);
// Step 20. No messages should be received!
checkNoMessageReceived(receiveHolder);
// Step 21. Create another transaction
Xid xid2 = new DummyXid("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
// Step 22. Start the transaction
xaRes.start(xid2, XAResource.TMNOFLAGS);
// Step 23. Re-send those messages
producer.send(helloMessage);
producer.send(worldMessage);
// Step 24. Stop the work
xaRes.end(xid2, XAResource.TMSUCCESS);
// Step 25. Prepare
xaRes.prepare(xid2);
// Step 26. No messages should be received at this moment
checkNoMessageReceived(receiveHolder);
// Step 27. Commit!
xaRes.commit(xid2, false);
Thread.sleep(2000);
// Step 28. Check the result, all message received
checkAllMessageReceived(receiveHolder);
if (!result.get())
throw new IllegalStateException();
} finally {
// Step 29. Be sure to close our JMS resources!
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
}
}
use of javax.jms.XASession in project activemq-artemis by apache.
the class XAReceiveExample method main.
public static void main(final String[] args) throws Exception {
XAConnection connection = null;
InitialContext initialContext = null;
try {
// Step 1. Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext();
// Step 2. Lookup on the queue
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
// Step 3. Perform a lookup on the XA Connection Factory
XAConnectionFactory cf = (XAConnectionFactory) initialContext.lookup("XAConnectionFactory");
// Step 4.Create a JMS XAConnection
connection = cf.createXAConnection();
// Step 5. Start the connection
connection.start();
// Step 6. Create a JMS XASession
XASession xaSession = connection.createXASession();
// Step 7. Create a normal session
Session normalSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Step 8. Create a normal Message Producer
MessageProducer normalProducer = normalSession.createProducer(queue);
// Step 9. Get the JMS Session
Session session = xaSession.getSession();
// Step 10. Create a message consumer
MessageConsumer xaConsumer = session.createConsumer(queue);
// Step 11. Create two Text Messages
TextMessage helloMessage = session.createTextMessage("hello");
TextMessage worldMessage = session.createTextMessage("world");
// Step 12. create a transaction
Xid xid1 = new DummyXid("xa-example1".getBytes(StandardCharsets.US_ASCII), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
// Step 13. Get the JMS XAResource
XAResource xaRes = xaSession.getXAResource();
// Step 14. Begin the Transaction work
xaRes.start(xid1, XAResource.TMNOFLAGS);
// Step 15. Send two messages.
normalProducer.send(helloMessage);
normalProducer.send(worldMessage);
// Step 16. Receive the message
TextMessage rm1 = (TextMessage) xaConsumer.receive();
System.out.println("Message received: " + rm1.getText());
TextMessage rm2 = (TextMessage) xaConsumer.receive();
System.out.println("Message received: " + rm2.getText());
// Step 17. Stop the work
xaRes.end(xid1, XAResource.TMSUCCESS);
// Step 18. Prepare
xaRes.prepare(xid1);
// Step 19. Roll back the transaction
xaRes.rollback(xid1);
// Step 20. Create another transaction
Xid xid2 = new DummyXid("xa-example2".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
// Step 21. Start the transaction
xaRes.start(xid2, XAResource.TMNOFLAGS);
// Step 22. receive those messages again
rm1 = (TextMessage) xaConsumer.receive();
System.out.println("Message received again: " + rm1.getText());
rm2 = (TextMessage) xaConsumer.receive();
System.out.println("Message received again: " + rm2.getText());
// Step 23. Stop the work
xaRes.end(xid2, XAResource.TMSUCCESS);
// Step 24. Prepare
xaRes.prepare(xid2);
// Step 25. Commit!
xaRes.commit(xid2, false);
// Step 26. Check no more messages are received.
TextMessage rm3 = (TextMessage) xaConsumer.receive(2000);
if (rm3 == null) {
System.out.println("No message received after commit.");
} else {
throw new IllegalStateException();
}
} finally {
// Step 27. Be sure to close our JMS resources!
if (initialContext != null) {
initialContext.close();
}
if (connection != null) {
connection.close();
}
}
}
use of javax.jms.XASession in project activemq-artemis by apache.
the class JMSBridgeImpl method stopSessionFailover.
private void stopSessionFailover() {
XASession xaSource = (XASession) sourceSession;
XASession xaTarget = (XASession) targetSession;
((ClientSessionInternal) xaSource.getXAResource()).getSessionContext().releaseCommunications();
((ClientSessionInternal) xaTarget.getXAResource()).getSessionContext().releaseCommunications();
}
use of javax.jms.XASession in project activemq-artemis by apache.
the class TimeoutXATest method testTimeoutOnTX2.
@Test
@BMRules(rules = { @BMRule(name = "removing TX", targetClass = "org.apache.activemq.artemis.core.transaction.impl.ResourceManagerImpl", targetMethod = "removeTransaction(javax.transaction.xa.Xid)", targetLocation = "ENTRY", helper = "org.apache.activemq.artemis.tests.extras.byteman.TimeoutXATest", action = "removingTX()"), @BMRule(name = "afterRollback TX", targetClass = "org.apache.activemq.artemis.core.transaction.impl.TransactionImpl", targetMethod = "afterRollback", targetLocation = "ENTRY", helper = "org.apache.activemq.artemis.tests.extras.byteman.TimeoutXATest", action = "afterRollback()") })
public void testTimeoutOnTX2() throws Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
XAConnection connection = connectionFactory.createXAConnection();
Connection connction2 = connectionFactory.createConnection();
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Queue queue = session.createQueue("Queue1");
MessageProducer producer = session.createProducer(queue);
for (int i = 0; i < 10; i++) {
producer.send(session.createTextMessage("hello " + 1));
}
session.commit();
final XASession xaSession = connection.createXASession();
final Xid xid = newXID();
xaSession.getXAResource().start(xid, XAResource.TMNOFLAGS);
MessageConsumer consumer = xaSession.createConsumer(queue);
connection.start();
for (int i = 0; i < 10; i++) {
Assert.assertNotNull(consumer.receive(5000));
}
xaSession.getXAResource().end(xid, XAResource.TMSUCCESS);
final CountDownLatch latchStore = new CountDownLatch(1000);
Thread storingThread = new Thread() {
@Override
public void run() {
try {
for (int i = 0; i < 100000; i++) {
latchStore.countDown();
server.getStorageManager().storeDuplicateID(SimpleString.toSimpleString("crebis"), new byte[] { 1 }, server.getStorageManager().generateID());
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
removingTXEntered0.await();
Thread t = new Thread() {
@Override
public void run() {
try {
xaSession.getXAResource().rollback(xid);
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
removingTXEntered1.await();
storingThread.start();
latchStore.await();
removingTXAwait1.countDown();
Thread.sleep(1000);
removingTXAwait0.countDown();
Assert.assertTrue(enteredRollbackLatch.await(10, TimeUnit.SECONDS));
waitingRollbackLatch.countDown();
t.join();
consumer.close();
consumer = session.createConsumer(queue);
for (int i = 0; i < 10; i++) {
Assert.assertNotNull(consumer.receive(5000));
}
Assert.assertNull(consumer.receiveNoWait());
connection.close();
connction2.close();
}
use of javax.jms.XASession in project activemq-artemis by apache.
the class SimpleOpenWireTest method testXAPrepare.
@Test
public void testXAPrepare() throws Exception {
try {
XAConnection connection = xaFactory.createXAConnection();
XASession xasession = connection.createXASession();
Xid xid = newXID();
xasession.getXAResource().start(xid, XAResource.TMNOFLAGS);
Queue queue = xasession.createQueue(queueName);
MessageProducer producer = xasession.createProducer(queue);
producer.send(xasession.createTextMessage("hello"));
producer.send(xasession.createTextMessage("hello"));
xasession.getXAResource().end(xid, XAResource.TMSUCCESS);
xasession.getXAResource().prepare(xid);
connection.close();
System.err.println("Done!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations