Search in sources :

Example 31 with XAConnection

use of javax.jms.XAConnection in project brave by openzipkin.

the class JmsTracingTest method xaConnection_doesntDoubleWrap.

@Test
public void xaConnection_doesntDoubleWrap() {
    XAConnection wrapped = jmsTracing.xaConnection(mock(XAConnection.class));
    assertThat(jmsTracing.xaConnection(wrapped)).isSameAs(wrapped);
}
Also used : XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 32 with XAConnection

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

the class ActiveMQXAConnectionFactoryTest method testIsSameRM.

public void testIsSameRM() throws URISyntaxException, JMSException, XAException {
    XAConnection connection1 = null;
    XAConnection connection2 = null;
    try {
        ActiveMQXAConnectionFactory cf1 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
        connection1 = (XAConnection) cf1.createConnection();
        XASession session1 = connection1.createXASession();
        XAResource resource1 = session1.getXAResource();
        ActiveMQXAConnectionFactory cf2 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
        connection2 = (XAConnection) cf2.createConnection();
        XASession session2 = connection2.createXASession();
        XAResource resource2 = session2.getXAResource();
        assertTrue(resource1.isSameRM(resource2));
        session1.close();
        session2.close();
    } finally {
        if (connection1 != null) {
            try {
                connection1.close();
            } catch (Exception e) {
            // ignore
            }
        }
        if (connection2 != null) {
            try {
                connection2.close();
            } catch (Exception e) {
            // ignore
            }
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) XASession(javax.jms.XASession) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JMSException(javax.jms.JMSException) XAException(javax.transaction.xa.XAException) XAConnection(javax.jms.XAConnection)

Example 33 with XAConnection

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

the class ActiveMQXAConnectionFactoryTest method testRollbackXaErrorCode.

public void testRollbackXaErrorCode() throws Exception {
    String brokerName = "rollbackErrorCode";
    BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:0)/" + brokerName));
    broker.start();
    broker.waitUntilStarted();
    ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory(broker.getTransportConnectors().get(0).getConnectUri());
    XAConnection connection = (XAConnection) cf.createConnection();
    connection.start();
    XASession session = connection.createXASession();
    XAResource resource = session.getXAResource();
    Xid tid = createXid();
    try {
        resource.rollback(tid);
        fail("Expected xa exception on no tx");
    } catch (XAException expected) {
        LOG.info("got expected xa", expected);
        assertEquals("no tx", XAException.XAER_NOTA, expected.errorCode);
    }
    connection.close();
    broker.stop();
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) XASession(javax.jms.XASession) BrokerService(org.apache.activemq.broker.BrokerService) URI(java.net.URI) XAConnection(javax.jms.XAConnection)

Example 34 with XAConnection

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

the class ActiveMQXAConnectionFactoryTest method testConsumerCloseTransactionalSendReceive.

public void testConsumerCloseTransactionalSendReceive() throws Exception {
    ActiveMQXAConnectionFactory cf1 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
    XAConnection connection1 = (XAConnection) cf1.createConnection();
    connection1.start();
    XASession session = connection1.createXASession();
    XAResource resource = session.getXAResource();
    Destination dest = new ActiveMQQueue(getName());
    // publish a message
    Xid tid = createXid();
    resource.start(tid, XAResource.TMNOFLAGS);
    MessageProducer producer = session.createProducer(dest);
    ActiveMQTextMessage message = new ActiveMQTextMessage();
    message.setText(getName());
    producer.send(message);
    producer.close();
    resource.end(tid, XAResource.TMSUCCESS);
    resource.commit(tid, true);
    session.close();
    session = connection1.createXASession();
    MessageConsumer consumer = session.createConsumer(dest);
    tid = createXid();
    resource = session.getXAResource();
    resource.start(tid, XAResource.TMNOFLAGS);
    TextMessage receivedMessage = (TextMessage) consumer.receive(1000);
    consumer.close();
    assertNotNull(receivedMessage);
    assertEquals(getName(), receivedMessage.getText());
    resource.end(tid, XAResource.TMSUCCESS);
    resource.commit(tid, true);
    session = connection1.createXASession();
    consumer = session.createConsumer(dest);
    tid = createXid();
    resource = session.getXAResource();
    resource.start(tid, XAResource.TMNOFLAGS);
    assertNull(consumer.receive(1000));
    resource.end(tid, XAResource.TMSUCCESS);
    resource.commit(tid, true);
}
Also used : Destination(javax.jms.Destination) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) MessageConsumer(javax.jms.MessageConsumer) XASession(javax.jms.XASession) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage)

