Apache Camel conditional routing - java

I have a service which has two operations.
RegisterUser
UpdateUser
I have a camel rout:
<camel:route id="myRoute">
<camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&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 processor bean, when I specify:
RegisterUser registerUser = exchange.getIn().getBody(RegisterUser.class);
I get the register user object. Everything works fine.
The problem is that I want camel to route my request conditionally, for e.g:
If the service operation is RegisterUser I want to route the message to my specific bean and if the service operation is UpdateUser I want to route the message to the other bean.
I have tried to use camel xPath, but it not seems to be working.
<camel:route id="myRoute">
<camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&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 searching how to set up camel to route to the different targets but did not find anything. Maybe somebody knows where might be the problem?

The information of the operation required will be in the header of the message.
The header 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&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 the example is using apache aries blueprint - but it will be identical for spring, other than the namespace)

try using camel-simple expressions instead of xpath for this...
<when><simple>${body} is 'com.RegisterUser'</simple><to uri="..."/></when>

Spring XML route
In my case I use inbound Jetty EP.
I check parametr in request.
Invole URL http://localhost:8080/srv?alg=1
<choice id="_choice1">
<when id="_when1">
<simple>${in.header.alg} == '1'</simple>
<log id="_log10" message="LOG ALG 1"/>
</when>
...
<otherwise id="_otherwise1">
<setFaultBody id="_setFaultBody1">
<constant>Return message about ERROR</constant>
</setFaultBody>
</otherwise>
</choice>

final CamelContext context = exchange.getContext();
if (isAlive) {
context.startRoute("table-reader-route");
log.info("starting dailycase route= " + response);
} else {
context.stopRoute("table-reader-route");
log.info("stoping dailycase route= " + response);
}

Related

Apache Camel. How to manage with a Custom Processor the error raised at runtime

I'm trying to convert my route made with Java DSL in a route made with XML.
The following is my original route that works. And what it does is simple.
Get as input an array of integer and at runtime throws some errors.
At the end of the route I need to read all the error raised by myself and not with a long stacktrace or other long messages in console.
other lines of code...
camelContext.addRoutes(new RouteBuilder() {
#Override
public void configure() throws Exception {
onException(Exception.class)
.handled(true)
.process(new ErrorProcessor());
from("direct:start_route")
.split(body())
.processRef("payloadProcessor")
.to("direct:a")
.end()
.to("direct:d");
from("direct:a")
.beanRef("stupidBean", "stupidMethod");
from("direct:d")
.beanRef("errorProcessor", "check");
}
});
other lines of code...
The following is my xml route that doesn't work.
...
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="direct:call.playWithPayload" />
<onException>
<exception>java.lang.Exception</exception>
<process ref="errorProcessor" />
</onException>
<split onPrepareRef="payloadProcessor">
<simple>${body}</simple>
<to uri="direct:call.a" />
</split>
<to uri="direct:call.d" />
</route>
<!--SUB ROUTE-->
<route>
<from uri="direct:call.a" />
<bean ref="stupidBean" method="stupidMethod" />
</route>
<route>
<from uri="direct:call.d" />
<bean ref="errorProcessor" method="check" />
</route>
</camelContext>
...
What I need is that the direct:call.d is called after the split.
Thanks to this I can read all the errors added into a List<Exception> that is stored into the header.
I think that the problem is in the onException management.
When I try to add the handled to reproduce the my Java DSL
<onException>
...
<handled>
<constant>
true
</constant>
</handled>
...
I got this error:
Invalid content was found starting with element 'handled'.
One of '{"http://camel.apache.org/schema/spring":description, "http://camel.apache.org/schema/spring":exception}' is expected.
Found solution.
My problem was an incorrect format of my xml route.
<onException>
<exception>java.lang.Exception</exception>
<handled>
<constant>true</constant>
</handled>
<process ref="errorProcessor" />
</onException>
now it works.

Apache Camel route: onCompletion not reached when exception occurs?

I have a Camel route that looks something like the one below. If all records parse successfully, then I get an email from the onCompletion step. If one record gets an exception then the rest of the records will process, which is fine, but the onCompletion step does not fire.
What I'd like is for the onCompletion step to run even if there are errors and to be able to send a message saying "route completed with errors". How can I do this?
<route id="route1">
<from uri="file://C:/TEMP/load?noop=true&idempotentRepository=#sysoutStore&sorter=#externalDataFilesSorter"/>
<choice>
<when>
<simple>${file:name} regex '*file.*.(txt)'</simple>
<to uri="direct:RouteFile" />
</when>
</choice>
</route>
<route id="testRouteDirect">
<from uri="direct:RouteFile" />
<onException>
<exception>java.lang.IllegalArgumentException</exception>
<redeliveryPolicy maximumRedeliveries="1" />
<handled>
<constant>true</constant>
</handled>
<to uri="log:java.lang.IllegalArgumentException"></to>
</onException>
<onException>
<exception>java.text.ParseException</exception>
<redeliveryPolicy maximumRedeliveries="1" />
<handled>
<constant>true</constant>
</handled>
<to uri="log:java.text.ParseException"></to>
</onException>
<split parallelProcessing="false" strategyRef="exchangePropertiesAggregatorStrategy" >
<tokenize token="\r\n"/>
<to uri="log:Record"></to>
</split>
<onCompletion>
<to uri="log:completion"></to>
<to uri="smtp://mail.com?contentType=text/html&to=done#test.com&from=route#test.com&subject=we're done" />
</onCompletion>
</route>
The best part of your route is, you have onException inside your route with handled=true. So move your onCompletion to the parent route(route1), It should work !
There are a bunch of tickets related to oncompletion on the camel site: Camel Jira URL. I upgraded to a newer version of camel & I don't get this issue any more.

