org.apache.activemq.broker.region.RegionBroker

Here are the examples of the java api class org.apache.activemq.broker.region.RegionBroker taken from open source projects.

1. NetworkOfTwentyBrokersTest#verifyPeerBrokerInfo()

Project: activemq-artemis
File: NetworkOfTwentyBrokersTest.java
private void verifyPeerBrokerInfo(BrokerItem brokerItem, final int max) throws Exception {
    final BrokerService broker = brokerItem.broker;
    final RegionBroker regionBroker = (RegionBroker) broker.getRegionBroker();
    Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            LOG.info("verify infos " + broker.getBrokerName() + ", len: " + regionBroker.getPeerBrokerInfos().length);
            return max == regionBroker.getPeerBrokerInfos().length;
        }
    }, 120 * 1000);
    LOG.info("verify infos " + broker.getBrokerName() + ", len: " + regionBroker.getPeerBrokerInfos().length);
    for (BrokerInfo info : regionBroker.getPeerBrokerInfos()) {
        LOG.info(info.getBrokerName());
    }
    assertEquals(broker.getBrokerName(), max, regionBroker.getPeerBrokerInfos().length);
}

2. ThreeBrokerQueueNetworkTest#verifyConsumerCount()

Project: activemq-artemis
File: ThreeBrokerQueueNetworkTest.java
private void verifyConsumerCount(BrokerService broker, int count, final Destination dest) throws Exception {
    final RegionBroker regionBroker = (RegionBroker) broker.getRegionBroker();
    waitFor(new Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return !regionBroker.getDestinations(ActiveMQDestination.transform(dest)).isEmpty();
        }
    });
    Queue internalQueue = (Queue) regionBroker.getDestinations(ActiveMQDestination.transform(dest)).iterator().next();
    LOG.info("Verify: consumer count on " + broker.getBrokerName() + " matches:" + count + ", val:" + internalQueue.getConsumers().size());
    assertEquals("consumer count on " + broker.getBrokerName() + " matches for q: " + internalQueue, count, internalQueue.getConsumers().size());
}

3. DeadLetterTestSupport#validateConsumerPrefetch()

Project: activemq-artemis
File: DeadLetterTestSupport.java
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());
        }
    }
}

4. TempDestLoadTest#testLoadTempAdvisoryTopics()

Project: activemq-artemis
File: TempDestLoadTest.java
public void testLoadTempAdvisoryTopics() throws Exception {
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        TemporaryTopic tempTopic = session.createTemporaryTopic();
        MessageConsumer consumer = session.createConsumer(tempTopic);
        MessageProducer producer = session.createProducer(tempTopic);
        consumer.close();
        producer.close();
        tempTopic.delete();
    }
    AdvisoryBroker ab = (AdvisoryBroker) broker.getBroker().getAdaptor(AdvisoryBroker.class);
    assertTrue(ab.getAdvisoryDestinations().size() == 0);
    assertTrue(ab.getAdvisoryConsumers().size() == 0);
    assertTrue(ab.getAdvisoryProducers().size() == 0);
    RegionBroker rb = (RegionBroker) broker.getBroker().getAdaptor(RegionBroker.class);
    for (Destination dest : rb.getDestinationMap().values()) {
        LOG.debug("Destination: {}", dest);
    }
    // there should be at least 2 destinations - advisories -
    // 1 for the connection + 1 generic ones
    assertTrue("Should be at least 2 destinations", rb.getDestinationMap().size() > 2);
}

5. ThreeBrokerTempDestDemandSubscriptionCleanupTest#testSubscriptionsCleanedUpAfterConnectionClose()

Project: activemq-artemis
File: ThreeBrokerTempDestDemandSubscriptionCleanupTest.java
/**
    * This test is slightly different from the above. We don't explicitly close the consumer down
    * (which we did in the previous test to force the RemoveInfo to be sent). Here we just close
    * the connection which should still clean up the subscriptions and temp destinations on the
    * networked brokers.
    *
    * @throws Exception
    */
public void testSubscriptionsCleanedUpAfterConnectionClose() throws Exception {
    final BrokerItem brokerA = brokers.get(BROKER_A);
    for (int i = 0; i < NUM_ITER; i++) {
        Connection conn = null;
        try {
            conn = brokerA.createConnection();
            conn.start();
            final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = sess.createQueue(ECHO_QUEUE_NAME);
            MessageProducer producer = sess.createProducer(destination);
            LOG.info("Starting iter: " + i);
            Destination replyTo = sess.createTemporaryQueue();
            MessageConsumer responseConsumer = sess.createConsumer(replyTo);
            Message message = sess.createTextMessage("Iteration: " + i);
            message.setJMSReplyTo(replyTo);
            producer.send(message);
            TextMessage response = (TextMessage) responseConsumer.receive(CONSUME_TIMEOUT);
            assertNotNull("We should have gotten a response, but didn't for iter: " + i, response);
            assertEquals("We got the wrong response from the echo service", "Iteration: " + i, response.getText());
            // so closing the connection without closing the consumer first will leak subscriptions
            // in a nob?
            //              responseConsumer.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
            fail();
        }
    }
    // for the real test... we should not have any subscriptions left on broker C for the temp dests
    BrokerItem brokerC = brokers.get(BROKER_C);
    RegionBroker regionBroker = (RegionBroker) brokerC.broker.getRegionBroker();
    final AbstractRegion region = (AbstractRegion) regionBroker.getTempQueueRegion();
    assertTrue("There were no lingering temp-queue destinations", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            LOG.info("Lingering temps: " + region.getSubscriptions().size());
            return 0 == region.getSubscriptions().size();
        }
    }));
}

