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());
}
}
}
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());
}
}
}
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());
}
}
}
}
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);
}
}
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);
}
Aggregations