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:

<ejb-jar>
    <display-name>DemoMDB</display-name> 
    <enterprise-beans>
        <message-driven>
            <display-name>testMdb</display-name>
            <ejb-name>DemoMDB</ejb-name>
            <ejb-class>DemoMDB</ejb-class>
            <transaction-type>Container</transaction-type>
            <message-selector>RECIPIENT='MDB'</message-selector>
            <message-driven-destination>
                <destination-type>javax.jms.Queue</destination-type>
            </message-driven-destination> 
                <resource-ref>
                <description>description</description>
                <res-ref-name>jms/myQueueConnectionFactory</res-ref-name>
                <res-type>javax.jms.QueueConnectionFactory</res-type>
                <res-auth>Application</res-auth>
                <res-sharing-scope>Shareable</res-sharing-scope>
            </resource-ref> 
            <resource-env-ref>
               <resource-env-ref-name>jms/persistentQueue</resource-env-ref-name>
             <resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
            </resource-env-ref> 
        </message-driven>
    </enterprise-beans>

    <assembly-descriptor>
        <container-transaction>
            <method>
                <ejb-name>DemoMDB</ejb-name>
                <method-name>onMessage</method-name>
                <method-params>
                    <method-param>javax.jms.Message</method-param>
                </method-params>
            </method>
            <trans-attribute>Required</trans-attribute>
        </container-transaction>
    </assembly-descriptor> 
</ejb-jar>

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">
...
<queue
  name="myQueue"
  filter="color='red'" />
...
</subsystem>

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