How to set headers from bean integration method call in a camel route

Help needed on setting headers based on the method call from bean integration.
within my application I am using a custom POJO and there are before I actually send the message over the wire I want to do set the headers on the exchange, but don't want to do it within my bean and rather do it where my spring DSL is written for the route.
I know that usually the value returned from method is sent as body for the message to the next the endpoint but i want to send values returned as header.
I have attached a sample of my route needs to be, and want:
<route id="someRoute">
<from ref="InboundAsyncEndpoint" />
<to uri="bean:validatorBean?method=validateMessageInternals(MyCostomMessagePojo obj)" />
<choice>
<when>
<simple>
${body.getMetaData().getFinalDestinationName()} == 'AMQEndpoint'
</simple>
<to uri="bean:payloadAndHeaderExtractor?extractHeader(MyCostomMessagePojo obj)" />
<to uri="bean:payloadAndHeaderExtractor?extractPayload(MyCostomMessagePojo obj)" /> <!-- i want the headers being set on the exchange from the map that is returnd from the previous bean and method -->
<to uri="activemq:someQueue"
</when>
<otherwise>
...
...
</otherwise>
</choice>
You can do that in the following way:
<setHeader headerName="YOUR_HEADER">
<simple>bean:payloadAndHeaderExtractor?extractHeader(MyCostomMessagePojo obj)</simple>
</setHeader>
Hope this helps.
R.

Proper use of Camel Aggregator "to" URIs

I have a route where I want Camel to visit the following beans:
First, loggingBean
Second, an aggregator that waits for a certain number of messages to aggregate on it
Once the Aggregator's completionSize is reached (3), keep going to #4
Third, processorBean
And fourth/last, finalizerBean
Here is my route:
<route id="my-camel-route">
<from uri="direct:starter" />
<to uri="bean:loggingBean?method=shutdown" />
<aggregate strategyRef="myAggregationStrategy" completionSize="3">
<correlationExpression>
<simple>${header.id} == 1</simple>
</correlationExpression>
<to uri="bean:processorBean?method=process" />
</aggregate>
<to uri="bean:finalizerBean?method=shutdown" />
</route>
My question: do I need to place finalizerBean inside the <aggregate> element like so:
<aggregate strategyRef="myAggregationStrategy" completionSize="3">
<correlationExpression>
<simple>${header.id} == 1</simple>
</correlationExpression>
<to uri="bean:processorBean?method=process" />
<to uri="bean:finalizerBean?method=shutdown" />
</aggregate>
Basically, I'm wondering if the way I currently have things will prompt Camel to send the message to the aggregator, and then also send it on to finalizerBean (essentially, bypassing the aggregator). In my case, I want it to aggregate until completionSize is 3, and then send the aggregated exchange on to the processorBean and then finally finalizerBean.
Or have I configured this correctly? What's the difference between finalizerBean being inside the <aggregate> element vs being outside it?
The second example is correct.
<aggregate strategyRef="myAggregationStrategy" completionSize="3">
<correlationExpression>
<simple>${header.id} == 1</simple>
</correlationExpression>
<to uri="bean:processorBean?method=process" />
<to uri="bean:finalizerBean?method=shutdown" />
</aggregate>
If finalizerBean is "outside" the <aggregate>, it will get executed for every message that comes from direct:starter - which isn't what you want ;)

camel serialize json in dead letter queue

I have to stock my object (CcRequest) in a DeadLetterQueue with Json format.
How it is possible ?
Here my simple context :
<camelContext id="el1DMRCamelContext" autoStartup="true" xmlns="http://camel.apache.org/schema/blueprint" >
<template id="producerTemplate" />
<!-- Routes -->
<route id="createCcProcessorRoute" errorHandlerRef="createCcErrorHandler" >
<from uri="activemq:queue:createCc" />
<process ref="createCcProcessor" />
</route>
</camelContext>
<bean id="createCcErrorHandler" class="org.apache.camel.builder.DeadLetterChannelBuilder" >
<property name="deadLetterUri" value="activemq:queue:createCcDLQ" />
<property name="redeliveryPolicy" ref="redeliveryPolicy" />
</bean>
I would like my object (CcRequest stocked in the body of the exchange) in my activemq:queue:createCcDLQ to be in Json format instead of binaries.
Is it possible ?
I would not classify what you want to do as a dead letter handler. Dead letter is usually just moving/redelivery of the original message.
It is pretty simple you use the excetion clause instead.
<dataFormats>
<json id="jsonFormat" library="Jackson"/>
</dataFormats>
<onException>
<exception>java.lang.RuntimeException</exception>
<marshal ref="jsonFormat"/>
<to uri="activemq:queue:createCcDLQ"/>
</onException>
Camel Exception Clause docs
Camel JSON docs

Categories

Resources