Conditional Apache Camel Routing

I have a service that has two operations.

RegisterUser UpdateUser 

I have a camel route:

 <camel:route id="myRoute"> <camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&amp;synchronous=true" /> <camel:bean ref="processor" method="processMessage"/> <camel:to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/> <camel:to uri="cxf:bean:myTargetEndpoint"/> </camel:route> 

In my bean processor, when I specify:

 RegisterUser registerUser = exchange.getIn().getBody(RegisterUser.class); 

I get a registry user object. Everything is working fine. The problem is that I want the camel to request my request conditionally, for example, for example:

If the operation is a RegisterUser service, I want to redirect the message to my specific bean, and if the operation is UpdateUser , I want to redirect the message to another bean.

I tried using camel xPath, but it does not work.

 <camel:route id="myRoute"> <camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&amp;synchronous=true" /> <camel:choice> <camel:when> <camel:xpath> //RegisterUser </camel:xpath> <camel:bean ref="processor" method="processMessage"/> <camel:to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/> </camel:when> </camel:choice> <camel:to uri="cxf:bean:myTargetEndpoint"/> </camel:route> 

I was looking for how to set up a camel to route for different purposes, but found nothing. Maybe someone knows where the problem might be?

+7
source share
2 answers

Information about the required operation will be in the message header.

The title you are looking for is called "operationName"

So here is an example:

 <camelContext xmlns="http://camel.apache.org/schema/blueprint"> <route id="example"> <from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&amp;synchronous=true" /> <log message="The expected operation is :: ${headers.operationName}" /> <choice> <when> <simple>${headers.operationName} == 'RegisterUser'</simple> <bean ref="processor" method="processMessage"/> <to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/> </when> <when> <simple>${headers.operationName} == 'UpdateUser'</simple> <!-- Do the update user logic here --> <bean ref="processor" method="updateUser" /> </when> </choice> <to uri="cxf:bean:myTargetEndpoint"/> </route> </camelContext> 

(Note that the example uses the apache aries project - but it will be identical for spring, except for the namespace)

+14
source

try using camel-simple instead of xpath for this ...

 <when><simple>${body} is 'com.RegisterUser'</simple><to uri="..."/></when> 
+4
source

All Articles