From 077087571f2ad66848302181ab951d14c65d9a93 Mon Sep 17 00:00:00 2001 From: Justin Bertram Date: Mon, 8 Feb 2021 20:20:05 -0600 Subject: [PATCH] ARTEMIS-2943 fix static selector example The static-selector example was using a multicast queue instead of an anycast queue which meant that the consumer never actually received any of the messages. Furthermore, it wasn't actually verifying that it received the proper messages so there was no failure. This commit resolves these issues. --- .../jms/example/StaticSelectorExample.java | 27 ++++++++++++------- .../resources/activemq/server0/broker.xml | 4 +-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/examples/features/standard/static-selector/src/main/java/org/apache/activemq/artemis/jms/example/StaticSelectorExample.java b/examples/features/standard/static-selector/src/main/java/org/apache/activemq/artemis/jms/example/StaticSelectorExample.java index d930890d41..52bd0eb74f 100644 --- a/examples/features/standard/static-selector/src/main/java/org/apache/activemq/artemis/jms/example/StaticSelectorExample.java +++ b/examples/features/standard/static-selector/src/main/java/org/apache/activemq/artemis/jms/example/StaticSelectorExample.java @@ -28,6 +28,7 @@ import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.InitialContext; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; /** * A simple JMS example that shows how static message selectors work. @@ -35,7 +36,9 @@ import java.util.concurrent.atomic.AtomicBoolean; public class StaticSelectorExample { public static void main(final String[] args) throws Exception { - AtomicBoolean result = new AtomicBoolean(true); + AtomicInteger good = new AtomicInteger(0); + AtomicInteger bad = new AtomicInteger(0); + AtomicBoolean failed = new AtomicBoolean(false); Connection connection = null; InitialContext initialContext = null; try { @@ -63,7 +66,7 @@ public class StaticSelectorExample { // Step 8. Create a JMS Message Consumer that receives 'red' messages MessageConsumer redConsumer = session.createConsumer(queue); - redConsumer.setMessageListener(new SimpleMessageListener("red", result)); + redConsumer.setMessageListener(new SimpleMessageListener("red", failed, good, bad)); // Step 9. Create five messages with different 'color' properties TextMessage redMessage1 = session.createTextMessage("Red-1"); @@ -91,7 +94,7 @@ public class StaticSelectorExample { // Step 11. Waiting for the message listener to check the received messages. Thread.sleep(5000); - if (!result.get()) + if (good.get() != 2 || bad.get() != 0 || failed.get()) throw new IllegalStateException(); } finally { // Step 12. Be sure to close our JMS resources! @@ -108,11 +111,15 @@ public class StaticSelectorExample { class SimpleMessageListener implements MessageListener { private final String name; - private AtomicBoolean result; + private AtomicInteger good; + private AtomicInteger bad; + private AtomicBoolean failed; - SimpleMessageListener(final String listener, AtomicBoolean result) { + SimpleMessageListener(final String listener, AtomicBoolean failed, AtomicInteger good, AtomicInteger bad) { name = listener; - this.result = result; + this.failed = failed; + this.good = good; + this.bad = bad; } @Override @@ -125,12 +132,14 @@ class SimpleMessageListener implements MessageListener { textMessage.getText() + "] with color property: " + colorProp); - if (colorProp != null && !colorProp.equals(name)) { - result.set(false); + if (colorProp != null && colorProp.equals(name)) { + good.incrementAndGet(); + } else { + bad.incrementAndGet(); } } catch (JMSException e) { e.printStackTrace(); - result.set(false); + failed.set(true); } } } diff --git a/examples/features/standard/static-selector/src/main/resources/activemq/server0/broker.xml b/examples/features/standard/static-selector/src/main/resources/activemq/server0/broker.xml index 066b92fd58..31a9a93889 100644 --- a/examples/features/standard/static-selector/src/main/resources/activemq/server0/broker.xml +++ b/examples/features/standard/static-selector/src/main/resources/activemq/server0/broker.xml @@ -50,11 +50,11 @@ under the License.
- + - +