Guide to Annotation DSL (Dynamic Router)
In some situations, we may need to route our messages based on a list of destinations that have been created dynamically, For these types of situations we can use a Dynamic Router that can utilize a self-configuration using a dynamic recipient list. This can be linked to a data store that contains a set of destinations.
In the following example, we see the implementation of an Annotation DSL Dynamic Router pattern to determine our destinations through the use of a processor.
Â
Annotation DSL Example
public class MyDynamicRouter {
@Consume(uri = "jms:queue:order")
@RecipientList
public List<String> route(@XPath("/customer/id")
String customerId, @Header("location") String location,
Document body) {
// query data store, find best match for the
//endpoint and return destination (s)
}
}
Â
Camel uses a strong type converter feature to convert the payload to the specific type designated by the parameter within the method signature (We could use String and Camel will convert the body to a String). You have the ability to register your own type converters too using the @Converter annotation. It can be defined at either the class or method levels.
NOTE: In this example, it uses a Bean Parameter Binding to bind the parameters to the route method based on an @XPath expression on the XML payload of the JMS message. This allows us to extract the customer id as a string parameter. @Header will bind a JMS property with the key location. The use of Document refers to the XML payload of the message.
Â