Search in sources :

Example 6 with XASession

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

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

the class ActiveMQXAConnectionFactoryTest method testReadonlyNoLeak.

public void testReadonlyNoLeak() throws Exception {
    final String brokerName = "readOnlyNoLeak";
    BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:0)/" + brokerName));
    broker.setPersistent(false);
    broker.start();
    ActiveMQXAConnectionFactory cf1 = new ActiveMQXAConnectionFactory("failover:(" + broker.getTransportConnectors().get(0).getConnectUri() + ")");
    cf1.setStatsEnabled(true);
    ActiveMQXAConnection xaConnection = (ActiveMQXAConnection) cf1.createConnection();
    xaConnection.start();
    XASession session = xaConnection.createXASession();
    XAResource resource = session.getXAResource();
    Xid tid = createXid();
    resource.start(tid, XAResource.TMNOFLAGS);
    session.close();
    resource.end(tid, XAResource.TMSUCCESS);
    resource.commit(tid, true);
    // not apply to artemis
    // assertTransactionGoneFromBroker(tid);
    // assertTransactionGoneFromConnection(brokerName, xaConnection.getClientID(), xaConnection.getConnectionInfo().getConnectionId(), tid);
    assertSessionGone(xaConnection, session);
    assertTransactionGoneFromFailoverState(xaConnection, tid);
    // two phase
    session = xaConnection.createXASession();
    resource = session.getXAResource();
    tid = createXid();
    resource.start(tid, XAResource.TMNOFLAGS);
    session.close();
    resource.end(tid, XAResource.TMSUCCESS);
    assertEquals(XAResource.XA_RDONLY, resource.prepare(tid));
    // no need for a commit on read only
    // assertTransactionGoneFromBroker(tid);
    // assertTransactionGoneFromConnection(brokerName, xaConnection.getClientID(), xaConnection.getConnectionInfo().getConnectionId(), tid);
    assertSessionGone(xaConnection, session);
    assertTransactionGoneFromFailoverState(xaConnection, tid);
    xaConnection.close();
    broker.stop();
}
Also used : XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XASession(javax.jms.XASession) BrokerService(org.apache.activemq.broker.BrokerService) URI(java.net.URI)

Example 8 with XASession

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

Example 9 with XASession

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

the class ConcurrentDeliveryCancelTest method testConcurrentCancels.

