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.
This commit is contained in:
parent
4f6c39809a
commit
077087571f
|
@ -28,6 +28,7 @@ import javax.jms.Session;
|
||||||
import javax.jms.TextMessage;
|
import javax.jms.TextMessage;
|
||||||
import javax.naming.InitialContext;
|
import javax.naming.InitialContext;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple JMS example that shows how static message selectors work.
|
* 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 class StaticSelectorExample {
|
||||||
|
|
||||||
public static void main(final String[] args) throws Exception {
|
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;
|
Connection connection = null;
|
||||||
InitialContext initialContext = null;
|
InitialContext initialContext = null;
|
||||||
try {
|
try {
|
||||||
|
@ -63,7 +66,7 @@ public class StaticSelectorExample {
|
||||||
|
|
||||||
// Step 8. Create a JMS Message Consumer that receives 'red' messages
|
// Step 8. Create a JMS Message Consumer that receives 'red' messages
|
||||||
MessageConsumer redConsumer = session.createConsumer(queue);
|
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
|
// Step 9. Create five messages with different 'color' properties
|
||||||
TextMessage redMessage1 = session.createTextMessage("Red-1");
|
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.
|
// Step 11. Waiting for the message listener to check the received messages.
|
||||||
Thread.sleep(5000);
|
Thread.sleep(5000);
|
||||||
|
|
||||||
if (!result.get())
|
if (good.get() != 2 || bad.get() != 0 || failed.get())
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
} finally {
|
} finally {
|
||||||
// Step 12. Be sure to close our JMS resources!
|
// Step 12. Be sure to close our JMS resources!
|
||||||
|
@ -108,11 +111,15 @@ public class StaticSelectorExample {
|
||||||
class SimpleMessageListener implements MessageListener {
|
class SimpleMessageListener implements MessageListener {
|
||||||
|
|
||||||
private final String name;
|
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;
|
name = listener;
|
||||||
this.result = result;
|
this.failed = failed;
|
||||||
|
this.good = good;
|
||||||
|
this.bad = bad;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -125,12 +132,14 @@ class SimpleMessageListener implements MessageListener {
|
||||||
textMessage.getText() +
|
textMessage.getText() +
|
||||||
"] with color property: " +
|
"] with color property: " +
|
||||||
colorProp);
|
colorProp);
|
||||||
if (colorProp != null && !colorProp.equals(name)) {
|
if (colorProp != null && colorProp.equals(name)) {
|
||||||
result.set(false);
|
good.incrementAndGet();
|
||||||
|
} else {
|
||||||
|
bad.incrementAndGet();
|
||||||
}
|
}
|
||||||
} catch (JMSException e) {
|
} catch (JMSException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
result.set(false);
|
failed.set(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,11 +50,11 @@ under the License.
|
||||||
|
|
||||||
<addresses>
|
<addresses>
|
||||||
<address name="exampleQueue">
|
<address name="exampleQueue">
|
||||||
<multicast>
|
<anycast>
|
||||||
<queue name="exampleQueue">
|
<queue name="exampleQueue">
|
||||||
<filter string="color='red'"/>
|
<filter string="color='red'"/>
|
||||||
</queue>
|
</queue>
|
||||||
</multicast>
|
</anycast>
|
||||||
</address>
|
</address>
|
||||||
</addresses>
|
</addresses>
|
||||||
</core>
|
</core>
|
||||||
|
|
Loading…
Reference in New Issue