Search in sources :

Example 86 with ActiveMQException

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

the class BackupSyncLargeMessageTest method testBackupStartsWhenLiveIsReceivingLargeMessage.

/**
 * LargeMessages are passed from the client to the server in chunks. Here we test the backup
 * starting the data synchronization with the live in the middle of a multiple chunks large
 * message upload from the client to the live server.
 *
 * @throws Exception
 */
@Test
public void testBackupStartsWhenLiveIsReceivingLargeMessage() throws Exception {
    final ClientSession session = addClientSession(sessionFactory.createSession(true, true));
    session.createQueue(FailoverTestBase.ADDRESS, FailoverTestBase.ADDRESS, null, true);
    final ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    final ClientMessage message = session.createMessage(true);
    final int largeMessageSize = 1000 * MIN_LARGE_MESSAGE;
    message.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(largeMessageSize));
    final AtomicBoolean caughtException = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                latch.countDown();
                producer.send(message);
                sendMessages(session, producer, 20);
                session.commit();
            } catch (ActiveMQException e) {
                e.printStackTrace();
                caughtException.set(true);
            } finally {
                latch2.countDown();
            }
        }
    };
    Executors.defaultThreadFactory().newThread(r).start();
    waitForLatch(latch);
    startBackupFinishSyncing();
    ActiveMQTestBase.waitForLatch(latch2);
    crash(session);
    assertFalse("no exceptions while sending message", caughtException.get());
    session.start();
    ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    ClientMessage msg = consumer.receive(2000);
    ActiveMQBuffer buffer = msg.getBodyBuffer();
    for (int j = 0; j < largeMessageSize; j++) {
        Assert.assertTrue("large msg , expecting " + largeMessageSize + " bytes, got " + j, buffer.readable());
        Assert.assertEquals("equal at " + j, ActiveMQTestBase.getSamplebyte(j), buffer.readByte());
    }
    receiveMessages(consumer, 0, 20, true);
    assertNull("there should be no more messages!", consumer.receiveImmediate());
    consumer.close();
    session.commit();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 87 with ActiveMQException

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

the class FailoverTest method testTimeoutOnFailoverConsume.

