This example shows you how to configure a JBoss Messaging queue with static message selectors (filters) (to configure a static selector directly on a JMS queue, please see the static-selector-jms example).
Static message selectors are JBoss Messaging's extension to message selectors as defined in JMS spec 1.1. Rather than specifying the selector in the application code, static message selectors are defined in one of JBoss Messaging's configuration files, jbm-configuration.xml, as an element called 'filter' inside each queue definition, like
<queues>
<queue name="jms.queue.selectorQueue">
<address>jms.queue.selectorQueue</address>
<filter string="color='red'"/>
</queue>
</queues>
Once thus configured, as this example does, the queue 'selectorQueue' only delivers messages that are selected against the filter, i.e., only the messages whose 'color' properties are of 'red' values can be received by its consumers. Those that don't match the filter will be dropped by queue and therefore will never be delivered to any of its consumers.
In the example code, five messages with different 'color' property values are sent to queue 'selectorQueue'. One consumer is created to receive messages from the queue. Of the five sent messages, two are of 'red' color properties, one is 'blue', one is 'green' and one has not the 'color' propery at all. The result is that the consumer only gets the two 'red' messages.
To run the example, simply type ant
from this directory
client-jndi.properties
file in the directory ../common/config
InitialContext initialContext = getContext();
Queue queue = (Queue) initialContext.lookup("/queue/selectorQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
MessageConsumer redConsumer = session.createConsumer(queue);
redConsumer.setMessageListener(new SimpleMessageListener("red"));
TextMessage redMessage1 = session.createTextMessage("Red-1");
redMessage1.setStringProperty("color", "red");
TextMessage redMessage2 = session.createTextMessage("Red-2");
redMessage2.setStringProperty("color", "red");
TextMessage greenMessage = session.createTextMessage("Green");
greenMessage.setStringProperty("color", "green");
TextMessage blueMessage = session.createTextMessage("Blue");
blueMessage.setStringProperty("color", "blue");
TextMessage normalMessage = session.createTextMessage("No color");
producer.send(redMessage1);
System.out.println("Message sent: " + redMessage1.getText());
producer.send(greenMessage);
System.out.println("Message sent: " + greenMessage.getText());
producer.send(blueMessage);
System.out.println("Message sent: " + blueMessage.getText());
producer.send(redMessage2);
System.out.println("Message sent: " + redMessage2.getText());
producer.send(normalMessage);
System.out.println("Message sent: " + normalMessage.getText());
Thread.sleep(5000);
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}