Search in sources :

Example 71 with BrokerService

use of org.apache.activemq.broker.BrokerService in project activemq-artemis by apache.

the class TopicSubscriptionZeroPrefetchTest method createBroker.

// helper method to create a broker with slow consumer advisory turned on
private BrokerService createBroker() throws Exception {
    BrokerService broker = new BrokerService();
    broker.setBrokerName("localhost");
    broker.setUseJmx(false);
    broker.setDeleteAllMessagesOnStartup(true);
    broker.addConnector("vm://localhost");
    broker.start();
    broker.waitUntilStarted();
    return broker;
}
Also used : BrokerService(org.apache.activemq.broker.BrokerService)

Example 72 with BrokerService

use of org.apache.activemq.broker.BrokerService in project activemq-artemis by apache.

the class TwoBrokerQueueClientsReconnectTest method testDuplicateSendWithNoAuditEnqueueCountStat.

public void testDuplicateSendWithNoAuditEnqueueCountStat() throws Exception {
    broker1 = "BrokerA";
    broker2 = "BrokerB";
    NetworkConnector networkConnector = bridgeBrokers(broker1, broker2);
    final AtomicBoolean first = new AtomicBoolean();
    final CountDownLatch gotMessageLatch = new CountDownLatch(1);
    BrokerService brokerService = brokers.get(broker2).broker;
    brokerService.setPersistent(true);
    brokerService.setDeleteAllMessagesOnStartup(true);
    // disable concurrent dispatch otherwise store duplicate suppression will be skipped b/c cursor audit is already
    // disabled so verification of stats will fail - ie: duplicate will be dispatched
    ((KahaDBPersistenceAdapter) brokerService.getPersistenceAdapter()).setConcurrentStoreAndDispatchQueues(false);
    brokerService.setPlugins(new BrokerPlugin[] { new BrokerPluginSupport() {

        @Override
        public void send(final ProducerBrokerExchange producerExchange, org.apache.activemq.command.Message messageSend) throws Exception {
            super.send(producerExchange, messageSend);
            if (first.compareAndSet(false, true)) {
                producerExchange.getConnectionContext().setDontSendReponse(true);
                new Thread() {

                    @Override
                    public void run() {
                        try {
                            LOG.info("Waiting for recepit");
                            assertTrue("message received on time", gotMessageLatch.await(60, TimeUnit.SECONDS));
                            LOG.info("Stopping connection post send and receive and multiple producers");
                            producerExchange.getConnectionContext().getConnection().stop();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        }
    } });
    // Create queue
    ActiveMQDestination dest = createDestination("TEST.FOO", false);
    // statically include our destination
    networkConnector.addStaticallyIncludedDestination(dest);
    // Run brokers
    startAllBrokers();
    waitForBridgeFormation();
    sendMessages("BrokerA", dest, 1);
    // wait for broker2 to get the initial forward
    Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return brokers.get(broker2).broker.getAdminView().getTotalMessageCount() == 1;
        }
    });
    // message still pending on broker1
    assertEquals("messages message still there", 1, brokers.get(broker1).broker.getAdminView().getTotalMessageCount());
    // allow the bridge to be shutdown and restarted
    gotMessageLatch.countDown();
    // verify message is forwarded after restart
    assertTrue("no messages enqueued on origin", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return 0 == brokers.get(broker1).broker.getAdminView().getTotalMessageCount();
        }
    }));
    assertEquals("one messages pending", 1, brokers.get(broker2).broker.getAdminView().getTotalMessageCount());
    assertEquals("one messages enqueued", 1, brokers.get(broker2).broker.getDestination(dest).getDestinationStatistics().getEnqueues().getCount());
}
Also used : ProducerBrokerExchange(org.apache.activemq.broker.ProducerBrokerExchange) BrokerPluginSupport(org.apache.activemq.broker.BrokerPluginSupport) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NetworkConnector(org.apache.activemq.network.NetworkConnector) Wait(org.apache.activemq.util.Wait) BrokerService(org.apache.activemq.broker.BrokerService) KahaDBPersistenceAdapter(org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter)

Example 73 with BrokerService

use of org.apache.activemq.broker.BrokerService in project activemq-artemis by apache.

the class TwoBrokerQueueClientsReconnectTest method testDuplicateSend.