Example 35 with XAConnection

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

the class JMSXDeliveryCountTest method testDeliveryCountUpdatedOnCloseXA.

@Test
public void testDeliveryCountUpdatedOnCloseXA() throws Exception {
    XAConnection xaConn = null;
    Connection conn = null;
    TransactionManager mgr = new TransactionManagerImple();
    Transaction toResume = null;
    Transaction tx = null;
    try {
        toResume = mgr.suspend();
        conn = cf.createConnection();
        // Send a message
        Session producerSess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer producer = producerSess.createProducer(queue1);
        TextMessage tm = producerSess.createTextMessage("message1");
        producer.send(tm);
        xaConn = xacf.createXAConnection();
        XASession consumerSess = xaConn.createXASession();
        MessageConsumer consumer = consumerSess.createConsumer(queue1);
        xaConn.start();
        DummyXAResource res = new DummyXAResource();
        mgr.begin();
        tx = mgr.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(consumerSess.getXAResource());
        TextMessage rm = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(rm);
        Assert.assertEquals(tm.getText(), rm.getText());
        Assert.assertEquals(1, rm.getIntProperty("JMSXDeliveryCount"));
        Assert.assertFalse(rm.getJMSRedelivered());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
        mgr.rollback();
        mgr.begin();
        tx = mgr.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(consumerSess.getXAResource());
        rm = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(rm);
        Assert.assertEquals(tm.getText(), rm.getText());
        Assert.assertEquals(2, rm.getIntProperty("JMSXDeliveryCount"));
        Assert.assertTrue(rm.getJMSRedelivered());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
        mgr.rollback();
        mgr.begin();
        tx = mgr.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(consumerSess.getXAResource());
        rm = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(rm);
        Assert.assertEquals(tm.getText(), rm.getText());
        Assert.assertEquals(3, rm.getIntProperty("JMSXDeliveryCount"));
        Assert.assertTrue(rm.getJMSRedelivered());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
        mgr.rollback();
        // Must close consumer first
        consumer.close();
        consumerSess.close();
        consumerSess = xaConn.createXASession();
        consumer = consumerSess.createConsumer(queue1);
        mgr.begin();
        tx = mgr.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(consumerSess.getXAResource());
        rm = (TextMessage) consumer.receive(1000);
        Assert.assertNotNull(rm);
        Assert.assertEquals(tm.getText(), rm.getText());
        Assert.assertEquals(4, rm.getIntProperty("JMSXDeliveryCount"));
        Assert.assertTrue(rm.getJMSRedelivered());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(consumerSess.getXAResource(), XAResource.TMSUCCESS);
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (tx != null) {
            try {
                mgr.commit();
            } catch (Exception ignore) {
            }
        }
        if (xaConn != null) {
            xaConn.close();
        }
        if (toResume != null) {
            try {
                mgr.resume(toResume);
            } catch (Exception ignore) {
            }
        }
    }
}
Also used : TransactionManagerImple(com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple) MessageConsumer(javax.jms.MessageConsumer) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XAException(javax.transaction.xa.XAException) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

XAConnection (javax.jms.XAConnection)58 XASession (javax.jms.XASession)53 Test (org.junit.Test)42 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