Search in sources :

Example 1 with PriorityNetworkDispatchPolicy

use of org.apache.activemq.broker.region.policy.PriorityNetworkDispatchPolicy in project activemq-artemis by apache.

the class NoDuplicateOnTopicNetworkTest method testProducerConsumerTopic.

public void testProducerConsumerTopic() throws Exception {
    final CountDownLatch consumerStarted = new CountDownLatch(1);
    Thread producerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            TopicWithDuplicateMessages producer = new TopicWithDuplicateMessages();
            producer.setBrokerURL(BROKER_1);
            producer.setTopicName(TOPIC_NAME);
            try {
                producer.produce();
            } catch (JMSException e) {
                fail("Unexpected " + e);
            }
        }
    });
    final TopicWithDuplicateMessages consumer = new TopicWithDuplicateMessages();
    Thread consumerThread = new Thread(new Runnable() {

        @Override
        public void run() {
            consumer.setBrokerURL(BROKER_2);
            consumer.setTopicName(TOPIC_NAME);
            try {
                consumer.consumer();
                consumerStarted.countDown();
                consumer.getLatch().await(60, TimeUnit.SECONDS);
            } catch (Exception e) {
                fail("Unexpected " + e);
            }
        }
    });
    consumerThread.start();
    LOG.info("Started Consumer");
    assertTrue("consumer started eventually", consumerStarted.await(10, TimeUnit.SECONDS));
    // ensure subscription has percolated though the network
    Thread.sleep(2000);
    // verify network consumer priority
    final RegionBroker regionBroker = (RegionBroker) broker1.getRegionBroker();
    assertTrue("Found network destination with priority as expected", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            Map<ActiveMQDestination, Destination> destinationMap = regionBroker.getTopicRegion().getDestinationMap();
            LOG.info("destinations: " + destinationMap.keySet());
            boolean found = false;
            for (Destination destination : destinationMap.values()) {
                List<Subscription> subscriptions = destination.getConsumers();
                LOG.info(destination + " subscriptions: " + subscriptions);
                for (Subscription subscription : subscriptions) {
                    if (subscription.getConsumerInfo().isNetworkSubscription()) {
                        LOG.info("subscription: " + subscription + ", priority: " + subscription.getConsumerInfo().getPriority());
                        assertTrue("priority is < our base: " + subscription.getConsumerInfo().getPriority(), subscription.getConsumerInfo().getPriority() <= BASE_PRIORITY);
                        found = true;
                    }
                }
            }
            return found;
        }
    }));
    producerThread.start();
    LOG.info("Started Producer");
    producerThread.join();
    consumerThread.join();
    int duplicateCount = 0;
    Map<String, String> map = new HashMap<>();
    for (String msg : consumer.getMessageStrings()) {
        if (map.containsKey(msg)) {
            LOG.info("got duplicate: " + msg);
            duplicateCount++;
        }
        map.put(msg, msg);
    }
    consumer.unSubscribe();
    if (suppressDuplicateTopicSubs || dispatchPolicy instanceof PriorityNetworkDispatchPolicy) {
        assertEquals("no duplicates", 0, duplicateCount);
        assertEquals("got all required messages: " + map.size(), consumer.getNumMessages(), map.size());
    } else {
        assertTrue("we can get some duplicates: " + duplicateCount, duplicateCount >= 0);
        if (duplicateCount == 0) {
            assertEquals("got all required messages: " + map.size(), consumer.getNumMessages(), map.size());
        }
    }
}
Also used : ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) Destination(org.apache.activemq.broker.region.Destination) RegionBroker(org.apache.activemq.broker.region.RegionBroker) HashMap(java.util.HashMap) JMSException(javax.jms.JMSException) CountDownLatch(java.util.concurrent.CountDownLatch) JMSException(javax.jms.JMSException) ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) PriorityNetworkDispatchPolicy(org.apache.activemq.broker.region.policy.PriorityNetworkDispatchPolicy) Subscription(org.apache.activemq.broker.region.Subscription)

Example 2 with PriorityNetworkDispatchPolicy

use of org.apache.activemq.broker.region.policy.PriorityNetworkDispatchPolicy in project activemq-artemis by apache.

the class NoDuplicateOnTopicNetworkTest method initCombosForTestProducerConsumerTopic.

public void initCombosForTestProducerConsumerTopic() {
    this.addCombinationValues("suppressDuplicateTopicSubs", new Object[] { Boolean.TRUE, Boolean.FALSE });
    this.addCombinationValues("dispatchPolicy", new Object[] { new PriorityNetworkDispatchPolicy(), new SimpleDispatchPolicy() });
    this.addCombinationValues("durableSub", new Object[] { Boolean.TRUE, Boolean.FALSE });
}
Also used : SimpleDispatchPolicy(org.apache.activemq.broker.region.policy.SimpleDispatchPolicy) PriorityNetworkDispatchPolicy(org.apache.activemq.broker.region.policy.PriorityNetworkDispatchPolicy)

Aggregations

PriorityNetworkDispatchPolicy (org.apache.activemq.broker.region.policy.PriorityNetworkDispatchPolicy)2 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 JMSException (javax.jms.JMSException)1 Destination (org.apache.activemq.broker.region.Destination)1 RegionBroker (org.apache.activemq.broker.region.RegionBroker)1 Subscription (org.apache.activemq.broker.region.Subscription)1 SimpleDispatchPolicy (org.apache.activemq.broker.region.policy.SimpleDispatchPolicy)1 ActiveMQDestination (org.apache.activemq.command.ActiveMQDestination)1