public void testDuplicateSend() throws Exception {
    broker1 = "BrokerA";
    broker2 = "BrokerB";
    // enable producer audit for the network connector, off by default b/c of interference with composite
    // dests and virtual topics
    brokers.get(broker2).broker.getTransportConnectors().get(0).setAuditNetworkProducers(true);
    bridgeBrokers(broker1, broker2);
    final AtomicBoolean first = new AtomicBoolean();
    final CountDownLatch gotMessageLatch = new CountDownLatch(1);
    BrokerService brokerService = brokers.get(broker2).broker;
    brokerService.setPersistent(true);
    brokerService.setDeleteAllMessagesOnStartup(true);
    brokerService.setPlugins(new BrokerPlugin[] { new BrokerPluginSupport() {

        @Override
        public void send(final ProducerBrokerExchange producerExchange, org.apache.activemq.command.Message messageSend) throws Exception {
            super.send(producerExchange, messageSend);
            if (first.compareAndSet(false, true)) {
                producerExchange.getConnectionContext().setDontSendReponse(true);
                new Thread() {

                    @Override
                    public void run() {
                        try {
                            LOG.info("Waiting for recepit");
                            assertTrue("message received on time", gotMessageLatch.await(60, TimeUnit.SECONDS));
                            LOG.info("Stopping connection post send and receive and multiple producers");
                            producerExchange.getConnectionContext().getConnection().stop();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        }
    } });
    // Run brokers
    startAllBrokers();
    waitForBridgeFormation();
    // Create queue
    Destination dest = createDestination("TEST.FOO", false);
    MessageConsumer client2 = createConsumer(broker2, dest);
    sendMessages("BrokerA", dest, 1);
    assertEquals("Client got message", 1, receiveExactMessages(client2, 1));
    client2.close();
    gotMessageLatch.countDown();
    // message still pending on broker1
    assertEquals("messages message still there", 1, brokers.get(broker1).broker.getAdminView().getTotalMessageCount());
    client2 = createConsumer(broker2, dest);
    LOG.info("Let the second client receive the rest of the messages");
    assertEquals("no duplicate message", 0, receiveAllMessages(client2));
    assertEquals("no duplicate message", 0, receiveAllMessages(client2));
    assertEquals("no messages enqueued", 0, brokers.get(broker2).broker.getAdminView().getTotalMessageCount());
    assertTrue("no messages enqueued on origin", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return 0 == brokers.get(broker1).broker.getAdminView().getTotalMessageCount();
        }
    }));
}
Also used : ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) Destination(javax.jms.Destination) ProducerBrokerExchange(org.apache.activemq.broker.ProducerBrokerExchange) MessageConsumer(javax.jms.MessageConsumer) BrokerPluginSupport(org.apache.activemq.broker.BrokerPluginSupport) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BrokerService(org.apache.activemq.broker.BrokerService)

Example 74 with BrokerService

use of org.apache.activemq.broker.BrokerService in project activemq-artemis by apache.

the class NetworkBridgeProducerFlowControlTest method testFastAndSlowRemoteConsumers.

/**
 * This test is parameterized by {@link #persistentTestMessages}, which
 * determines whether the producer on broker0 sends persistent or
 * non-persistent messages, and {@link #networkIsAlwaysSendSync}, which
 * determines how the bridge will forward both persistent and non-persistent
 * messages to broker1.
 *
 * @see #initCombosForTestFastAndSlowRemoteConsumers()
 */
