A message selector allows a JMS consumer to be more selective about the messages that it receives from a particular topic or queue. A message selector uses message properties and message headers as criteria in conditional expressions. These expressions use Boolean logic to declare the messages that are delivered to a client. The message consumer will only receive messages where the headers and properties match the selector. There are different patterns that can be used in selector strings to filter messages and the broker (JMS provider) filters messages according to that query. It is not possible for a message selector to filter messages on the basis of the content of the message body. See the list of supported message types and header fields in WSO2 MB.
Note |
---|
Note that message selectors are not supported in a clustered setup of WSO2 Message Broker (WSO2 MB). |
See the following topics for details:
Table of Contents | ||||
---|---|---|---|---|
|
...
The way selectors work depends on the type of subscription that is created from JMS clients. The following sections explain the different scenarios:
Queue subscriptions
- Only messages that match the selector will be delivered to the subscriber.
- If at least one subscriber out of a few subscribers for a queue has a matching selector, the message will be delivered to that subscriber.
- If more than one subscriber has a matching selector, the messages will be delivered in round robin manner between those subscribers.
- If none of the subscribers have matching selectors, that message will be placed in the Dead Letter Channel (DLC).
...
- Only messages that match the selector will be delivered to the durable topic subscriber.
- When keeping a copy of a message coming for a topic for the particular subscription ID, selectors are not evaluated. They are evaluated upon delivery only.
- If a message does not match with the selector for a particular durable topic subscription, it will be routed to the Dead Letter Channel (DLC).
Messages routed to DLC
- If none of the subscribers have matching selectors with a message and if the message is for a durable queue or topicor durable subscription, the message is routed to the DLC.
- User has the option to delete messages selectively if they are unnecessary.
- It is possible to create a new queue and route all unmatched messages to that queue if required.
- It is possible to create a new subscriber with a matching selector for some or all messages routed to the DLC and route all messages to the original queue again. Then, if there are newly created matching subscriptions, those messages will be delivered accordingly. Other messages will go to DLC queue once again.
...
A message header contains a number of predefined fields that contain values that both clients and providers can use to identify and to route messages. Following is a list See the list of supported message types and header fields in WSO2 MB.
The following are examples of message header selector strings that are supported:
...
If you need values in addition to those provided by the header fields, you can create and set properties for the messages. Following See the list of supported message types and header fields in WSO2 MB.
The following is a list of message property selector strings that are supported:
...
You can create a message selector subscriber for a topic as shown in the following example:example given below. Note that the nolocal
parameter is set to 'false' as it is not supported by WSO2 MB. However, messages will be delivered to the subscriber irrespective of the nolocal
parameter.
Code Block |
---|
String messgeSelector="JMSType='AAAA'";
topicSession.createSubscriber(topic,messgeSelector,true) |
...
false) |
The following examples illustrate how you can create a subscriber using selectors. The messageSelector
property is set to "releaseYear < 1980"
in all of these examples.
Queue subscription
Code Block |
---|
QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); //Receive message Queue queue = (Queue) ctx.lookup(queueName); MessageConsumer queueReceiver = queueSession. createConsumer(queue, "releaseYear < 1980"); |
...
Code Block |
---|
TopicSession topicSession =
topicConnection.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE);
Topic topic = topicSession.createTopic(topicName);
javax.jms.TopicSubscriber topicSubscriber = topicSession.
createDurableSubscriber(topic, "mySub4", "releaseYear < 1980", false); |
Publishing messages using selectors
...