How to add a Selector with MDB 3.0 ?

Message selectors allow an MDB to be more selective about the messages it receives from a particular topic or queue. Message selectors use Message properties as criteria in conditional expressions. These conditional expressions use Boolean logic to declare which messages should be delivered to a client.

With MDB 3.0 it’s easier to configure a selector : simply add an @ActivationConfigProperty at the top of your MDB:

@MessageDriven(activationConfig = {
  @ActivationConfigProperty(propertyName = "messageSelector",propertyValue = "color= 'red'")}

public class Mdbsample implements MessageListener {

Using an MDB Selector in XML descriptors

If you don’t want to include in your code the Selector info, you can also specify it in the ejb-jar.xml configuration file. See this example:



Setting a Message Selector programmatically

You can also set a Selector on your Producer by setting a String property as follows:

Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(true, 0);
Message message = session.createMessage();
message.setStringProperty("color", "yellow");
JMSProducer producer = context.createProducer();

producer.send(queue, message);

How to define a Selector at Queue level

Finally, it is worth mentioning that the messaging-activemq subsystem (available in WildFly / JBoss EAP 7) also allows to define a selector for your destination. To do that, set the filter attribute of your destination as shown here:

<subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0">
  filter="color='red'" />

You can set the filter when you are creating your Queue as in this example:

jms-queue add --queue-address=QUEUE_ADDRESS --selector=FILTER_EXPRESSION