public void testFastAndSlowRemoteConsumers() throws Exception {
    final int NUM_MESSAGES = 100;
    final long TEST_MESSAGE_SIZE = 1024;
    final long SLOW_CONSUMER_DELAY_MILLIS = 100;
    // Consumer prefetch is disabled for broker1's consumers.
    final ActiveMQQueue SLOW_SHARED_QUEUE = new ActiveMQQueue(NetworkBridgeProducerFlowControlTest.class.getSimpleName() + ".slow.shared?consumer.prefetchSize=1");
    final ActiveMQQueue FAST_SHARED_QUEUE = new ActiveMQQueue(NetworkBridgeProducerFlowControlTest.class.getSimpleName() + ".fast.shared?consumer.prefetchSize=1");
    // Start a local and a remote broker.
    createBroker(new URI("broker:(tcp://localhost:0" + ")?brokerName=broker0&persistent=false&useJmx=true"));
    BrokerService remoteBroker = createBroker(new URI("broker:(tcp://localhost:0" + ")?brokerName=broker1&persistent=false&useJmx=true"));
    // Set a policy on the remote broker that limits the maximum size of the
    // slow shared queue.
    PolicyEntry policyEntry = new PolicyEntry();
    policyEntry.setMemoryLimit(5 * TEST_MESSAGE_SIZE);
    PolicyMap policyMap = new PolicyMap();
    policyMap.put(SLOW_SHARED_QUEUE, policyEntry);
    remoteBroker.setDestinationPolicy(policyMap);
    // Create an outbound bridge from the local broker to the remote broker.
    // The bridge is configured with the remoteDispatchType enhancement.
    NetworkConnector nc = bridgeBrokers("broker0", "broker1");
    nc.setAlwaysSyncSend(networkIsAlwaysSendSync);
    nc.setPrefetchSize(1);
    startAllBrokers();
    waitForBridgeFormation();
    // Send the test messages to the local broker's shared queues. The
    // messages are either persistent or non-persistent to demonstrate the
    // difference between synchronous and asynchronous dispatch.
    persistentDelivery = persistentTestMessages;
    sendMessages("broker0", FAST_SHARED_QUEUE, NUM_MESSAGES);
    sendMessages("broker0", SLOW_SHARED_QUEUE, NUM_MESSAGES);
    // Start two asynchronous consumers on the remote broker, one for each
    // of the two shared queues, and keep track of how long it takes for
    // each of the consumers to receive all the messages.
    final CountDownLatch fastConsumerLatch = new CountDownLatch(NUM_MESSAGES);
    final CountDownLatch slowConsumerLatch = new CountDownLatch(NUM_MESSAGES);
    final long startTimeMillis = System.currentTimeMillis();
    final AtomicLong fastConsumerTime = new AtomicLong();
    final AtomicLong slowConsumerTime = new AtomicLong();
    Thread fastWaitThread = new Thread() {

        @Override
        public void run() {
            try {
                fastConsumerLatch.await();
                fastConsumerTime.set(System.currentTimeMillis() - startTimeMillis);
            } catch (InterruptedException ex) {
                exceptions.add(ex);
                Assert.fail(ex.getMessage());
            }
        }
    };
    Thread slowWaitThread = new Thread() {

        @Override
        public void run() {
            try {
                slowConsumerLatch.await();
                slowConsumerTime.set(System.currentTimeMillis() - startTimeMillis);
            } catch (InterruptedException ex) {
                exceptions.add(ex);
                Assert.fail(ex.getMessage());
            }
        }
    };
    fastWaitThread.start();
    slowWaitThread.start();
    createConsumer("broker1", FAST_SHARED_QUEUE, fastConsumerLatch);
    MessageConsumer slowConsumer = createConsumer("broker1", SLOW_SHARED_QUEUE, slowConsumerLatch);
    MessageIdList messageIdList = brokers.get("broker1").consumers.get(slowConsumer);
    messageIdList.setProcessingDelay(SLOW_CONSUMER_DELAY_MILLIS);
    fastWaitThread.join();
    slowWaitThread.join();
    assertTrue("no exceptions on the wait threads:" + exceptions, exceptions.isEmpty());
    LOG.info("Fast consumer duration (ms): " + fastConsumerTime.get());
    LOG.info("Slow consumer duration (ms): " + slowConsumerTime.get());
    // Verify the behaviour as described in the description of this class.
    if (networkIsAlwaysSendSync) {
        Assert.assertTrue(fastConsumerTime.get() < slowConsumerTime.get() / 10);
    } else {
        Assert.assertEquals(persistentTestMessages, fastConsumerTime.get() < slowConsumerTime.get() / 10);
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) MessageIdList(org.apache.activemq.util.MessageIdList) URI(java.net.URI) PolicyEntry(org.apache.activemq.broker.region.policy.PolicyEntry) AtomicLong(java.util.concurrent.atomic.AtomicLong) PolicyMap(org.apache.activemq.broker.region.policy.PolicyMap) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) NetworkConnector(org.apache.activemq.network.NetworkConnector) BrokerService(org.apache.activemq.broker.BrokerService)

Example 75 with BrokerService

use of org.apache.activemq.broker.BrokerService in project activemq-artemis by apache.

the class NetworkBridgeProducerFlowControlTest method doTestSendFailIfNoSpaceDoesNotBlockNetwork.

