Search in sources :

Example 81 with ActiveMQException

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

the class FailoverTest method testTimeoutOnFailover.

// https://issues.jboss.org/browse/HORNETQ-685
@Test(timeout = 120000)
public void testTimeoutOnFailover() throws Exception {
    locator.setCallTimeout(1000).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setAckBatchSize(0).setReconnectAttempts(300).setRetryInterval(100);
    if (nodeManager instanceof InVMNodeManager) {
        ((InVMNodeManager) nodeManager).failoverPause = 500L;
    }
    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);
    final CountDownLatch latch = new CountDownLatch(10);
    final CountDownLatch latchFailed = new CountDownLatch(1);
    Runnable r = new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < 500; i++) {
                ClientMessage message = session.createMessage(true);
                message.putIntProperty("counter", i);
                try {
                    System.out.println("Sent " + i);
                    producer.send(message);
                    if (i < 10) {
                        latch.countDown();
                        if (latch.getCount() == 0) {
                            latchFailed.await(10, TimeUnit.SECONDS);
                        }
                    }
                } catch (Exception e) {
                    // this is our retry
                    try {
                        if (!producer.isClosed())
                            producer.send(message);
                    } catch (ActiveMQException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    };
    Thread t = new Thread(r);
    t.start();
    Assert.assertTrue("latch released", latch.await(10, TimeUnit.SECONDS));
    crash(session);
    latchFailed.countDown();
    t.join(30000);
    if (t.isAlive()) {
        t.interrupt();
        Assert.fail("Thread still alive");
    }
    Assert.assertTrue(backupServer.getServer().waitForActivation(5, TimeUnit.SECONDS));
    ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    for (int i = 0; i < 500; i++) {
        ClientMessage m = consumer.receive(1000);
        Assert.assertNotNull("message #=" + i, m);
    // assertEquals(i, m.getIntProperty("counter").intValue());
    }
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) 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) 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) Test(org.junit.Test)

Example 82 with ActiveMQException

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

the class FailoverTest method testForceBlockingReturn.

@Test(timeout = 120000)
public void testForceBlockingReturn() throws Exception {
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true).setReconnectAttempts(300).setRetryInterval(100);
    createClientSessionFactory();
    // Add an interceptor to delay the send method so we can get time to cause failover before it returns
    liveServer.getServer().getRemotingService().addIncomingInterceptor(new DelayInterceptor());
    final ClientSession session = createSession(sf, true, true, 0);
    session.createQueue(FailoverTestBase.ADDRESS, RoutingType.MULTICAST, FailoverTestBase.ADDRESS, null, true);
    final ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    class Sender extends Thread {

        @Override
        public void run() {
            ClientMessage message = session.createMessage(true);
            message.getBodyBuffer().writeString("message");
            try {
                producer.send(message);
            } catch (ActiveMQException e1) {
                this.e = e1;
            }
        }

        volatile ActiveMQException e;
    }
    Sender sender = new Sender();
    sender.start();
    crash(session);
    sender.join();
    Assert.assertNotNull(sender.e);
    Assert.assertNotNull(sender.e.getCause());
    Assert.assertEquals(sender.e.getType(), ActiveMQExceptionType.UNBLOCKED);
    Assert.assertEquals(((ActiveMQException) sender.e.getCause()).getType(), ActiveMQExceptionType.DISCONNECTED);
    session.close();
}
Also used : 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) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 83 with ActiveMQException

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

the class FailoverTest method testTransactedMessagesSentSoRollback.

@Test(timeout = 120000)
public void testTransactedMessagesSentSoRollback() 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());
    }
    ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    ClientMessage message = consumer.receiveImmediate();
    Assert.assertNull("message should be null! Was: " + message, message);
    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 84 with ActiveMQException

use of org.apache.activemq.artemis.api.core.ActiveMQException 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 85 with ActiveMQException

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

the class FailoverOnFlowControlTest method testOverflowSend.

@Test
public void testOverflowSend() throws Exception {
    ServerLocator locator = getServerLocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setReconnectAttempts(300).setProducerWindowSize(1000).setRetryInterval(100);
    final ArrayList<ClientSession> sessionList = new ArrayList<>();
    Interceptor interceptorClient = new Interceptor() {

        AtomicInteger count = new AtomicInteger(0);

        @Override
        public boolean intercept(Packet packet, RemotingConnection connection) throws ActiveMQException {
            log.debug("Intercept..." + packet.getClass().getName());
            if (packet instanceof SessionProducerCreditsMessage) {
                SessionProducerCreditsMessage credit = (SessionProducerCreditsMessage) packet;
                log.debug("Credits: " + credit.getCredits());
                if (count.incrementAndGet() == 2) {
                    log.debug("### crashing server");
                    try {
                        InVMConnection.setFlushEnabled(false);
                        crash(false, sessionList.get(0));
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        InVMConnection.setFlushEnabled(true);
                    }
                    return false;
                }
            }
            return true;
        }
    };
    locator.addIncomingInterceptor(interceptorClient);
    ClientSessionFactoryInternal sf = createSessionFactoryAndWaitForTopology(locator, 2);
    ClientSession session = sf.createSession(true, true);
    sessionList.add(session);
    session.createQueue(ADDRESS, ADDRESS, null, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    final int numMessages = 10;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(true);
        message.getBodyBuffer().writeBytes(new byte[5000]);
        message.putIntProperty("counter", i);
        producer.send(message);
    }
    session.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) SessionProducerCreditsMessage(org.apache.activemq.artemis.core.protocol.core.impl.wireformat.SessionProducerCreditsMessage) ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ArrayList(java.util.ArrayList) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

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