camel loadbalancer failover custom error handler - java

With route defined in Spring DSL:
<route>
<from uri="direct:load1" />
<loadBalance>
<failover roundRobin="true" maximumFailoverAttempts="1"/>
<to uri="broker1:queue:queue"/>
<to uri="broker2:queue:queue"/>
</loadBalance>
</route>
How would implement a custom error handler or processor to handle the out body when the failover exhausted the maximumFailoverAttempts?
To make it simpler to understand, both broker1 and broker2 endpoints are unavailable, a processor should then be invoked to set the out body to "failed" as an example.
Currently I'm using <doTry>
<route>
<from uri="direct:load1" />
<doTry>
<loadBalance>
<failover roundRobin="true" maximumFailoverAttempts="1"/>
<to uri="broker1:queue:queue1"/>
<to uri="broker1:queue:queue2"/>
</loadBalance>
<doCatch>
<exception>java.lang.Exception</exception>
<process ref="loadbalanceExceptionProcessor" />
</doCatch>
</doTry>
</route>
Is there a better approach to this?

Related

Why header value isn't inserted into Apache Camel's `to` url parameter of the route in Spring DSL?

We have a route that will accept the kafka.KEY and use that as mqtt url parameter to send the data to the right topic.
<routes
xmlns="http://camel.apache.org/schema/spring">
<route id="KafkaToMQTT">
<from uri="kafka://mqtt?brokers=localhost:9092"/>
<to uri="micrometer:timer:camel.proxy.kafka.mqtt.stream?action=start"/>
<log message="Headers ${header.kafka.KEY}"/>
<to uri="mqtt:mqtt?host=tcp://localhost:1883&publishTopicName=try${header.kafka.KEY}"/>
<to uri="log://camel.proxy?groupInterval=3&level=INFO"/>
<to uri="micrometer:timer:camel.proxy.kafka.mqtt.stream?action=stop"/>
</route>
</routes>
In the log messages I see the ${header.kafka.KEY} correctly, while in the mqtt I'm getting the topic as literally try${header.kafka.KEY}
What is the reason for that, how to make the header to be used there?
To avoid that the right element instead of to should be used, that is toD.
toD concatenates the url correctly, so the right route XML is:
<routes
xmlns="http://camel.apache.org/schema/spring">
<route id="KafkaToMQTT">
<from uri="kafka://mqtt?brokers=localhost:9092"/>
<to uri="micrometer:timer:camel.proxy.kafka.mqtt.stream?action=start"/>
<log message="Headers ${header.kafka.KEY}"/>
<toD uri="mqtt:mqtt?host=tcp://localhost:1883&publishTopicName=ESP_02/try${header.kafka.KEY}"/>
<to uri="log://camel.proxy?groupInterval=3&level=INFO"/>
<to uri="micrometer:timer:camel.proxy.kafka.mqtt.stream?action=stop"/>
</route>
</routes>

Accessing member of java pojo set as exchange property in the camel spring route

I have written my route using spring xml which looks like this
<camelContext xmlns="http://camel.apache.org/schema/spring">
<propertyPlaceholder id="properties" location="classpath:props.properties" />
<route>
<from
uri="activemq:queue:adapter.queue?mapJmsMessage=false&disableReplyTo=true" />
<log message="This is a status request"></log>
<process ref="psuedoRoute"></process>
</route>
</camelContext>
As I am getting a Java POJO through my activemq end point, and that is the exchange body. Is it possible to read the value of a string member within this route xml itself?
Yes, its possible. You may use SPEL, it allows to call a method of Java object, getter in your case.
It could be like:
<camelContext xmlns="http://camel.apache.org/schema/spring">
<propertyPlaceholder id="properties" location="classpath:props.properties" />
<route>
<from
uri="activemq:queue:adapter.queue?mapJmsMessage=false&disableReplyTo=true" />
<log message="This is a status request"></log>
<process ref="psuedoRoute"></process>
<log message="This is a status request"></log>
<setBody>
<spel>#{body.getValue()}</spel>
</setBody>
</route>
</camelContext>

Apache Camel routing

A short question about Apache Camel.
I have the following scenario, where my server receives jms messages and then transform to csv file and then insert DB.
For this purpose i have 2 beans:
xml2csv
insertDB
I use routing like:
<route id="route1" errorHandlerRef="myErrorHandler">
<from uri="file://{someFolder1}}
?...
<to uri="bean:xml2csv" />
<log message="transformed to xml file" />
</route>
<route id="route2" errorHandlerRef="myErrorHandler">
<from uri="file://{{someFolder2}}
?...
<to uri="direct:csvOnboardingChannel" />
</route>
<route id="csvOnboarding" errorHandlerRef="myErrorHandler">
<from uri="direct:csvOnboardingChannel" />
<to uri="bean:insertDB" />
</route>
When "route" a file from-to, is it move like a message? or putting the question different, does Apache Camel take a file, wrap it as a message and route it to a bean or a component?
Do I understand it correct or am in a wrong directation.
Yes, your understanding is correct. Camel reads in the file's data and sends it as a message through the route to a bean. Might also be simpler as a single route, like so:
<route id="route1" errorHandlerRef="myErrorHandler">
<from uri="file://{someFolder1}}">
<to uri="bean:xml2csv" />
<to uri="bean:insertDB" />
</route>

Apache Camel: Spring WS SOAP Operation did not match

I am trying to create a Spring WS consumer like this:
<route>
<from uri="direct:start"/>
<to uri="spring-ws:rootqname:http://{ip}:{port}/PORTAL/webservices/A20?soapAction=acceptMessage"/>
<to uri="log:output"/>
</route>
Error:
SOAPAction http://{ip}:{port}/PORTAL/webservices/A20/acceptMessage does not match an operation
In your property placeholders you need to use double braces like so:
<route>
<from uri="direct:start"/>
<to uri="spring-ws:rootqname:http://{{ip}}:{{port}}/PORTAL/webservices/A20?soapAction=acceptMessage"/>
<to uri="log:output"/>
</route>
More info here: http://camel.apache.org/using-propertyplaceholder.html

How can I manually start a Camel Route through Spring XML?

I've got my route's autoStart set to false:
<route id="myRoute" autoStartup="false">
Everything I can find online about how to start it after that is for starting it in java and says to call startRoute("myRoute"); on the camelContext.. but I can't find anything about how to call that from within a route in Spring XML.
Here's my setup:
<route id="myRoute" autoStartup="false">
<from uri="ftp://remote/dir" />
<to uri="file:///local/dir" />
</route>
<route id="kickOff">
<from uri="timer://runOnce?repeatCount=1&delay=30000" />
<!-- START myRoute HERE -->
<to uri="bean:postProcessor?method=postProcess" />
</route>
My goal is to have the FTP get all the files on the FTP once, then stop that route. Currently it'll continue polling the FTP indefinitely. I tried adding a org.apache.camel.impl.LimitedPollingConsumerPollStrategy with a limit of 1 but that didn't seem to change anything.
See the controlbus eip / component where you can start routes:
http://camel.apache.org/controlbus.html
The code should be something alike:
<route id="kickOff">
<from uri="timer://runOnce?repeatCount=1&delay=30000" />
<to uri="controlbus:route?routeId=myRoute&action=start" />
<to uri="bean:postProcessor?method=postProcess" />
</route>

Categories