Is there a way to set message headers from a bean? - apache-camel

I have a simple camel route I need to modify. The route looks like this:
from(source.uri)
.unmarshal()
.bean(TransformMessageBean.class, "SomeMethod")
.to(destination.uri)
I want to add another bean method call after the unmarshaling that set's a header value without disrupting the current data flow . Does anyone know of a way to do this? I read that in apache's documentation that a bean's return value is set in the outbound message body. Is there a way to change that to a header?
Thanks in advance!

Certainly! One of the options available is
.setHeader("headerName").method(beanInstance, "methodToGetHeaderValue")
Using this approach, the method (say, methodToGetHeaderValue) on the provided bean instance will return a value, and that will become the value of the header.
There are a number of options available. You can call it with a bean instance, a bean class (like in your example), or even a bean name, all with or without a method name.

In Spring XML DSL, it would be something like
<setHeader headerName="headerName">
<method bean="mybean" method="mymethod" />
</setHeader>

Another way in XML:
<setHeader name="headerName">
<simple>${bean:yourBean.getMethod}</simple>
</setHeader>

Related

How to read two pair of files in same directory and send to same camel exchange?

I need to read pair of files with same name but different extension in same directory say for example
1. abc.json
2. abc.signed
above files should be sent same camel exchange, where value from abc.signed is used to validate the abc.json file.
Please advise !!!
Use pollenrich to read both kind of file in a single route and then perform validation within that route.
<pollEnrich timeout="5000"><simple>file://pathto//yourfile?antInclude=*.json&noop=true</simple>
</pollEnrich>
<setHeader headerName="JSON">
<simple>${body}</simple>
</setHeader>
By doing so, you can able to get both json and signed values.

Moving Multiple file from one folder to another based on filename - camel

I have one requirement, where I need to move multiple files present in one folder to another. This should be done based on filename which should be dynamic.
As Far I have tried out pollenrich and file (antInclude) but in both case I got struck.
<route id="readFile" autoStartup="true">
<from uri="timer://timer1?repeatCount=1"/>
<setHeader headerName="xxx">
<simple>1234</simple>
</setHeader>
<pollEnrich timeout="15000">
<simple>file://{{baseDirectory}}?move={{destinationDirectory}}&antInclude=*_${header.xxx}.txt</simple>
</pollEnrich>
</route>
Note: Header value will be dynamic, using javascript will pull that data and set it in header.
Any help on this would most welcome.
Thank you for all your response,
I achieved it by using pollenrich but tweaking something in pollenrich.
Got CamelBatchsize using pollenrich.
Running into a loop using size.
Using the above code used to move the file. I achieved it.
Thanks.

SpEL for URI Evaluation in Apache Camel

Is it possible to use SpEL when specifying a URI in a route? I've tried this a few ways but doesn't seem to be working.
Would like to do something like:
<from uri="jms:queue:#{ ${mq.dynamic.switch} ? '$mq.dynamic.queue' : '$mq.static.queue'}?connectionFactory=#connectionFactory" />
I'm essentially trying to evaluate a property to determine what queues to leverage when configuring a JMS route.
No that is not possible in <from>. Howerver you can use Camel's property placeholders:
http://camel.apache.org/using-propertyplaceholder.html
And in Camel routes such as <to> you can use "dynamic-to" which allows to use any of the scripting languages such as SpEL also:
http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html
http://camel.apache.org/languages.html

Bug in Camel 2.13.2 Release-Bud Id 7544

The XSD allows to specify customId="true" id="foo" in the route elements. But when reading the route using Spring you get a parse error in Spring. I can't able to understand this bug. Can you please clarify me?
You should not use customId but only id.
The customId is for internal usage when updating routes from XML that the id is auto assigned or assigned by the end user.

How to get File Name without extension in Camel

Hi Does anyone know how to get a file name without extension in camel. using spring xml.
I know ${header.CamelFileNameOnly} will give the full file name like "test.txt".
I would just like to get the name "test" and need to use this name in somewhere else, does anyone know how to do it ?
Use the file-expression language. Your route shall be something like
<camel:route>
<camel:from uri="file://input/orders" />
<camel:setHeader headerName="FileNameWithoutExtension">
<camel:simple>${file:onlyname.noext}</camel:simple>
</camel:setHeader>
</camel:route>
See the Camel documentation
http://camel.apache.org/file-language.html
You can then do use simple language (http://camel.apache.org/simple.html) to grab the only the name part using ${file:onlyname.noext}

Resources