Search in sources :

Example 26 with XAConnection

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

the class XATest method test2PCReceiveRollback1PCOptimization.

@Test
public void test2PCReceiveRollback1PCOptimization() throws Exception {
    // Since both resources have some RM, TM will probably use 1PC optimization
    XAConnection conn = null;
    Connection conn2 = null;
    try {
        conn2 = cf.createConnection();
        Session sessProducer = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer prod = sessProducer.createProducer(queue1);
        Message m = sessProducer.createTextMessage("XATest1");
        prod.send(m);
        m = sessProducer.createTextMessage("XATest2");
        prod.send(m);
        conn = xacf.createXAConnection();
        conn.start();
        tm.begin();
        XASession sess = conn.createXASession();
        XAResource res = sess.getXAResource();
        XAResource res2 = new DummyXAResource();
        Transaction tx = tm.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(res2);
        MessageConsumer cons = sess.createConsumer(queue1);
        TextMessage m2 = (TextMessage) cons.receive(5000);
        Assert.assertNotNull(m2);
        Assert.assertEquals("XATest1", m2.getText());
        m2 = (TextMessage) cons.receive(5000);
        Assert.assertNotNull(m2);
        Assert.assertEquals("XATest2", m2.getText());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(res2, XAResource.TMSUCCESS);
        tm.rollback();
        // Message should be redelivered
        // New tx
        tm.begin();
        tx = tm.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(res2);
        TextMessage m3 = (TextMessage) cons.receive(5000);
        Assert.assertNotNull(m3);
        Assert.assertEquals("XATest1", m3.getText());
        m3 = (TextMessage) cons.receive(5000);
        Assert.assertNotNull(m3);
        Assert.assertEquals("XATest2", m3.getText());
        Assert.assertTrue(m3.getJMSRedelivered());
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(res2, XAResource.TMSUCCESS);
        tm.commit();
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (conn2 != null) {
            conn2.close();
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) Transaction(javax.transaction.Transaction) XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session) Test(org.junit.Test)

Example 27 with XAConnection

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

the class ConnectionFactoryTest method testConnectionTypes.

@Test
public void testConnectionTypes() throws Exception {
    deployConnectionFactory(0, JMSFactoryType.CF, "ConnectionFactory", "/ConnectionFactory");
    deployConnectionFactory(0, JMSFactoryType.QUEUE_XA_CF, "CF_QUEUE_XA_TRUE", "/CF_QUEUE_XA_TRUE");
    deployConnectionFactory(0, JMSFactoryType.XA_CF, "CF_XA_TRUE", "/CF_XA_TRUE");
    deployConnectionFactory(0, JMSFactoryType.QUEUE_CF, "CF_QUEUE", "/CF_QUEUE");
    deployConnectionFactory(0, JMSFactoryType.TOPIC_CF, "CF_TOPIC", "/CF_TOPIC");
    deployConnectionFactory(0, JMSFactoryType.TOPIC_XA_CF, "CF_TOPIC_XA_TRUE", "/CF_TOPIC_XA_TRUE");
    Connection genericConnection = null;
    XAConnection xaConnection = null;
    QueueConnection queueConnection = null;
    TopicConnection topicConnection = null;
    XAQueueConnection xaQueueConnection = null;
    XATopicConnection xaTopicConnection = null;
    ConnectionFactory genericFactory = (ConnectionFactory) ic.lookup("/ConnectionFactory");
    genericConnection = genericFactory.createConnection();
    assertConnectionType(genericConnection, "generic");
    XAConnectionFactory xaFactory = (XAConnectionFactory) ic.lookup("/CF_XA_TRUE");
    xaConnection = xaFactory.createXAConnection();
    assertConnectionType(xaConnection, "xa");
    QueueConnectionFactory queueCF = (QueueConnectionFactory) ic.lookup("/CF_QUEUE");
    queueConnection = queueCF.createQueueConnection();
    assertConnectionType(queueConnection, "queue");
    TopicConnectionFactory topicCF = (TopicConnectionFactory) ic.lookup("/CF_TOPIC");
    topicConnection = topicCF.createTopicConnection();
    assertConnectionType(topicConnection, "topic");
    XAQueueConnectionFactory xaQueueCF = (XAQueueConnectionFactory) ic.lookup("/CF_QUEUE_XA_TRUE");
    xaQueueConnection = xaQueueCF.createXAQueueConnection();
    assertConnectionType(xaQueueConnection, "xa-queue");
    XATopicConnectionFactory xaTopicCF = (XATopicConnectionFactory) ic.lookup("/CF_TOPIC_XA_TRUE");
    xaTopicConnection = xaTopicCF.createXATopicConnection();
    assertConnectionType(xaTopicConnection, "xa-topic");
    genericConnection.close();
    xaConnection.close();
    queueConnection.close();
    topicConnection.close();
    xaQueueConnection.close();
    xaTopicConnection.close();
    undeployConnectionFactory("ConnectionFactory");
    undeployConnectionFactory("CF_QUEUE_XA_TRUE");
    undeployConnectionFactory("CF_XA_TRUE");
    undeployConnectionFactory("CF_QUEUE");
    undeployConnectionFactory("CF_TOPIC");
    undeployConnectionFactory("CF_TOPIC_XA_TRUE");
}
Also used : XATopicConnectionFactory(javax.jms.XATopicConnectionFactory) TopicConnectionFactory(javax.jms.TopicConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) XAQueueConnectionFactory(javax.jms.XAQueueConnectionFactory) XAConnectionFactory(javax.jms.XAConnectionFactory) QueueConnectionFactory(javax.jms.QueueConnectionFactory) XATopicConnectionFactory(javax.jms.XATopicConnectionFactory) TopicConnectionFactory(javax.jms.TopicConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) XAQueueConnection(javax.jms.XAQueueConnection) QueueConnection(javax.jms.QueueConnection) XAQueueConnectionFactory(javax.jms.XAQueueConnectionFactory) QueueConnectionFactory(javax.jms.QueueConnectionFactory) XAQueueConnection(javax.jms.XAQueueConnection) XAConnection(javax.jms.XAConnection) XATopicConnection(javax.jms.XATopicConnection) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) QueueConnection(javax.jms.QueueConnection) XAConnectionFactory(javax.jms.XAConnectionFactory) XATopicConnection(javax.jms.XATopicConnection) XAQueueConnectionFactory(javax.jms.XAQueueConnectionFactory) XATopicConnectionFactory(javax.jms.XATopicConnectionFactory) XAQueueConnection(javax.jms.XAQueueConnection) XATopicConnection(javax.jms.XATopicConnection) TopicConnection(javax.jms.TopicConnection) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 28 with XAConnection

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

