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 define message selectors in the Ant configuration file that defines the component—see “Message selectors”. You can also create message selectors programmatically. The following example illustrates how to create a message selector and use it when you are creating a new MessageConsumer:

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

String mySelector = “value = 100 and Type = ‘TextMessage’”;

// Create a MessageConsumer for a queue using mySelector.

MessageConsumer receiver = mySession.createConsumer(myQueue, mySelector);

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 = mySession.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 = mySession.createBytesMessage();
bytesMsg.setIntProperty(“Value”, 200);
bytesMsg.setStringProperty(“Type”, “BytesMessage”);
sender.send(bytesMsg);

When messages are retrieved from the message queue, the text message is returned but the bytes message is not.