Search in sources :

Example 6 with ActiveMQObjectClosedException

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

the class FailoverTest method testTimeoutOnFailoverConsumeBlocked.

@Test(timeout = 120000)
public void testTimeoutOnFailoverConsumeBlocked() throws Exception {
    locator.setCallTimeout(5000).setBlockOnNonDurableSend(true).setConsumerWindowSize(0).setBlockOnDurableSend(true).setAckBatchSize(0).setBlockOnAcknowledge(true).setReconnectAttempts(-1).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);
        message.putBooleanProperty("end", i == 499);
        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<>();
    Thread t = new Thread() {

        @Override
        public void run() {
            ClientMessage message = null;
            try {
                while ((message = getMessage()) != null) {
                    Integer counter = message.getIntProperty("counter");
                    received.put(counter, message);
                    try {
                        log.info("acking message = id = " + message.getMessageID() + ", counter = " + message.getIntProperty("counter"));
                        message.acknowledge();
                    } catch (ActiveMQException e) {
                        e.printStackTrace();
                        continue;
                    }
                    log.info("Acked counter = " + counter);
                    if (counter.equals(10)) {
                        latch.countDown();
                    }
                    if (received.size() == 500) {
                        endLatch.countDown();
                    }
                    if (message.getBooleanProperty("end")) {
                        break;
                    }
                }
            } catch (Exception e) {
                Assert.fail("failing due to exception " + e);
            }
        }

        private ClientMessage getMessage() {
            while (true) {
                try {
                    ClientMessage msg = consumer.receive(20000);
                    if (msg == null) {
                        log.info("Returning null message on consuming");
                    }
                    return msg;
                } catch (ActiveMQObjectClosedException oce) {
                    throw new RuntimeException(oce);
                } catch (ActiveMQException ignored) {
                    // retry
                    ignored.printStackTrace();
                }
            }
        }
    };
    t.start();
    latch.await(10, TimeUnit.SECONDS);
    log.info("crashing session");
    crash(session);
    endLatch.await(60, TimeUnit.SECONDS);
    t.join();
    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) HashMap(java.util.HashMap) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQTransactionOutcomeUnknownException(org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQDuplicateIdException(org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException) ActiveMQTransactionRolledBackException(org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) XAException(javax.transaction.xa.XAException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 7 with ActiveMQObjectClosedException

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

the class KillSlowConsumerExample method main.

public static void main(final String[] args) throws Exception {
    // Step 1. Create an initial context to perform the JNDI lookup.
    InitialContext initialContext = new InitialContext();
    // Step 2. Perform a lookup on the queue
    Queue slowConsumerKillQueue = (Queue) initialContext.lookup("queue/slow.consumer.kill");
    // Step 3. Perform a lookup on the Connection Factory
    ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
    // Step 4.Create a JMS Connection
    try (Connection connection = connectionFactory.createConnection()) {
        // Step 5. Create a JMS Session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 6. Create a JMS Message Producer
        MessageProducer producer = session.createProducer(slowConsumerKillQueue);
        // Step 7. Create a Text Message
        TextMessage message = session.createTextMessage("This is a text message");
        System.out.println("Sending messages to queue ... ");
        // Step 8. Send messages to the queue
        for (int i = 0; i < 50; i++) {
            producer.send(message);
        }
        // Step 9. Create a JMS Message Consumer
        MessageConsumer messageConsumer = session.createConsumer(slowConsumerKillQueue);
        // Step 10. Start the Connection
        connection.start();
        System.out.println("About to wait for " + WAIT_TIME + " seconds");
        // Step 11. Wait for slow consumer to be detected
        Thread.sleep(TimeUnit.SECONDS.toMillis(WAIT_TIME));
        try {
            // step 12. Try to us the connection - expect it to be closed already
            messageConsumer.receive(TimeUnit.SECONDS.toMillis(1));
            // messageConsumer.receive() should throw exception - we should not get to here.
            throw new RuntimeException("SlowConsumerExample.slowConsumerKill() FAILED - expected " + "connection to be shutdown by Slow Consumer policy");
        } catch (JMSException ex) {
            if (ex.getCause() instanceof ActiveMQObjectClosedException) {
                // received exception - as expected
                System.out.println("SUCCESS! Received EXPECTED exception: " + ex);
            } else {
                throw new RuntimeException("SlowConsumerExample.slowConsumerKill() FAILED - expected " + "ActiveMQObjectClosedException BUT got " + ex.getCause());
            }
        }
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 8 with ActiveMQObjectClosedException

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

the class SlowConsumerTest method testSlowConsumerKilled.

@Test
public void testSlowConsumerKilled() throws Exception {
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = addClientSession(sf.createSession(false, true, true, false));
    ClientProducer producer = addClientProducer(session.createProducer(QUEUE));
    assertPaging();
    final int numMessages = 25;
    for (int i = 0; i < numMessages; i++) {
        producer.send(createTextMessage(session, "m" + i));
    }
    ClientConsumer consumer = addClientConsumer(session.createConsumer(QUEUE));
    session.start();
    Thread.sleep(3000);
    try {
        consumer.receiveImmediate();
        fail();
    } catch (ActiveMQObjectClosedException e) {
        assertEquals(e.getType(), ActiveMQExceptionType.OBJECT_CLOSED);
    }
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) 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

ActiveMQObjectClosedException (org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException)8 Test (org.junit.Test)7 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)6 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)6 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)5 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)4 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)3 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)3 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)3 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)2 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)2 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)2 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)2 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)2 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 JMSException (javax.jms.JMSException)1