// https://issues.jboss.org/browse/HORNETQ-685
@Test(timeout = 120000)
public void testTimeoutOnFailoverConsume() throws Exception {
    locator.setCallTimeout(5000).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setAckBatchSize(0).setBlockOnAcknowledge(true).setReconnectAttempts(300).setRetryInterval(100).setAckBatchSize(0);
    if (nodeManager instanceof InVMNodeManager) {
        ((InVMNodeManager) nodeManager).failoverPause = 5000L;
    }
    ClientSessionFactoryInternal sf1 = (ClientSessionFactoryInternal) createSessionFactory(locator);
    final ClientSession session = createSession(sf1, true, true);
    session.createQueue(FailoverTestBase.ADDRESS, RoutingType.MULTICAST, FailoverTestBase.ADDRESS, null, true);
    final ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    for (int i = 0; i < 500; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("counter", i);
        producer.send(message);
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch endLatch = new CountDownLatch(1);
    final ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    final Map<Integer, ClientMessage> received = new HashMap<>();
    consumer.setMessageHandler(new MessageHandler() {

        @Override
        public void onMessage(ClientMessage message) {
            Integer counter = message.getIntProperty("counter");
            received.put(counter, message);
            try {
                log.debug("acking message = id = " + message.getMessageID() + ", counter = " + message.getIntProperty("counter"));
                message.acknowledge();
            } catch (ActiveMQException e) {
                e.printStackTrace();
                return;
            }
            log.debug("Acked counter = " + counter);
            if (counter.equals(10)) {
                latch.countDown();
            }
            if (received.size() == 500) {
                endLatch.countDown();
            }
        }
    });
    latch.await(10, TimeUnit.SECONDS);
    log.info("crashing session");
    crash(session);
    endLatch.await(60, TimeUnit.SECONDS);
    Assert.assertTrue("received only " + received.size(), received.size() == 500);
    session.close();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) HashMap(java.util.HashMap) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 88 with ActiveMQException

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

the class FailoverTest method testTransactedMessagesSentSoRollbackAndContinueWork.

/**
 * Test that once the transacted session has throw a TRANSACTION_ROLLED_BACK exception,
 * it can be reused again
 */
@Test(timeout = 120000)
public void testTransactedMessagesSentSoRollbackAndContinueWork() throws Exception {
    createSessionFactory();
    ClientSession session = createSessionAndQueue();
    ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    sendMessagesSomeDurable(session, producer);
    crash(session);
    Assert.assertTrue(session.isRollbackOnly());
    try {
        session.commit();
        Assert.fail("Should throw exception");
    } catch (ActiveMQTransactionRolledBackException trbe) {
    // ok
    } catch (ActiveMQException e) {
        Assert.fail("Invalid Exception type:" + e.getType());
    }
    ClientMessage message = session.createMessage(false);
    int counter = RandomUtil.randomInt();
    message.putIntProperty("counter", counter);
    producer.send(message);
    // session is working again
    session.commit();
    session.start();
    ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    message = consumer.receive(1000);
    Assert.assertNotNull("expecting a message", message);
    Assert.assertEquals(counter, message.getIntProperty("counter").intValue());
    session.close();
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQTransactionRolledBackException(org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) 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 89 with ActiveMQException

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

the class AsynchronousFailoverTest method doTestTransactional.

private void doTestTransactional(final TestRunner runner) throws Throwable {
    // For duplication detection
    int executionId = 0;
    while (!runner.isFailed()) {
        ClientSession session = null;
        executionId++;
        log.info("#test doTestTransactional starting now. Execution " + executionId);
        try {
            boolean retry = false;
            final int numMessages = 1000;
            session = sf.createSession(false, false);
            listener = new CountDownSessionFailureListener(session);
            session.addFailureListener(listener);
            do {
                try {
                    ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
                    for (int i = 0; i < numMessages; i++) {
                        ClientMessage message = session.createMessage(true);
                        message.getBodyBuffer().writeString("message" + i);
                        message.putIntProperty("counter", i);
                        message.putStringProperty(Message.HDR_DUPLICATE_DETECTION_ID, new SimpleString("id:" + i + ",exec:" + executionId));
                        addPayload(message);
                        if (log.isDebugEnabled()) {
                            log.debug("Sending message " + message);
                        }
                        producer.send(message);
                    }
                    log.debug("Sending commit");
                    session.commit();
                    retry = false;
                } catch (ActiveMQDuplicateIdException die) {
                    logAndSystemOut("#test duplicate id rejected on sending");
                    break;
                } catch (ActiveMQTransactionRolledBackException trbe) {
                    log.info("#test transaction rollback retrying on sending");
                    // OK
                    retry = true;
                } catch (ActiveMQUnBlockedException ube) {
                    log.info("#test transaction rollback retrying on sending");
                    // OK
                    retry = true;
                } catch (ActiveMQTransactionOutcomeUnknownException toue) {
                    log.info("#test transaction rollback retrying on sending");
                    // OK
                    retry = true;
                } catch (ActiveMQException e) {
                    log.info("#test Exception " + e, e);
                    throw e;
                }
            } while (retry);
            logAndSystemOut("#test Finished sending, starting consumption now");
            boolean blocked = false;
            retry = false;
            ClientConsumer consumer = null;
            do {
                ArrayList<Integer> msgs = new ArrayList<>();
                try {
                    if (consumer == null) {
                        consumer = session.createConsumer(FailoverTestBase.ADDRESS);
                        session.start();
                    }
                    for (int i = 0; i < numMessages; i++) {
                        if (log.isDebugEnabled()) {
                            log.debug("Consumer receiving message " + i);
                        }
                        ClientMessage message = consumer.receive(10000);
                        if (message == null) {
                            break;
                        }
                        if (log.isDebugEnabled()) {
                            log.debug("Received message " + message);
                        }
                        int count = message.getIntProperty("counter");
                        if (count != i) {
                            log.warn("count was received out of order, " + count + "!=" + i);
                        }
                        msgs.add(count);
                        message.acknowledge();
                    }
                    log.info("#test commit");
                    try {
                        session.commit();
                    } catch (ActiveMQTransactionRolledBackException trbe) {
                        // we know the tx has been rolled back so we just consume again
                        retry = true;
                        continue;
                    } catch (ActiveMQException e) {
                        // This could eventually happen
                        // We will get rid of this when we implement 2 phase commit on failover
                        log.warn("exception during commit, it will be ignored for now" + e.getMessage(), e);
                    }
                    try {
                        if (blocked) {
                            assertTrue("msgs.size is expected to be 0 or " + numMessages + " but it was " + msgs.size(), msgs.size() == 0 || msgs.size() == numMessages);
                        } else {
                            assertTrue("msgs.size is expected to be " + numMessages + " but it was " + msgs.size(), msgs.size() == numMessages);
                        }
                    } catch (Throwable e) {
                        log.info(threadDump("Thread dump, messagesReceived = " + msgs.size()));
                        logAndSystemOut(e.getMessage() + " messages received");
                        for (Integer msg : msgs) {
                            logAndSystemOut(msg.toString());
                        }
                        throw e;
                    }
                    int i = 0;
                    for (Integer msg : msgs) {
                        assertEquals(i++, (int) msg);
                    }
                    retry = false;
                    blocked = false;
                } catch (ActiveMQTransactionRolledBackException trbe) {
                    logAndSystemOut("Transaction rolled back with " + msgs.size(), trbe);
                    // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369
                    // ATM RolledBack exception is being called with the transaction is committed.
                    // the test will fail if you remove this next line
                    blocked = true;
                    retry = true;
                } catch (ActiveMQTransactionOutcomeUnknownException tou) {
                    logAndSystemOut("Transaction rolled back with " + msgs.size(), tou);
                    // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369
                    // ATM RolledBack exception is being called with the transaction is committed.
                    // the test will fail if you remove this next line
                    blocked = true;
                    retry = true;
                } catch (ActiveMQUnBlockedException ube) {
                    logAndSystemOut("Unblocked with " + msgs.size(), ube);
                    // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369
                    // This part of the test is never being called.
                    blocked = true;
                    retry = true;
                } catch (ActiveMQException e) {
                    logAndSystemOut(e.getMessage(), e);
                    throw e;
                }
            } while (retry);
        } finally {
            if (session != null) {
                session.close();
            }
        }
        listener = null;
    }
}
Also used : ActiveMQTransactionOutcomeUnknownException(org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) ActiveMQUnBlockedException(org.apache.activemq.artemis.api.core.ActiveMQUnBlockedException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQTransactionRolledBackException(org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQDuplicateIdException(org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Example 90 with ActiveMQException

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

the class SecurityTest method checkUserNoSendNoReceive.

private void checkUserNoSendNoReceive(final String queue, final ClientSession connection, final ClientSession sendingConn) throws Exception {
    connection.start();
    try {
        ClientProducer prod = connection.createProducer(queue);
        ClientMessage m = connection.createMessage(false);
        try {
            prod.send(m);
            Assert.fail("should throw exception");
        } catch (ActiveMQException e) {
        // pass
        }
        prod = sendingConn.createProducer(queue);
        prod.send(m);
        try {
            connection.createConsumer(queue);
            Assert.fail("should throw exception");
        } catch (ActiveMQException e) {
        // pass
        }
    } finally {
        connection.stop();
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Aggregations

ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)254 Test (org.junit.Test)140 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)121 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)84 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)79 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)78 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)59 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)54 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)45 CountDownLatch (java.util.concurrent.CountDownLatch)40 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)36 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)31 HashSet (java.util.HashSet)29 ActiveMQJAASSecurityManager (org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager)27 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)23 Role (org.apache.activemq.artemis.core.security.Role)22 ActiveMQSecurityException (org.apache.activemq.artemis.api.core.ActiveMQSecurityException)20 Set (java.util.Set)16 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)16 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)15