6. ThreeBrokerTempDestDemandSubscriptionCleanupTest#testSubscriptionsCleanedUpRace()

Project: activemq-artemis
File: ThreeBrokerTempDestDemandSubscriptionCleanupTest.java
/**
    * So we network three brokers together, and send a message with request-reply semantics.
    * The message goes to an echo service listening on broker C. We send a message on a queue
    * to broker A which gets demand forwarded to broker C. the echo service will respond to the
    * temp destination listed in the JMSReplyTo header. that will get demand forwarded back to
    * broker A. When the consumer of the temp dest on broker A closes, that subscription should
    * be removed on broker A. advisories firing from broker A to broker B should remove that
    * subscription on broker B. advisories firing from broker B to broker C should remove that
    * subscription on broker C.
    *
    * @throws Exception
    */
public void testSubscriptionsCleanedUpRace() throws Exception {
    final BrokerItem brokerA = brokers.get(BROKER_A);
    Runnable tester = new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < NUM_ITER; i++) {
                Connection conn = null;
                try {
                    conn = brokerA.createConnection();
                    conn.start();
                    final Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
                    Destination destination = sess.createQueue(ECHO_QUEUE_NAME);
                    MessageProducer producer = sess.createProducer(destination);
                    LOG.info("Starting iter: " + i);
                    Destination replyTo = sess.createTemporaryQueue();
                    MessageConsumer responseConsumer = sess.createConsumer(replyTo);
                    Message message = sess.createTextMessage("Iteration: " + i);
                    message.setJMSReplyTo(replyTo);
                    producer.send(message);
                    TextMessage response = (TextMessage) responseConsumer.receive(CONSUME_TIMEOUT);
                    assertNotNull("We should have gotten a response, but didn't for iter: " + i, response);
                    assertEquals("We got the wrong response from the echo service", "Iteration: " + i, response.getText());
                    // so we close the consumer so that an actual RemoveInfo command gets propogated through the
                    // network
                    responseConsumer.close();
                    conn.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    fail();
                }
            }
        }
    };
    ExecutorService threadService = Executors.newFixedThreadPool(2);
    threadService.submit(tester);
    threadService.submit(tester);
    threadService.shutdown();
    assertTrue("executor done on time", threadService.awaitTermination(30L, TimeUnit.SECONDS));
    // for the real test... we should not have any subscriptions left on broker C for the temp dests
    BrokerItem brokerC = brokers.get(BROKER_C);
    RegionBroker regionBroker = (RegionBroker) brokerC.broker.getRegionBroker();
    final AbstractRegion region = (AbstractRegion) regionBroker.getTempQueueRegion();
    assertTrue("There were no lingering temp-queue destinations", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            LOG.info("Lingering temps: " + region.getSubscriptions().size());
            return 0 == region.getSubscriptions().size();
        }
    }));
}

7. ThreeBrokerQueueNetworkTest#verifyConsumePriority()

Project: activemq-artemis
File: ThreeBrokerQueueNetworkTest.java
private void verifyConsumePriority(BrokerService broker, byte expectedPriority, Destination dest) throws Exception {
    RegionBroker regionBroker = (RegionBroker) broker.getRegionBroker();
    Queue internalQueue = (Queue) regionBroker.getDestinations(ActiveMQDestination.transform(dest)).iterator().next();
    for (Subscription consumer : internalQueue.getConsumers()) {
        assertEquals("consumer on " + broker.getBrokerName() + " matches priority: " + internalQueue, expectedPriority, consumer.getConsumerInfo().getPriority());
    }
}

8. ThreeBrokerQueueNetworkTest#logConsumerCount()

Project: activemq-artemis
File: ThreeBrokerQueueNetworkTest.java
private void logConsumerCount(BrokerService broker, int count, final Destination dest) throws Exception {
    final RegionBroker regionBroker = (RegionBroker) broker.getRegionBroker();
    waitFor(new Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return !regionBroker.getDestinations(ActiveMQDestination.transform(dest)).isEmpty();
        }
    });
    Queue internalQueue = (Queue) regionBroker.getDestinations(ActiveMQDestination.transform(dest)).iterator().next();
    LOG.info("Verify: consumer count on " + broker.getBrokerName() + " matches:" + count + ", val:" + internalQueue.getConsumers().size());
}

