Search in sources :

Example 46 with XAConnection

use of javax.jms.XAConnection in project activemq-artemis by apache.

the class JMSTestCase method createXAConnection.

protected final XAConnection createXAConnection() throws JMSException {
    XAConnection c = cf.createXAConnection();
    addConnection(c);
    return c;
}
Also used : XAConnection(javax.jms.XAConnection)

Example 47 with XAConnection

use of javax.jms.XAConnection in project activemq-artemis by apache.

the class SessionTest method testGetSession2.

@Test
public void testGetSession2() throws Exception {
    deployConnectionFactory(0, JMSFactoryType.CF, "ConnectionFactory", "/ConnectionFactory");
    XAConnection conn = getXAConnectionFactory().createXAConnection();
    XASession sess = conn.createXASession();
    sess.getSession();
    conn.close();
}
Also used : XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 48 with XAConnection

use of javax.jms.XAConnection 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();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ArrayList(java.util.ArrayList) InitialContext(javax.naming.InitialContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) XASession(javax.jms.XASession) XAConnectionFactory(javax.jms.XAConnectionFactory) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session)

Example 49 with XAConnection

use of javax.jms.XAConnection 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();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) InitialContext(javax.naming.InitialContext) Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) XASession(javax.jms.XASession) XAConnectionFactory(javax.jms.XAConnectionFactory) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session)

Example 50 with XAConnection

use of javax.jms.XAConnection 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();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) XAConnection(javax.jms.XAConnection) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) Xid(javax.transaction.xa.Xid) XASession(javax.jms.XASession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Aggregations

XAConnection (javax.jms.XAConnection)56 XASession (javax.jms.XASession)52 Test (org.junit.Test)41 MessageProducer (javax.jms.MessageProducer)40 MessageConsumer (javax.jms.MessageConsumer)35 Session (javax.jms.Session)35 TextMessage (javax.jms.TextMessage)34 XAResource (javax.transaction.xa.XAResource)29 Connection (javax.jms.Connection)28 Transaction (javax.transaction.Transaction)24 Message (javax.jms.Message)18 Xid (javax.transaction.xa.Xid)17 Queue (javax.jms.Queue)13 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)12 XAException (javax.transaction.xa.XAException)11 TemporaryQueue (javax.jms.TemporaryQueue)8 Destination (javax.jms.Destination)5 JMSException (javax.jms.JMSException)5 QueueSession (javax.jms.QueueSession)5 TopicSession (javax.jms.TopicSession)5