Search in sources :

Example 51 with XAConnection

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

the class SimpleOpenWireTest method testXAPrepare.

@Test
public void testXAPrepare() throws Exception {
    try {
        XAConnection connection = xaFactory.createXAConnection();
        XASession xasession = connection.createXASession();
        Xid xid = newXID();
        xasession.getXAResource().start(xid, XAResource.TMNOFLAGS);
        Queue queue = xasession.createQueue(queueName);
        MessageProducer producer = xasession.createProducer(queue);
        producer.send(xasession.createTextMessage("hello"));
        producer.send(xasession.createTextMessage("hello"));
        xasession.getXAResource().end(xid, XAResource.TMSUCCESS);
        xasession.getXAResource().prepare(xid);
        connection.close();
        System.err.println("Done!!!");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Xid(javax.transaction.xa.Xid) XASession(javax.jms.XASession) MessageProducer(javax.jms.MessageProducer) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TimeoutException(java.util.concurrent.TimeoutException) JMSException(javax.jms.JMSException) XAException(javax.transaction.xa.XAException) InvalidDestinationException(javax.jms.InvalidDestinationException) XAConnection(javax.jms.XAConnection) Test(org.junit.Test)

Example 52 with XAConnection

use of javax.jms.XAConnection 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 53 with XAConnection

use of javax.jms.XAConnection 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 54 with XAConnection

use of javax.jms.XAConnection 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)

Example 55 with XAConnection

use of javax.jms.XAConnection in project narayana by jbosstm.

the class ProtocolConverter method getXASession.

protected StompSession getXASession(Xid xid) throws JMSException {
    StompSession xaSession = xaSessions.get(xid);
    if (xaSession == null) {
        XAConnection xaConnection;
        if (login != null) {
            xaConnection = xaConnectionFactory.createXAConnection(login, passcode);
        } else {
            xaConnection = xaConnectionFactory.createXAConnection();
        }
        if (clientId != null) {
            xaConnection.setClientID(clientId);
        }
        xaConnection.start();
        Session session = xaConnection.createXASession();
        if (log.isDebugEnabled()) {
            log.debug("Created XA session");
        }
        xaSession = new StompSession(initialContext, this, session, xaConnection);
        log.trace("Created XA Session");
        xaSessions.put(xid, xaSession);
    } else {
        log.trace("Returned existing XA session");
    }
    return xaSession;
}
Also used : XAConnection(javax.jms.XAConnection) Session(javax.jms.Session)

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