the class SessionTest method testGetXAResource2.

@Test
public void testGetXAResource2() throws Exception {
    XAConnection conn = getXAConnectionFactory().createXAConnection();
    XASession sess = conn.createXASession();
    sess.getXAResource();
    conn.close();
}
Also used : XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 29 with XAConnection

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

the class OutgoingConnectionTest method testInexistentUserOnCreateConnection.

@Test
public void testInexistentUserOnCreateConnection() throws Exception {
    resourceAdapter = newResourceAdapter();
    MyBootstrapContext ctx = new MyBootstrapContext();
    resourceAdapter.start(ctx);
    ActiveMQRAManagedConnectionFactory mcf = new ActiveMQRAManagedConnectionFactory();
    mcf.setResourceAdapter(resourceAdapter);
    ActiveMQRAConnectionFactory qraConnectionFactory = new ActiveMQRAConnectionFactoryImpl(mcf, qraConnectionManager);
    Connection conn = null;
    try {
        conn = qraConnectionFactory.createConnection("IDont", "Exist");
        fail("Exception was expected");
    } catch (JMSSecurityException expected) {
    }
    conn = qraConnectionFactory.createConnection("testuser", "testpassword");
    conn.close();
    try {
        XAConnection xaconn = qraConnectionFactory.createXAConnection("IDont", "Exist");
        fail("Exception was expected");
    } catch (JMSSecurityException expected) {
    }
    XAConnection xaconn = qraConnectionFactory.createXAConnection("testuser", "testpassword");
    xaconn.close();
    try {
        TopicConnection topicconn = qraConnectionFactory.createTopicConnection("IDont", "Exist");
        fail("Exception was expected");
    } catch (JMSSecurityException expected) {
    }
    TopicConnection topicconn = qraConnectionFactory.createTopicConnection("testuser", "testpassword");
    topicconn.close();
    try {
        QueueConnection queueconn = qraConnectionFactory.createQueueConnection("IDont", "Exist");
        fail("Exception was expected");
    } catch (JMSSecurityException expected) {
    }
    QueueConnection queueconn = qraConnectionFactory.createQueueConnection("testuser", "testpassword");
    queueconn.close();
    mcf.stop();
}
Also used : ActiveMQRAManagedConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory) XAQueueConnection(javax.jms.XAQueueConnection) QueueConnection(javax.jms.QueueConnection) JMSSecurityException(javax.jms.JMSSecurityException) XAQueueConnection(javax.jms.XAQueueConnection) XAConnection(javax.jms.XAConnection) ManagedConnection(javax.resource.spi.ManagedConnection) Connection(javax.jms.Connection) TopicConnection(javax.jms.TopicConnection) ActiveMQRAManagedConnection(org.apache.activemq.artemis.ra.ActiveMQRAManagedConnection) QueueConnection(javax.jms.QueueConnection) ActiveMQRAConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory) TopicConnection(javax.jms.TopicConnection) ActiveMQRAConnectionFactoryImpl(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 30 with XAConnection

use of javax.jms.XAConnection in project tomee by apache.

the class TomEEManagedConnectionProxy method createXASession.

@Override
public XASession createXASession() throws JMSException {
    XASession session = ((XAConnection) connection.getPhysicalConnection()).createXASession();
    enlistInTransactionIfNeeded(session);
    return session;
}
Also used : XASession(javax.jms.XASession) XAConnection(javax.jms.XAConnection)

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