Search in sources :

Example 46 with XASession

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

the class XATest method testMultipleSessionsOneTxRollbackSend.

@Test
public void testMultipleSessionsOneTxRollbackSend() throws Exception {
    XAConnection conn = null;
    Connection conn2 = null;
    try {
        conn = xacf.createXAConnection();
        conn.start();
        tm.begin();
        // Create 2 sessions and enlist them
        XASession sess1 = conn.createXASession();
        ClientSessionInternal res1 = (ClientSessionInternal) sess1.getXAResource();
        XASession sess2 = conn.createXASession();
        ClientSessionInternal res2 = (ClientSessionInternal) sess2.getXAResource();
        res1.setForceNotSameRM(true);
        res2.setForceNotSameRM(true);
        Transaction tx = tm.getTransaction();
        tx.enlistResource(res1);
        tx.enlistResource(res2);
        // Send 2 messages - one from each session
        MessageProducer prod1 = sess1.createProducer(queue1);
        MessageProducer prod2 = sess2.createProducer(queue1);
        prod1.send(sess1.createTextMessage("echidna1"));
        prod2.send(sess2.createTextMessage("echidna2"));
        tx.delistResource(res1, XAResource.TMSUCCESS);
        tx.delistResource(res2, XAResource.TMSUCCESS);
        // rollback
        tm.rollback();
        // Messages should not be in queue
        conn2 = cf.createConnection();
        Session sess = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer cons = sess.createConsumer(queue1);
        conn2.start();
        TextMessage r1 = (TextMessage) cons.receive(100);
        Assert.assertNull(r1);
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (conn2 != null) {
            conn2.close();
        }
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) MessageConsumer(javax.jms.MessageConsumer) 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 47 with XASession

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

the class XATest method test2PCSendCommit1PCOptimization.

// Public --------------------------------------------------------
@Test
public void test2PCSendCommit1PCOptimization() throws Exception {
    // Since both resources have same RM, TM will probably use 1PC optimization
    XAConnection conn = null;
    Connection conn2 = null;
    try {
        conn = xacf.createXAConnection();
        tm.begin();
        XASession sess = conn.createXASession();
        XAResource res = sess.getXAResource();
        XAResource res2 = new DummyXAResource();
        Transaction tx = tm.getTransaction();
        tx.enlistResource(res);
        tx.enlistResource(res2);
        MessageProducer prod = sess.createProducer(queue1);
        prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        Message m = sess.createTextMessage("XATest1");
        prod.send(m);
        m = sess.createTextMessage("XATest2");
        prod.send(m);
        tx.delistResource(res, XAResource.TMSUCCESS);
        tx.delistResource(res2, XAResource.TMSUCCESS);
        tm.commit();
        conn2 = cf.createConnection();
        conn2.start();
        Session sessReceiver = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer cons = sessReceiver.createConsumer(queue1);
        TextMessage m2 = (TextMessage) cons.receive(1000);
        Assert.assertNotNull(m2);
        Assert.assertEquals("XATest1", m2.getText());
        m2 = (TextMessage) cons.receive(1000);
        Assert.assertNotNull(m2);
        Assert.assertEquals("XATest2", m2.getText());
    } finally {
        if (conn != null) {
            conn.close();
        }
        if (conn2 != null) {
            conn2.close();
        }
    }
}
Also used : XAResource(javax.transaction.xa.XAResource) MessageConsumer(javax.jms.MessageConsumer) Transaction(javax.transaction.Transaction) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) 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 48 with XASession

use of javax.jms.XASession 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 49 with XASession

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

the class OutgoingConnectionTest method testSimpleMessageSendAndReceiveXA.

@Test
public void testSimpleMessageSendAndReceiveXA() throws Exception {
    Xid xid = new XidImpl("xa1".getBytes(), 1, UUIDGenerator.getInstance().generateStringUUID().getBytes());
    XAQueueConnection queueConnection = qraConnectionFactory.createXAQueueConnection();
    XASession s = queueConnection.createXASession();
    XAResource resource = s.getXAResource();
    resource.start(xid, XAResource.TMNOFLAGS);
    Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
    MessageProducer mp = s.createProducer(q);
    MessageConsumer consumer = s.createConsumer(q);
    Message message = s.createTextMessage("test");
    mp.send(message);
    queueConnection.start();
    TextMessage textMessage = (TextMessage) consumer.receiveNoWait();
    assertNull(textMessage);
    resource.end(xid, XAResource.TMSUCCESS);
    resource.commit(xid, true);
    resource.start(xid, XAResource.TMNOFLAGS);
    textMessage = (TextMessage) consumer.receiveNoWait();
    resource.end(xid, XAResource.TMSUCCESS);
    resource.commit(xid, true);
    assertNotNull(textMessage);
    assertEquals(textMessage.getText(), "test");
    // When I wrote this call, this method was doing an infinite loop.
    // this is just to avoid such thing again
    textMessage.getJMSDeliveryTime();
}
Also used : Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) XASession(javax.jms.XASession) MessageProducer(javax.jms.MessageProducer) XAQueueConnection(javax.jms.XAQueueConnection) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 50 with XASession

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

the class OutgoingConnectionTest method testOutgoingXAResourceWrapper.

@Test
public void testOutgoingXAResourceWrapper() throws Exception {
    XAQueueConnection queueConnection = qraConnectionFactory.createXAQueueConnection();
    XASession s = queueConnection.createXASession();
    XAResource resource = s.getXAResource();
    assertTrue(resource instanceof ActiveMQXAResourceWrapper);
    ActiveMQXAResourceWrapperImpl xaResourceWrapper = (ActiveMQXAResourceWrapperImpl) resource;
    assertTrue(xaResourceWrapper.getJndiName().equals("java://jmsXA NodeId:" + server.getNodeID()));
    assertTrue(xaResourceWrapper.getProductVersion().equals(VersionLoader.getVersion().getFullVersion()));
    assertTrue(xaResourceWrapper.getProductName().equals(ActiveMQResourceAdapter.PRODUCT_NAME));
}
Also used : XAResource(javax.transaction.xa.XAResource) ActiveMQXAResourceWrapper(org.apache.activemq.artemis.service.extensions.xa.ActiveMQXAResourceWrapper) XASession(javax.jms.XASession) XAQueueConnection(javax.jms.XAQueueConnection) ActiveMQXAResourceWrapperImpl(org.apache.activemq.artemis.service.extensions.xa.ActiveMQXAResourceWrapperImpl) 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