Sometimes there is a need for message-driven beans to receive only the messages that they are intended to process. To selectively deliver messages to a message-driven bean, the bean should be deployed with a message selector. The bean source needs no changes in order to use the message selector. This tip shows a simple message-driven bean that only wants messages that contain an attribute UserRole set to 'admin'. The bean prints out the role of the incoming message for verification. In the XML descriptor for the bean, we describe the message selector that filters undesired messages for the message-driven bean.

Here we show the partial XML descriptor that describes the simple EJB and its message selector:

A sample MDB code:

public class SimpleMDB implements MessageDrivenBean, MessageListener {

    public void onMessage(Message message) {
        MapMessage map = (MapMessagemessage;
        try {
            String role = map.getString("UserRole");
            System.out.println("Received Message for Role: " + role);
            // the process the message:
        catch (Exception e) {

The filter statement in descriptor:

			<!-- statement filtering incoming messages -->
				<![CDATA[ UserRole = 'admin' ]]>