Search in sources :

Example 1 with XAConnection

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

the class ActiveMQXAConnectionFactoryTest method testExceptionAfterClose.

public void testExceptionAfterClose() throws Exception {
    ActiveMQXAConnectionFactory cf1 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
    XAConnection connection1 = (XAConnection) cf1.createConnection();
    connection1.start();
    XASession session = connection1.createXASession();
    session.close();
    try {
        session.commit();
        fail("expect exception after close");
    } catch (javax.jms.IllegalStateException expected) {
    }
    try {
        session.rollback();
        fail("expect exception after close");
    } catch (javax.jms.IllegalStateException expected) {
    }
    try {
        session.getTransacted();
        fail("expect exception after close");
    } catch (javax.jms.IllegalStateException expected) {
    }
}
Also used : XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection)

Example 2 with XAConnection

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

the class ActiveMQXAConnectionFactoryTest method testIsSameRMOverride.

public void testIsSameRMOverride() throws URISyntaxException, JMSException, XAException {
    XAConnection connection1 = null;
    XAConnection connection2 = null;
    try {
        ActiveMQXAConnectionFactory cf1 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false&jms.rmIdFromConnectionId=true");
        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();
        assertFalse(resource1.isSameRM(resource2));
        // ensure identity is preserved
        XASession session1a = connection1.createXASession();
        assertTrue(resource1.isSameRM(session1a.getXAResource()));
        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 3 with XAConnection

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

the class ActiveMQXAConnectionFactoryTest method testCloseSendConnection.

public void testCloseSendConnection() throws Exception {
    String brokerName = "closeSend";
    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();
    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);
    connection.close();
    // comment out this check as it doesn't apply to artemis
    // assertTransactionGoneFromBroker(tid);
    broker.stop();
}
Also used : Destination(javax.jms.Destination) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XASession(javax.jms.XASession) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) BrokerService(org.apache.activemq.broker.BrokerService) URI(java.net.URI) XAConnection(javax.jms.XAConnection) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage)

Example 4 with XAConnection

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

the class ActiveMQXAConnectionFactoryTest method testSessionCloseTransactionalSendReceive.

public void testSessionCloseTransactionalSendReceive() 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);
    session.close();
    resource.end(tid, XAResource.TMSUCCESS);
    resource.commit(tid, true);
    session = connection1.createXASession();
    MessageConsumer consumer = session.createConsumer(dest);
    tid = createXid();
    resource = session.getXAResource();
    resource.start(tid, XAResource.TMNOFLAGS);
    TextMessage receivedMessage = (TextMessage) consumer.receive(1000);
    session.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 5 with XAConnection

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

the class ActiveMQXAConnectionFactoryTest method testVanilaTransactionalProduceReceive.

public void testVanilaTransactionalProduceReceive() throws Exception {
    XAConnection connection1 = null;
    try {
        ActiveMQXAConnectionFactory cf1 = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
        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);
        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);
        assertNotNull(receivedMessage);
        assertEquals(getName(), receivedMessage.getText());
        resource.end(tid, XAResource.TMSUCCESS);
        resource.commit(tid, true);
        session.close();
    } finally {
        if (connection1 != null) {
            try {
                connection1.close();
            } catch (Exception e) {
            // ignore
            }
        }
    }
}
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) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JMSException(javax.jms.JMSException) XAException(javax.transaction.xa.XAException) XAConnection(javax.jms.XAConnection) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage)

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