Search in sources :

Example 16 with MessageIdList

use of org.apache.activemq.util.MessageIdList in project activemq-artemis by apache.

the class TwoBrokerMulticastQueueTest method receiveMessages.

protected int receiveMessages(Connection conn, Destination dest, int waitTime) throws JMSException, InterruptedException {
    conn.start();
    MessageIdList list = new MessageIdList();
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = sess.createConsumer(dest);
    consumer.setMessageListener(list);
    if (waitTime > 0) {
        Thread.sleep(waitTime);
    } else {
        list.waitForMessagesToArrive(MESSAGE_COUNT);
    }
    conn.close();
    return list.getMessageCount();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MessageIdList(org.apache.activemq.util.MessageIdList) Session(javax.jms.Session)

Example 17 with MessageIdList

use of org.apache.activemq.util.MessageIdList 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 18 with MessageIdList

use of org.apache.activemq.util.MessageIdList 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)

Example 19 with MessageIdList

use of org.apache.activemq.util.MessageIdList in project activemq-artemis by apache.

the class RetroactiveConsumerTestWithSimpleMessageListTest method testSendThenConsume.

public void testSendThenConsume() throws Exception {
    // lets some messages
    connection = createConnection();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer producer = createProducer();
    for (int i = 0; i < messageCount; i++) {
        TextMessage message = session.createTextMessage("Message: " + i + " sent at: " + new Date());
        sendMessage(producer, message);
    }
    producer.close();
    session.close();
    connection.close();
    connection = createConnection();
    connection.start();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = createConsumer();
    MessageIdList listener = new MessageIdList();
    consumer.setMessageListener(listener);
    listener.waitForMessagesToArrive(messageCount);
    listener.assertMessagesReceived(messageCount);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MessageProducer(javax.jms.MessageProducer) MessageIdList(org.apache.activemq.util.MessageIdList) TextMessage(javax.jms.TextMessage) Date(java.util.Date)

Example 20 with MessageIdList

use of org.apache.activemq.util.MessageIdList in project activemq-artemis by apache.

the class RetroactiveConsumerWithMessageQueryTest method testConsumeAndReceiveInitialQueryBeforeUpdates.

public void testConsumeAndReceiveInitialQueryBeforeUpdates() throws Exception {
    // lets some messages
    connection = createConnection();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    connection.start();
    MessageConsumer consumer = session.createConsumer(destination);
    MessageIdList listener = new MessageIdList();
    listener.setVerbose(true);
    consumer.setMessageListener(listener);
    MessageProducer producer = session.createProducer(destination);
    int updateMessageCount = messageCount - DummyMessageQuery.MESSAGE_COUNT;
    for (int i = 0; i < updateMessageCount; i++) {
        TextMessage message = session.createTextMessage("Update Message: " + i + " sent at: " + new Date());
        producer.send(message);
    }
    producer.close();
    LOG.info("Sent: " + updateMessageCount + " update messages");
    listener.assertMessagesReceived(messageCount);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) MessageProducer(javax.jms.MessageProducer) MessageIdList(org.apache.activemq.util.MessageIdList) TextMessage(javax.jms.TextMessage) Date(java.util.Date)

Aggregations

MessageIdList (org.apache.activemq.util.MessageIdList)63 MessageConsumer (javax.jms.MessageConsumer)58 Destination (javax.jms.Destination)34 ActiveMQDestination (org.apache.activemq.command.ActiveMQDestination)31 CountDownLatch (java.util.concurrent.CountDownLatch)8 Connection (javax.jms.Connection)6 VirtualDestination (org.apache.activemq.broker.region.virtual.VirtualDestination)5 Test (org.junit.Test)5 URI (java.net.URI)4 BrokerService (org.apache.activemq.broker.BrokerService)4 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)4 NetworkConnector (org.apache.activemq.network.NetworkConnector)4 HashMap (java.util.HashMap)3 Session (javax.jms.Session)3 ActiveMQMessageConsumer (org.apache.activemq.ActiveMQMessageConsumer)3 TestSupport.getDestination (org.apache.activemq.TestSupport.getDestination)3 Destination (org.apache.activemq.broker.region.Destination)3 PolicyEntry (org.apache.activemq.broker.region.policy.PolicyEntry)3 PolicyMap (org.apache.activemq.broker.region.policy.PolicyMap)3 ActiveMQTopic (org.apache.activemq.command.ActiveMQTopic)3