Search in sources :

Example 51 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer in project activemq-artemis by apache.

the class HeuristicXATest method doHeuristicCompletionWithRestart.

private void doHeuristicCompletionWithRestart(final boolean isCommit) throws Exception {
    Configuration configuration = createDefaultInVMConfig().setJMXManagementEnabled(true);
    ActiveMQServer server = createServer(true, configuration);
    server.setMBeanServer(mbeanServer);
    server.start();
    Xid xid = newXID();
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(true, false, false);
    session.createQueue(ADDRESS, ADDRESS, true);
    session.start(xid, XAResource.TMNOFLAGS);
    ClientProducer producer = session.createProducer(ADDRESS);
    ClientMessage msg = session.createMessage(true);
    msg.getBodyBuffer().writeString(body);
    producer.send(msg);
    session.end(xid, XAResource.TMSUCCESS);
    session.prepare(xid);
    session.close();
    ActiveMQServerControl jmxServer = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
    String[] preparedTransactions = jmxServer.listPreparedTransactions();
    Assert.assertEquals(1, preparedTransactions.length);
    System.out.println(preparedTransactions[0]);
    if (isCommit) {
        jmxServer.commitPreparedTransaction(XidImpl.toBase64String(xid));
    } else {
        jmxServer.rollbackPreparedTransaction(XidImpl.toBase64String(xid));
    }
    preparedTransactions = jmxServer.listPreparedTransactions();
    Assert.assertEquals(0, preparedTransactions.length);
    if (isCommit) {
        Assert.assertEquals(1, getMessageCount(((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable())));
        session = sf.createSession(false, false, false);
        session.start();
        ClientConsumer consumer = session.createConsumer(ADDRESS);
        msg = consumer.receive(1000);
        Assert.assertNotNull(msg);
        msg.acknowledge();
        Assert.assertEquals(body, msg.getBodyBuffer().readString());
        session.commit();
        session.close();
    }
    Assert.assertEquals(0, getMessageCount(((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable())));
    server.stop();
    server.start();
    jmxServer = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
    if (isCommit) {
        String[] listHeuristicCommittedTransactions = jmxServer.listHeuristicCommittedTransactions();
        Assert.assertEquals(1, listHeuristicCommittedTransactions.length);
        System.out.println(listHeuristicCommittedTransactions[0]);
    } else {
        String[] listHeuristicRolledBackTransactions = jmxServer.listHeuristicRolledBackTransactions();
        Assert.assertEquals(1, listHeuristicRolledBackTransactions.length);
        System.out.println(listHeuristicRolledBackTransactions[0]);
    }
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Xid(javax.transaction.xa.Xid) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) Configuration(org.apache.activemq.artemis.core.config.Configuration) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Example 52 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer in project activemq-artemis by apache.

the class HeuristicXATest method doForgetHeuristicCompletedTxAndRestart.

private void doForgetHeuristicCompletedTxAndRestart(final boolean heuristicCommit) throws Exception {
    Configuration configuration = createDefaultInVMConfig().setJMXManagementEnabled(true);
    ActiveMQServer server = createServer(true, configuration);
    server.setMBeanServer(mbeanServer);
    server.start();
    Xid xid = newXID();
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(true, false, false);
    session.createQueue(ADDRESS, ADDRESS, true);
    session.start(xid, XAResource.TMNOFLAGS);
    ClientProducer producer = session.createProducer(ADDRESS);
    ClientMessage msg = session.createMessage(true);
    msg.getBodyBuffer().writeBytes(new byte[123]);
    producer.send(msg);
    session.end(xid, XAResource.TMSUCCESS);
    session.prepare(xid);
    ActiveMQServerControl jmxServer = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
    String[] preparedTransactions = jmxServer.listPreparedTransactions();
    Assert.assertEquals(1, preparedTransactions.length);
    System.out.println(preparedTransactions[0]);
    if (heuristicCommit) {
        jmxServer.commitPreparedTransaction(XidImpl.toBase64String(xid));
    } else {
        jmxServer.rollbackPreparedTransaction(XidImpl.toBase64String(xid));
    }
    preparedTransactions = jmxServer.listPreparedTransactions();
    Assert.assertEquals(0, preparedTransactions.length);
    session.forget(xid);
    session.close();
    if (heuristicCommit) {
        Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
    } else {
        Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
    }
    server.stop();
    server.start();
    // we need to recreate the sf
    sf = createSessionFactory(locator);
    session = sf.createSession(true, false, false);
    Xid[] recoveredXids = session.recover(XAResource.TMSTARTRSCAN);
    Assert.assertEquals(0, recoveredXids.length);
    jmxServer = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
    if (heuristicCommit) {
        Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
    } else {
        Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
    }
    session.close();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Xid(javax.transaction.xa.Xid) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) Configuration(org.apache.activemq.artemis.core.config.Configuration) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Example 53 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer in project activemq-artemis by apache.

the class InterruptedLargeMessageTest method testInterruptLargeMessageSend.

@Test
public void testInterruptLargeMessageSend() throws Exception {
    ClientSession session = null;
    LargeMessageTestInterceptorIgnoreLastPacket.clearInterrupt();
    ActiveMQServer server = createServer(true, isNetty());
    server.getConfiguration().getIncomingInterceptorClassNames().add(LargeMessageTestInterceptorIgnoreLastPacket.class.getName());
    server.start();
    locator.setBlockOnNonDurableSend(false).setBlockOnDurableSend(false);
    ClientSessionFactory sf = createSessionFactory(locator);
    session = sf.createSession(false, true, true);
    session.createQueue(ADDRESS, ADDRESS, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    Message clientFile = createLargeClientMessageStreaming(session, LARGE_MESSAGE_SIZE, true);
    clientFile.setExpiration(System.currentTimeMillis());
    producer.send(clientFile);
    Thread.sleep(500);
    // 
    // for (ServerSession srvSession : server.getSessions()) {
    // ((ServerSessionImpl) srvSession).clearLargeMessage();
    // }
    server.fail(false);
    ActiveMQTestBase.forceGC();
    server.start();
    server.stop();
    validateNoFilesOnLargeDir();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) SessionContinuationMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionContinuationMessage) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 54 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer in project activemq-artemis by apache.

the class InterruptedLargeMessageTest method testCloseConsumerDuringTransmission.

@Test
public void testCloseConsumerDuringTransmission() throws Exception {
    ActiveMQServer server = createServer(true, isNetty());
    LargeMessageTestInterceptorIgnoreLastPacket.disableInterrupt();
    server.start();
    locator.setBlockOnNonDurableSend(false).setBlockOnDurableSend(false).addIncomingInterceptor(new LargeMessageTestInterceptorIgnoreLastPacket());
    ClientSessionFactory sf = createSessionFactory(locator);
    final ClientSession session = sf.createSession(false, true, true);
    session.createQueue(ADDRESS, ADDRESS, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    Message clientFile = createLargeClientMessageStreaming(session, LARGE_MESSAGE_SIZE, true);
    producer.send(clientFile);
    session.commit();
    LargeMessageTestInterceptorIgnoreLastPacket.clearInterrupt();
    final AtomicInteger unexpectedErrors = new AtomicInteger(0);
    final AtomicInteger expectedErrors = new AtomicInteger(0);
    final ClientConsumer cons = session.createConsumer(ADDRESS);
    session.start();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                System.out.println("Receiving message");
                ClientMessage msg = cons.receive(5000);
                if (msg == null) {
                    System.err.println("Message not received");
                    unexpectedErrors.incrementAndGet();
                    return;
                }
                msg.checkCompletion();
            } catch (ActiveMQException e) {
                e.printStackTrace();
                expectedErrors.incrementAndGet();
            }
        }
    };
    t.start();
    LargeMessageTestInterceptorIgnoreLastPacket.awaitInterrupt();
    cons.close();
    t.join();
    Assert.assertEquals(0, unexpectedErrors.get());
    Assert.assertEquals(1, expectedErrors.get());
    session.close();
    server.stop();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) SessionContinuationMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionContinuationMessage) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 55 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer in project activemq-artemis by apache.