public void doTestSendFailIfNoSpaceDoesNotBlockNetwork(ActiveMQDestination slowDestination, ActiveMQDestination fastDestination) throws Exception {
    final int NUM_MESSAGES = 100;
    final long TEST_MESSAGE_SIZE = 1024;
    final long SLOW_CONSUMER_DELAY_MILLIS = 100;
    // Start a local and a remote broker.
    createBroker(new URI("broker:(tcp://localhost:0" + ")?brokerName=broker0&persistent=false&useJmx=true"));
    BrokerService remoteBroker = createBroker(new URI("broker:(tcp://localhost:0" + ")?brokerName=broker1&persistent=false&useJmx=true"));
    remoteBroker.getSystemUsage().setSendFailIfNoSpace(true);
    // Set a policy on the remote broker that limits the maximum size of the
    // slow shared queue.
    PolicyEntry policyEntry = new PolicyEntry();
    policyEntry.setMemoryLimit(5 * TEST_MESSAGE_SIZE);
    PolicyMap policyMap = new PolicyMap();
    policyMap.put(slowDestination, policyEntry);
    remoteBroker.setDestinationPolicy(policyMap);
    // Create an outbound bridge from the local broker to the remote broker.
    // The bridge is configured with the remoteDispatchType enhancement.
    NetworkConnector nc = bridgeBrokers("broker0", "broker1");
    nc.setAlwaysSyncSend(true);
    nc.setPrefetchSize(1);
    startAllBrokers();
    waitForBridgeFormation();
    // Start two asynchronous consumers on the remote broker, one for each
    // of the two shared queues, and keep track of how long it takes for
    // each of the consumers to receive all the messages.
    final CountDownLatch fastConsumerLatch = new CountDownLatch(NUM_MESSAGES);
    final CountDownLatch slowConsumerLatch = new CountDownLatch(NUM_MESSAGES);
    final long startTimeMillis = System.currentTimeMillis();
    final AtomicLong fastConsumerTime = new AtomicLong();
    final AtomicLong slowConsumerTime = new AtomicLong();
    Thread fastWaitThread = new Thread() {

        @Override
        public void run() {
            try {
                fastConsumerLatch.await();
                fastConsumerTime.set(System.currentTimeMillis() - startTimeMillis);
            } catch (InterruptedException ex) {
                exceptions.add(ex);
                Assert.fail(ex.getMessage());
            }
        }
    };
    Thread slowWaitThread = new Thread() {

        @Override
        public void run() {
            try {
                slowConsumerLatch.await();
                slowConsumerTime.set(System.currentTimeMillis() - startTimeMillis);
            } catch (InterruptedException ex) {
                exceptions.add(ex);
                Assert.fail(ex.getMessage());
            }
        }
    };
    fastWaitThread.start();
    slowWaitThread.start();
    createConsumer("broker1", fastDestination, fastConsumerLatch);
    MessageConsumer slowConsumer = createConsumer("broker1", slowDestination, slowConsumerLatch);
    MessageIdList messageIdList = brokers.get("broker1").consumers.get(slowConsumer);
    messageIdList.setProcessingDelay(SLOW_CONSUMER_DELAY_MILLIS);
    // Send the test messages to the local broker's shared queues. The
    // messages are either persistent or non-persistent to demonstrate the
    // difference between synchronous and asynchronous dispatch.
    persistentDelivery = false;
    sendMessages("broker0", fastDestination, NUM_MESSAGES);
    sendMessages("broker0", slowDestination, NUM_MESSAGES);
    fastWaitThread.join(TimeUnit.SECONDS.toMillis(60));
    slowWaitThread.join(TimeUnit.SECONDS.toMillis(60));
    assertTrue("no exceptions on the wait threads:" + exceptions, exceptions.isEmpty());
    LOG.info("Fast consumer duration (ms): " + fastConsumerTime.get());
    LOG.info("Slow consumer duration (ms): " + slowConsumerTime.get());
    assertTrue("fast time set", fastConsumerTime.get() > 0);
    assertTrue("slow time set", slowConsumerTime.get() > 0);
    // Verify the behaviour as described in the description of this class.
    Assert.assertTrue(fastConsumerTime.get() < slowConsumerTime.get() / 10);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) MessageIdList(org.apache.activemq.util.MessageIdList) URI(java.net.URI) PolicyEntry(org.apache.activemq.broker.region.policy.PolicyEntry) AtomicLong(java.util.concurrent.atomic.AtomicLong) PolicyMap(org.apache.activemq.broker.region.policy.PolicyMap) NetworkConnector(org.apache.activemq.network.NetworkConnector) BrokerService(org.apache.activemq.broker.BrokerService)

Aggregations

BrokerService (org.apache.activemq.broker.BrokerService)349 URI (java.net.URI)53 PolicyEntry (org.apache.activemq.broker.region.policy.PolicyEntry)45 PolicyMap (org.apache.activemq.broker.region.policy.PolicyMap)45 Test (org.junit.Test)43 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)32 TransportConnector (org.apache.activemq.broker.TransportConnector)31 Before (org.junit.Before)22 File (java.io.File)20 MemoryPersistenceAdapter (org.apache.activemq.store.memory.MemoryPersistenceAdapter)17 JMSException (javax.jms.JMSException)16 JDBCPersistenceAdapter (org.apache.activemq.store.jdbc.JDBCPersistenceAdapter)14 KahaDBPersistenceAdapter (org.apache.activemq.store.kahadb.KahaDBPersistenceAdapter)14 BrokerFactoryBean (org.apache.activemq.xbean.BrokerFactoryBean)14 NetworkConnector (org.apache.activemq.network.NetworkConnector)13 MessageConsumer (javax.jms.MessageConsumer)12 PersistenceAdapter (org.apache.activemq.store.PersistenceAdapter)12 ArrayList (java.util.ArrayList)11 Destination (javax.jms.Destination)11 ClassPathResource (org.springframework.core.io.ClassPathResource)10