9. QueueMemoryFullMultiBrokersTest#testQueueNetworkWithConsumerFull()

Project: activemq-artemis
File: QueueMemoryFullMultiBrokersTest.java
public void testQueueNetworkWithConsumerFull() throws Exception {
    bridgeAllBrokers();
    startAllBrokers();
    Destination dest = createDestination("TEST.FOO", false);
    sendMessages("Broker1", dest, 50);
    CountDownLatch latch = new CountDownLatch(MESSAGE_COUNT);
    createConsumer("Broker2", dest, latch);
    assertConsumersConnect("Broker1", dest, 1, 30000);
    sendMessages("Broker1", dest, MESSAGE_COUNT - 50);
    // Wait for messages to be delivered
    assertTrue("Missing " + latch.getCount() + " messages", latch.await(45, TimeUnit.SECONDS));
    // verify stats, all messages acked
    BrokerService broker1 = brokers.get("Broker1").broker;
    RegionBroker regionBroker = (RegionBroker) broker1.getRegionBroker();
    // give the acks a chance to flow
    Thread.sleep(2000);
    Queue internalQueue = (Queue) regionBroker.getDestinations(ActiveMQDestination.transform(dest)).iterator().next();
    assertTrue("All messages are consumed and acked from source:" + internalQueue, internalQueue.getMessages().isEmpty());
    assertEquals("messages source:" + internalQueue, 0, internalQueue.getDestinationStatistics().getMessages().getCount());
    assertEquals("inflight source:" + internalQueue, 0, internalQueue.getDestinationStatistics().getInflight().getCount());
}

10. NoDuplicateOnTopicNetworkTest#testProducerConsumerTopic()

Project: activemq-artemis
File: NoDuplicateOnTopicNetworkTest.java
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());
        }
    }
}

11. ConsumeTopicPrefetchTest#doValidateConsumerPrefetch()

Project: activemq-artemis
File: ConsumeTopicPrefetchTest.java
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());
            }
        }
    }
}

12. TestSupport#getDestinationMap()

Project: activemq-artemis
File: TestSupport.java
private static Map<ActiveMQDestination, org.apache.activemq.broker.region.Destination> getDestinationMap(BrokerService target, ActiveMQDestination destination) {
    RegionBroker regionBroker = (RegionBroker) target.getRegionBroker();
    if (destination.isTemporary()) {
        return destination.isQueue() ? regionBroker.getTempQueueRegion().getDestinationMap() : regionBroker.getTempTopicRegion().getDestinationMap();
    }
    return destination.isQueue() ? regionBroker.getQueueRegion().getDestinationMap() : regionBroker.getTopicRegion().getDestinationMap();
}

13. TempQueueMemoryTest#testLoadRequestReply()

Project: activemq-artemis
File: TempQueueMemoryTest.java
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);
}

14. TempDestLoadTest#testLoadTempAdvisoryQueues()

Project: activemq-artemis
File: TempDestLoadTest.java
public void testLoadTempAdvisoryQueues() throws Exception {
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        TemporaryQueue tempQueue = session.createTemporaryQueue();
        MessageConsumer consumer = session.createConsumer(tempQueue);
        MessageProducer producer = session.createProducer(tempQueue);
        consumer.close();
        producer.close();
        tempQueue.delete();
    }
    AdvisoryBroker ab = (AdvisoryBroker) broker.getBroker().getAdaptor(AdvisoryBroker.class);
    assertTrue(ab.getAdvisoryDestinations().size() == 0);
    assertTrue(ab.getAdvisoryConsumers().size() == 0);
    assertTrue(ab.getAdvisoryProducers().size() == 0);
    RegionBroker rb = (RegionBroker) broker.getBroker().getAdaptor(RegionBroker.class);
    for (Destination dest : rb.getDestinationMap().values()) {
        LOG.debug("Destination: {}", dest);
    }
    // there should be at least 2 destinations - advisories -
    // 1 for the connection + 1 generic ones
    assertTrue("Should be at least 2 destinations", rb.getDestinationMap().size() > 2);
}

15. TempDestDeleteTest#destinationExists()

Project: activemq-artemis
File: TempDestDeleteTest.java
private boolean destinationExists(Destination dest) throws Exception {
    RegionBroker rb = (RegionBroker) broker.getBroker().getAdaptor(RegionBroker.class);
    return rb.getTopicRegion().getDestinationMap().containsKey(dest) || rb.getQueueRegion().getDestinationMap().containsKey(dest) || rb.getTempTopicRegion().getDestinationMap().containsKey(dest) || rb.getTempQueueRegion().getDestinationMap().containsKey(dest);
}