the class InterruptedLargeMessageTest method testSendPreparedXA.

@Test
public void testSendPreparedXA() throws Exception {
    ClientSession session = null;
    LargeMessageTestInterceptorIgnoreLastPacket.disableInterrupt();
    ActiveMQServer server = createServer(true, createDefaultConfig(isNetty()), 10000, 20000, new HashMap<String, AddressSettings>());
    server.getConfiguration().getIncomingInterceptorClassNames().add(LargeMessageTestInterceptorIgnoreLastPacket.class.getName());
    server.start();
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true);
    ClientSessionFactory sf = createSessionFactory(locator);
    session = sf.createSession(true, false, false);
    Xid xid1 = newXID();
    Xid xid2 = newXID();
    session.createQueue(ADDRESS, ADDRESS, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    session.start(xid1, XAResource.TMNOFLAGS);
    for (int i = 0; i < 10; i++) {
        Message clientFile = createLargeClientMessageStreaming(session, LARGE_MESSAGE_SIZE, true);
        clientFile.putIntProperty("txid", 1);
        producer.send(clientFile);
    }
    session.end(xid1, XAResource.TMSUCCESS);
    session.prepare(xid1);
    session.start(xid2, XAResource.TMNOFLAGS);
    for (int i = 0; i < 10; i++) {
        Message clientFile = createLargeClientMessageStreaming(session, LARGE_MESSAGE_SIZE, true);
        clientFile.putIntProperty("txid", 2);
        clientFile.putIntProperty("i", i);
        producer.send(clientFile);
    }
    session.end(xid2, XAResource.TMSUCCESS);
    session.prepare(xid2);
    session.close();
    sf.close();
    server.fail(false);
    server.start();
    for (int start = 0; start < 2; start++) {
        System.out.println("Start " + start);
        sf = createSessionFactory(locator);
        if (start == 0) {
            session = sf.createSession(true, false, false);
            session.commit(xid1, false);
            session.close();
        }
        session = sf.createSession(false, false, false);
        ClientConsumer cons1 = session.createConsumer(ADDRESS);
        session.start();
        for (int i = 0; i < 10; i++) {
            log.info("I = " + i);
            ClientMessage msg = cons1.receive(5000);
            Assert.assertNotNull(msg);
            Assert.assertEquals(1, msg.getIntProperty("txid").intValue());
            msg.acknowledge();
        }
        if (start == 1) {
            session.commit();
        } else {
            session.rollback();
        }
        session.close();
        sf.close();
        server.stop();
        server.start();
    }
    server.stop();
    validateNoFilesOnLargeDir(server.getConfiguration().getLargeMessagesDirectory(), 10);
    server.start();
    sf = createSessionFactory(locator);
    session = sf.createSession(true, false, false);
    session.rollback(xid2);
    sf.close();
    server.stop();
    server.start();
    server.stop();
    validateNoFilesOnLargeDir();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) SessionContinuationMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionContinuationMessage) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Xid(javax.transaction.xa.Xid) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Aggregations

ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)859 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)716 Test (org.junit.Test)702 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)658 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)644 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)478 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)395 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)173 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)166 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)106 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)103 CountDownLatch (java.util.concurrent.CountDownLatch)102 Configuration (org.apache.activemq.artemis.core.config.Configuration)93 Queue (org.apache.activemq.artemis.core.server.Queue)85 Xid (javax.transaction.xa.Xid)70 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)64 HashMap (java.util.HashMap)63 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)55 ArrayList (java.util.ArrayList)51 QueueControl (org.apache.activemq.artemis.api.core.management.QueueControl)50