The selective consumer is consumer that applies a filter to incoming messages so that only messages meeting that specific selection criteria are processed. A JMS selector is a predicate expression involving JMS headers and JMS properties. If the selector evaluates to true, the JMS message is allowed to reach the consumer.
Let us consider an Order processing example, we have two types of orders (One is Online and another one is physical store order) All orders are pushing in to ORDER queue along with the details and Two consumers (Online order processor and Physical order processor) are selecting messages based on the predicate expressions.
PrerequisitesActiveMQ should be up and running
1. Create a Maven ProjectCreate a Maven project with following dependencies
2. Setup Spring Configuration (camel-application-context-msgselector.xml)In this configuration we will load the activemq component with name jms and start one route which needs to process the Order Queue.
3. Create a Message RouterThis route has the message selector and it will listen the ORDER Queue and based on the selector it will redirect to the other Queues.
eg: ONLINE order details will go the the online processor and store orders will go the the store processor.
4. Create a Message producerThis class will push the order messages in to ORDER queue and add the selector name as well.
Run itRun the above main program and check the active mq console, we can see the massages are routed in to different queue based on the selector.