@Test
@BMRules(rules = { @BMRule(name = "enterCancel-holdThere", targetClass = "org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl", targetMethod = "close", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.ConcurrentDeliveryCancelTest.enterCancel();") })
public void testConcurrentCancels() throws Exception {
    System.out.println(server.getConfiguration().getJournalLocation().toString());
    server.getAddressSettingsRepository().clear();
    AddressSettings settings = new AddressSettings();
    settings.setMaxDeliveryAttempts(-1);
    server.getAddressSettingsRepository().addMatch("#", settings);
    ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616", "test");
    cf.setReconnectAttempts(0);
    cf.setRetryInterval(10);
    System.out.println(".....");
    for (ServerSession srvSess : server.getSessions()) {
        System.out.println(srvSess);
    }
    String queueName = RandomUtil.randomString();
    Queue queue = createQueue(queueName);
    int numberOfMessages = 10000;
    {
        Connection connection = cf.createConnection();
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        MessageProducer producer = session.createProducer(queue);
        for (int i = 0; i < numberOfMessages; i++) {
            TextMessage msg = session.createTextMessage("message " + i);
            msg.setIntProperty("i", i);
            producer.send(msg);
        }
        session.commit();
        connection.close();
    }
    for (int i = 0; i < 100; i++) {
        XAConnectionFactory xacf = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616", "test");
        final XAConnection connection = xacf.createXAConnection();
        final XASession theSession = connection.createXASession();
        ((ActiveMQSession) theSession).getCoreSession().addMetaData("theSession", "true");
        connection.start();
        final MessageConsumer consumer = theSession.createConsumer(queue);
        XidImpl xid = newXID();
        theSession.getXAResource().start(xid, XAResource.TMNOFLAGS);
        // I'm setting a small timeout just because I'm lazy to call end myself
        theSession.getXAResource().setTransactionTimeout(1);
        for (int msg = 0; msg < 11; msg++) {
            Assert.assertNotNull(consumer.receiveNoWait());
        }
        System.out.println(".....");
        final List<ServerSession> serverSessions = new LinkedList<>();
        // We will force now the failure simultaneously from several places
        for (ServerSession srvSess : server.getSessions()) {
            if (srvSess.getMetaData("theSession") != null) {
                System.out.println(srvSess);
                serverSessions.add(srvSess);
            }
        }
        // from Transactional reaper
        resetLatches(2);
        List<Thread> threads = new LinkedList<>();
        threads.add(new Thread("ConsumerCloser") {

            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName() + " closing consumer");
                    consumer.close();
                    System.out.println(Thread.currentThread().getName() + " closed consumer");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        threads.add(new Thread("SessionCloser") {

            @Override
            public void run() {
                for (ServerSession sess : serverSessions) {
                    System.out.println("Thread " + Thread.currentThread().getName() + " starting");
                    try {
                        // A session.close could sneak in through failover or some other scenarios.
                        // a call to RemotingConnection.fail wasn't replicating the issue.
                        // I needed to call Session.close() directly to replicate what was happening in production
                        sess.close(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread " + Thread.currentThread().getName() + " done");
                }
            }
        });
        for (Thread t : threads) {
            t.start();
        }
        Assert.assertTrue(latchEnter.await(10, TimeUnit.MINUTES));
        latchFlag.countDown();
        for (Thread t : threads) {
            t.join(5000);
            Assert.assertFalse(t.isAlive());
        }
        connection.close();
    }
    Connection connection = cf.createConnection();
    try {
        connection.setClientID("myID");
        // I am too lazy to call end on all the transactions
        Thread.sleep(5000);
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(queue);
        HashMap<Integer, AtomicInteger> mapCount = new HashMap<>();
        while (true) {
            TextMessage message = (TextMessage) consumer.receiveNoWait();
            if (message == null) {
                break;
            }
            Integer value = message.getIntProperty("i");
            AtomicInteger count = mapCount.get(value);
            if (count == null) {
                count = new AtomicInteger(0);
                mapCount.put(message.getIntProperty("i"), count);
            }
            count.incrementAndGet();
        }
        boolean failed = false;
        for (int i = 0; i < numberOfMessages; i++) {
            AtomicInteger count = mapCount.get(i);
            if (count == null) {
                System.out.println("Message " + i + " not received");
                failed = true;
            } else if (count.get() > 1) {
                System.out.println("Message " + i + " received " + count.get() + " times");
                failed = true;
            }
        }
        Assert.assertFalse("test failed, look at the system.out of the test for more information", failed);
    } finally {
        connection.close();
    }
}
Also used : HashMap(java.util.HashMap) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) XASession(javax.jms.XASession) Queue(javax.jms.Queue) XAConnection(javax.jms.XAConnection) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) MessageConsumer(javax.jms.MessageConsumer) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) LinkedList(java.util.LinkedList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XAConnectionFactory(javax.jms.XAConnectionFactory) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XASession(javax.jms.XASession) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 10 with XASession

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

the class FailureXATest method doTestCrashServerAfterXACommit.

private void doTestCrashServerAfterXACommit(boolean onePhase) throws Exception {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    XAConnection connection = connectionFactory.createXAConnection();
    try {
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        Queue queue = session.createQueue("Queue1");
        final XASession xaSession = connection.createXASession();
        MessageConsumer consumer = xaSession.createConsumer(queue);
        MessageProducer producer = session.createProducer(queue);
        producer.send(session.createTextMessage("hello " + 1));
        session.commit();
        XAResource xaResource = xaSession.getXAResource();
        final Xid xid = newXID();
        xaResource.start(xid, XAResource.TMNOFLAGS);
        connection.start();
        Assert.assertNotNull(consumer.receive(5000));
        xaResource.end(xid, XAResource.TMSUCCESS);
        try {
            xaResource.commit(xid, onePhase);
            Assert.fail("didn't get expected exception!");
        } catch (XAException xae) {
            if (onePhase) {
                // expected error code is XAER_RMFAIL
                Assert.assertEquals(XAException.XAER_RMFAIL, xae.errorCode);
            } else {
                // expected error code is XA_RETRY
                Assert.assertEquals(XAException.XA_RETRY, xae.errorCode);
            }
        }
    } finally {
        connection.close();
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) XASession(javax.jms.XASession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) XAConnection(javax.jms.XAConnection) XASession(javax.jms.XASession) Session(javax.jms.Session)

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