Filtering messages using selectors

You can use selectors to specify which messages you want delivered to a message queue. Once you add a selector to a queue, the message service delivers only those messages whose message topic matches the selector. You can create message selectors using EAServer Manager—see Chapter 8, “Setting up the Message Service,” in the EAServer System Administration Guide. You can also create message selectors programmatically. This example illustrates how to create a message selector and use it when you are creating a new QueueReceiver:

// Create a selector to receive only Text messages
// whose value property equals 100.

String selector = “value = 100 and Type = TextMessage”;

// Create a queue receiver using the selector.

QueueReceiver receiver =
   queueSession.createReceiver(queue, selector);

This code sample sends two messages to the message queue we just created. The properties of the first message match those of the message queue’s selector. The properties of the second message do not.

// Create and send a message whose properties match 
// the message queue selector.

TextMessage textMsg =
   queueSession.createTextMessage(“Text Message”);
textMsg.setIntProperty(“Value”, 100);
textMsg.setStringProperty(“Type”, “TextMessage”);
sender.send(textMsg);

// Create and send a Bytes message, whose value 
// property equals 200.

BytesMessage bytesMsg =
   queueSession.createBytesMessage();
bytesMsg.setIntProperty(“Value”, 200);
bytesMsg.setStringProperty(“Type”, “BytesMessage”);
sender.send(bytesMsg);

When we retrieve messages from the message queue, the Text message will be returned but the Bytes message will not.