Selector are Properties which are set in the Header of the JMS Message so that you can filter only the messages which are compliant with the filter. You can set the selector using the selector attribute which is an SQL 92 predicate that is used to filter messages within the broker. You may have to encode special characters like ‘='
as %3D
.
Here is an example of two routes:
import java.io.IOException; import javax.jms.ConnectionFactory; import javax.management.MBeanServerConnection; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.camel.CamelContext; import org.apache.camel.LoggingLevel; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.jms.JmsComponent; import org.apache.camel.impl.DefaultCamelContext; public class App { public static void main(String args[]) throws Exception { CamelContext context = new DefaultCamelContext(); ConnectionFactory connectionFactory = new ActiveMQConnectionFactory( "tcp://0.0.0.0:61616"); context.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory)); context.addRoutes(new RouteBuilder() { public void configure() { from("timer:foo?period=1s").setBody(simple("Message at ${date:now:yyyy-MM-dd HH:mm:ss}")).to( "activemq:queue:somequeue"); from("activemq:queue:somequeue?selector=clientid %3D 'someid'").to("activemq:queue:otherqueue"); } }); context.start(); try { System.in.read(); } catch (IOException e) { e.printStackTrace(); } context.stop(); } }
Only messages which contain the header “clientid = ‘someid'” are allowed to transit from “somequeue” to “otherqueue”.