Search in sources :

Example 6 with RegionBroker

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

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

the class DeadLetterTestSupport method validateConsumerPrefetch.

private void validateConsumerPrefetch(String destination, long expectedCount) {
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
    }
    RegionBroker regionBroker = (RegionBroker) broker.getRegionBroker();
    for (org.apache.activemq.broker.region.Destination dest : regionBroker.getQueueRegion().getDestinationMap().values()) {
        if (dest.getName().equals(destination)) {
            DestinationStatistics stats = dest.getDestinationStatistics();
            LOG.info(">>>> inflight for : " + dest.getName() + ": " + stats.getInflight().getCount());
            assertEquals("inflight for: " + dest.getName() + ": " + stats.getInflight().getCount() + " matches", expectedCount, stats.getInflight().getCount());
        }
    }
}
Also used : DestinationStatistics(org.apache.activemq.broker.region.DestinationStatistics) RegionBroker(org.apache.activemq.broker.region.RegionBroker)

Example 8 with RegionBroker

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

the class ConsumeTopicPrefetchTest method doValidateConsumerPrefetch.

protected void doValidateConsumerPrefetch(String destination, final long expectedCount, final boolean greaterOrEqual) throws JMSException {
    RegionBroker regionBroker = (RegionBroker) BrokerRegistry.getInstance().lookup("localhost").getRegionBroker();
    for (org.apache.activemq.broker.region.Destination dest : regionBroker.getTopicRegion().getDestinationMap().values()) {
        final org.apache.activemq.broker.region.Destination target = dest;
        if (dest.getName().equals(destination)) {
            try {
                Wait.waitFor(new Condition() {

                    @Override
                    public boolean isSatisified() throws Exception {
                        DestinationStatistics stats = target.getDestinationStatistics();
                        LOG.info("inflight for : " + target.getName() + ": " + stats.getInflight().getCount());
                        if (greaterOrEqual) {
                            return stats.getInflight().getCount() >= expectedCount;
                        } else {
                            return stats.getInflight().getCount() == expectedCount;
                        }
                    }
                });
            } catch (Exception e) {
                throw new JMSException(e.toString());
            }
            DestinationStatistics stats = dest.getDestinationStatistics();
            LOG.info("inflight for : " + dest.getName() + ": " + stats.getInflight().getCount());
            if (greaterOrEqual) {
                assertTrue("inflight for: " + dest.getName() + ": " + stats.getInflight().getCount() + " > " + stats.getInflight().getCount(), stats.getInflight().getCount() >= expectedCount);
            } else {
                assertEquals("inflight for: " + dest.getName() + ": " + stats.getInflight().getCount() + " matches", expectedCount, stats.getInflight().getCount());
            }
        }
    }
}
Also used : Condition(org.apache.activemq.util.Wait.Condition) DestinationStatistics(org.apache.activemq.broker.region.DestinationStatistics) RegionBroker(org.apache.activemq.broker.region.RegionBroker) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException)

Example 9 with RegionBroker

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

the class RegionBrokerProxy method newRegionBroker.

public static RegionBroker newRegionBroker(ArtemisBrokerWrapper broker) {
    Broker brokerProxy = null;
    try {
        brokerProxy = new RegionBrokerProxy(broker);
        RegionBroker regionBroker = Mockito.mock(RegionBroker.class, delegatesTo(brokerProxy));
        return regionBroker;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Broker(org.apache.activemq.broker.Broker) RegionBroker(org.apache.activemq.broker.region.RegionBroker) RegionBroker(org.apache.activemq.broker.region.RegionBroker)

Example 10 with RegionBroker

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

the class TempQueueMemoryTest method testLoadRequestReply.

public void testLoadRequestReply() throws Exception {
    for (int i = 0; i < numConsumers; i++) {
        serverSession.createConsumer(serverDestination).setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message msg) {
                try {
                    Destination replyTo = msg.getJMSReplyTo();
                    MessageProducer producer = serverSession.createProducer(replyTo);
                    producer.send(replyTo, msg);
                    if (serverTransactional) {
                        serverSession.commit();
                    }
                    producer.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    class Producer extends Thread {

        private final int numToSend;

        public Producer(int numToSend) {
            this.numToSend = numToSend;
        }

        @Override
        public void run() {
            try {
                Session session = clientConnection.createSession(clientTransactional, clientTransactional ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(serverDestination);
                for (int i = 0; i < numToSend; i++) {
                    TemporaryQueue replyTo = session.createTemporaryQueue();
                    MessageConsumer consumer = session.createConsumer(replyTo);
                    Message msg = session.createMessage();
                    msg.setJMSReplyTo(replyTo);
                    producer.send(msg);
                    if (clientTransactional) {
                        session.commit();
                    }
                    consumer.receive();
                    if (clientTransactional) {
                        session.commit();
                    }
                    consumer.close();
                    if (deleteTempQueue) {
                        replyTo.delete();
                    } else {
                    // temp queue will be cleaned up on clientConnection.close
                    }
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
    Vector<Thread> threads = new Vector<>(numProducers);
    for (int i = 0; i < numProducers; i++) {
        threads.add(new Producer(messagesToSend / numProducers));
    }
    startAndJoinThreads(threads);
    clientSession.close();
    serverSession.close();
    clientConnection.close();
    serverConnection.close();
    AdvisoryBroker ab = (AdvisoryBroker) broker.getBroker().getAdaptor(AdvisoryBroker.class);
    // The server destination will be left
    assertTrue(ab.getAdvisoryDestinations().size() == 1);
    assertTrue("should be zero but is " + ab.getAdvisoryConsumers().size(), ab.getAdvisoryConsumers().size() == 0);
    assertTrue("should be zero but is " + ab.getAdvisoryProducers().size(), ab.getAdvisoryProducers().size() == 0);
    RegionBroker rb = (RegionBroker) broker.getBroker().getAdaptor(RegionBroker.class);
    assertTrue(rb.getDestinationMap().size() >= 6);
}
Also used : ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) RegionBroker(org.apache.activemq.broker.region.RegionBroker) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Vector(java.util.Vector) Session(javax.jms.Session)

Aggregations

RegionBroker (org.apache.activemq.broker.region.RegionBroker)15 MessageConsumer (javax.jms.MessageConsumer)6 MessageProducer (javax.jms.MessageProducer)6 JMSException (javax.jms.JMSException)5 Destination (javax.jms.Destination)4 Queue (org.apache.activemq.broker.region.Queue)4 Message (javax.jms.Message)3 Session (javax.jms.Session)3 TextMessage (javax.jms.TextMessage)3 BrokerService (org.apache.activemq.broker.BrokerService)3 Destination (org.apache.activemq.broker.region.Destination)3 ActiveMQDestination (org.apache.activemq.command.ActiveMQDestination)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 Connection (javax.jms.Connection)2 TemporaryQueue (javax.jms.TemporaryQueue)2 AbstractRegion (org.apache.activemq.broker.region.AbstractRegion)2 DestinationStatistics (org.apache.activemq.broker.region.DestinationStatistics)2 Subscription (org.apache.activemq.broker.region.Subscription)2 HashMap (java.util.HashMap)1 Vector (java.util.Vector)1