Search in sources :

Example 56 with XASession

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

the class SimpleOpenWireTest method testXAConsumer.

@Test
public void testXAConsumer() throws Exception {
    Queue queue;
    try (Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE)) {
        queue = session.createQueue(queueName);
        System.out.println("Queue:" + queue);
        MessageProducer producer = session.createProducer(queue);
        for (int i = 0; i < 10; i++) {
            TextMessage msg = session.createTextMessage("test" + i);
            msg.setStringProperty("myobj", "test" + i);
            producer.send(msg);
        }
        session.close();
    }
    try (XAConnection xaconnection = xaFactory.createXAConnection()) {
        Xid xid = newXID();
        XASession session = xaconnection.createXASession();
        session.getXAResource().start(xid, XAResource.TMNOFLAGS);
        MessageConsumer consumer = session.createConsumer(queue);
        xaconnection.start();
        for (int i = 0; i < 5; i++) {
            TextMessage message = (TextMessage) consumer.receive(5000);
            Assert.assertNotNull(message);
            Assert.assertEquals("test" + i, message.getText());
        }
        session.getXAResource().end(xid, XAResource.TMSUCCESS);
        session.getXAResource().rollback(xid);
        consumer.close();
        xaconnection.close();
    }
    try (Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
        connection.start();
        MessageConsumer consumer = session.createConsumer(queue);
        for (int i = 0; i < 10; i++) {
            TextMessage message = (TextMessage) consumer.receive(5000);
            Assert.assertNotNull(message);
            // Assert.assertEquals("test" + i, message.getText());
            System.out.println("Message " + message.getText());
        }
        checkDuplicate(consumer);
        System.out.println("Queue:" + queue);
        session.close();
    }
    System.err.println("Done!!!");
}
Also used : Xid(javax.transaction.xa.Xid) MessageConsumer(javax.jms.MessageConsumer) XASession(javax.jms.XASession) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) XASession(javax.jms.XASession) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.ActiveMQSession) TopicSession(javax.jms.TopicSession) QueueSession(javax.jms.QueueSession) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 57 with XASession

use of javax.jms.XASession 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);
}
Also used : Xid(javax.transaction.xa.Xid) Transaction(org.apache.activemq.artemis.core.transaction.Transaction) XASession(javax.jms.XASession) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 58 with XASession

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

the class ConnectionTest method testXAInstanceof.

@Test
public void testXAInstanceof() throws Exception {
    conn = cf.createConnection();
    assertFalse(conn instanceof XAConnection);
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    assertFalse(sess instanceof XASession);
}
Also used : XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) QueueSession(javax.jms.QueueSession) XASession(javax.jms.XASession) Session(javax.jms.Session) TopicSession(javax.jms.TopicSession) Test(org.junit.Test)

Aggregations

XASession (javax.jms.XASession)58 XAConnection (javax.jms.XAConnection)51 MessageProducer (javax.jms.MessageProducer)41 Test (org.junit.Test)41 MessageConsumer (javax.jms.MessageConsumer)36 Session (javax.jms.Session)35 TextMessage (javax.jms.TextMessage)35 XAResource (javax.transaction.xa.XAResource)34 Connection (javax.jms.Connection)26 Transaction (javax.transaction.Transaction)24 Message (javax.jms.Message)19 Xid (javax.transaction.xa.Xid)19 Queue (javax.jms.Queue)14 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)12 XAException (javax.transaction.xa.XAException)11 TemporaryQueue (javax.jms.TemporaryQueue)8 JMSException (javax.jms.JMSException)7 XidImpl (org.apache.activemq.artemis.core.transaction.impl.XidImpl)6 QueueSession (javax.jms.QueueSession)5 TopicSession (javax.jms.TopicSession)5