Search in sources :

Example 1 with Subscription

use of org.apache.activemq.broker.region.Subscription 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 Subscription

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

the class PriorityNetworkDispatchPolicyTest method testRemoveLowerPriorityDup.

@Test
public void testRemoveLowerPriorityDup() throws Exception {
    List<Subscription> consumers = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        ConsumerInfo instance = info.copy();
        instance.setPriority((byte) i);
        consumers.add(new TopicSubscription(brokerService.getBroker(), context, instance, usageManager));
    }
    underTest.dispatch(node, null, consumers);
    long count = 0;
    for (Subscription consumer : consumers) {
        count += consumer.getEnqueueCounter();
    }
    assertEquals("only one sub got message", 1, count);
}
Also used : ConsumerInfo(org.apache.activemq.command.ConsumerInfo) TopicSubscription(org.apache.activemq.broker.region.TopicSubscription) ArrayList(java.util.ArrayList) Subscription(org.apache.activemq.broker.region.Subscription) TopicSubscription(org.apache.activemq.broker.region.TopicSubscription) Test(org.junit.Test)

Example 3 with Subscription

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

the class DurableSubscriptionUnsubscribeTest method assertCount.

private void assertCount(int all, int active) throws Exception {
    int inactive = all - active;
    // broker check
    Destination destination = broker.getDestination(topic);
    List<Subscription> subs = destination.getConsumers();
    int cActive = 0, cInactive = 0;
    for (Subscription sub : subs) {
        if (sub instanceof DurableTopicSubscription) {
            DurableTopicSubscription durable = (DurableTopicSubscription) sub;
            if (durable.isActive())
                cActive++;
            else
                cInactive++;
        }
    }
    assertEquals(active, cActive);
    assertEquals(inactive, cInactive);
    // admin view
    ObjectName[] subscriptions = broker.getAdminView().getDurableTopicSubscribers();
    assertEquals(active, subscriptions.length);
    subscriptions = broker.getAdminView().getInactiveDurableTopicSubscribers();
    assertEquals(inactive, subscriptions.length);
    // check the strange false MBean
    if (all == 0)
        assertEquals(0, countMBean());
    // check if we got all advisories
    assertEquals(100, all + advisories.get());
}
Also used : Destination(org.apache.activemq.broker.region.Destination) DurableTopicSubscription(org.apache.activemq.broker.region.DurableTopicSubscription) Subscription(org.apache.activemq.broker.region.Subscription) DurableTopicSubscription(org.apache.activemq.broker.region.DurableTopicSubscription) ObjectName(javax.management.ObjectName)

Example 4 with Subscription

use of org.apache.activemq.broker.region.Subscription in project gocd by gocd.

the class ActiveMqMessagingService method removeQueue.

@Override
public void removeQueue(String queueName) {
    try {
        ActiveMQQueue destination = new ActiveMQQueue(queueName);
        ConnectionContext connectionContext = BrokerSupport.getConnectionContext(broker.getBroker());
        Destination brokerDestination = broker.getDestination(destination);
        List<Subscription> consumers = brokerDestination.getConsumers();
        for (Subscription consumer : consumers) {
            consumer.remove(connectionContext, brokerDestination);
            brokerDestination.removeSubscription(connectionContext, consumer, 0);
        }
        broker.getBroker().removeDestination(connectionContext, destination, 1000);
        broker.removeDestination(destination);
    } catch (Exception e) {
        throw bomb(e);
    }
}
Also used : Destination(org.apache.activemq.broker.region.Destination) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) ConnectionContext(org.apache.activemq.broker.ConnectionContext) Subscription(org.apache.activemq.broker.region.Subscription)

Example 5 with Subscription

use of org.apache.activemq.broker.region.Subscription in project ignite by apache.

the class IgniteJmsStreamerTest method testQueueMultipleThreads.

/**
 * @throws Exception If failed.
 */
public void testQueueMultipleThreads() throws Exception {
    Destination dest = new ActiveMQQueue(QUEUE_NAME);
    // produce messages into the queue
    produceObjectMessages(dest, false);
    try (IgniteDataStreamer<String, String> dataStreamer = grid().dataStreamer(DEFAULT_CACHE_NAME)) {
        JmsStreamer<ObjectMessage, String, String> jmsStreamer = newJmsStreamer(ObjectMessage.class, dataStreamer);
        jmsStreamer.setDestination(dest);
        jmsStreamer.setThreads(5);
        // subscribe to cache PUT events and return a countdown latch starting at CACHE_ENTRY_COUNT
        CountDownLatch latch = subscribeToPutEvents(CACHE_ENTRY_COUNT);
        // start the streamer
        jmsStreamer.start();
        DestinationStatistics qStats = broker.getBroker().getDestinationMap().get(dest).getDestinationStatistics();
        assertEquals(5, qStats.getConsumers().getCount());
        // all cache PUT events received in 10 seconds
        latch.await(10, TimeUnit.SECONDS);
        // assert that all consumers received messages - given that the prefetch is 1
        for (Subscription subscription : broker.getBroker().getDestinationMap().get(dest).getConsumers()) assertTrue(subscription.getDequeueCounter() > 0);
        assertAllCacheEntriesLoaded();
        jmsStreamer.stop();
    }
}
Also used : Destination(javax.jms.Destination) DestinationStatistics(org.apache.activemq.broker.region.DestinationStatistics) ObjectMessage(javax.jms.ObjectMessage) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(org.apache.activemq.broker.region.Subscription)

Aggregations

Subscription (org.apache.activemq.broker.region.Subscription)6 Destination (org.apache.activemq.broker.region.Destination)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 RegionBroker (org.apache.activemq.broker.region.RegionBroker)2 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Destination (javax.jms.Destination)1 JMSException (javax.jms.JMSException)1 ObjectMessage (javax.jms.ObjectMessage)1 ObjectName (javax.management.ObjectName)1 ConnectionContext (org.apache.activemq.broker.ConnectionContext)1 DestinationStatistics (org.apache.activemq.broker.region.DestinationStatistics)1 DurableTopicSubscription (org.apache.activemq.broker.region.DurableTopicSubscription)1 Queue (org.apache.activemq.broker.region.Queue)1 TopicSubscription (org.apache.activemq.broker.region.TopicSubscription)1 PriorityNetworkDispatchPolicy (org.apache.activemq.broker.region.policy.PriorityNetworkDispatchPolicy)1 ActiveMQDestination (org.apache.activemq.command.ActiveMQDestination)1 ConsumerInfo (org.apache.activemq.command.ConsumerInfo)1 Test (org.junit.Test)1