Camel - StringTemplate doesn't display CamelBatchSize - apache-camel

I've a small string template (fragment for testing):
{headers.CamelBatchSize}
{headers.CamelFileName}
{headers.CamelFilePath}
{headers.fileCount}
And when I call it:
<setHeader headerName="fileCount">
<simple>${headers.CamelBatchSize}</simple>
</setHeader>
<to uri="string-template:file:notifymail.tm?delimiterStart={&delimiterStop=}" />
The first row is empty (CamelBatchSize) while the others displayed well. Why cannot I use the CamelBatchSize (without putting it into another header variable)? Where am I wrong?

Are you using a Batch-Consumer? https://camel.apache.org/batch-consumer.html

Related

Apache Camel: set boolean header using simple contains

Is there any way to set header with boolean value for checking contains like this.
<setHeader name="status">
<simple>${headers.type} contains 'client'</simple>
</setHeader>
This should set status as true if type contains client else false.
Check the docs, you need resultType
<setHeader name="status">
<simple resultType="java.lang.Boolean">${headers.type} contains 'client'</simple>
</setHeader>

Apache Camel: Send empty body ws

How can i call a SOAP web service with an empty message body using Apache Camel?
For example, the final endpoint on a route would be the invocation of a method on my proxy that takes 0 arguments.
EDIT:
example xml configuration:
<route id="someRoute">
<from uri="ref:activemq-queue"/>
<setHeader headerName="operationName">
<constant>invoke</constant>
</setHeader>
<to uri="cxf:bean:someWS"/>
</route>
...
<cxf:cxfEndpoint id="someWS" address="${ws.address}"
serviceClass="com.example.ws.SomeWS"
The problem is that the method 'invoke' on the WS takes 0 arguments, and an exception is thrown stating that 1 argument is being received. Is there a way for me to specify to ignore this received input?
You can set the message body to be null, if the invocation just take 0 argument. null simple expression is added since camel 2.12.3.
<route id="someRoute">
<from uri="ref:activemq-queue"/>
<setBody>
<simple>null</simple>
</setBody>
<setHeader headerName="operationName">
<constant>invoke</constant>
</setHeader>
<to uri="cxf:bean:someWS"/>
</route>
I also needed to set an empty body to Apache Camel xml and the solution below worked for me.
<setBody id="set-empty-body">
<constant/>
</setBody>
Hope this helps to whoever need it.
The xml configuration will fail if the content of the body is not with the correct type of the class required by the method of your bean. By default, it could be an Object and not a String. When the method recieves the body in an incorrect format, it would process a null value, instead of the body of the route. To solve this problem, you will need to convert the body into the expected class of the method of your bean. For example, if the method's argument of the bean is a String, then you could do something like:
<route id="someRoute">
<from uri="ref:activemq-queue"/>
<setHeader headerName="operationName">
<constant>invoke</constant>
</setHeader>
<convertBodyTo type="String"/>
<to uri="cxf:bean:someWS"/>
</route>
For more information about body conversion, you can check the docs: https://camel.apache.org/components/3.14.x/eips/convertBodyTo-eip.html

How to extract value from Exchange object in camel esb

I have created a simple cxf web service. following is the body of soap message
<soapenv:Body>
<bean:getRTOEmployeeSalary>
<!--Optional:-->
<bean:arg0>sdf</bean:arg0>
</bean:getRTOEmployeeSalary>
</soapenv:Body>
My requirement is to extract the value of arg0 in my camel context file. i.e. i want to log the value of arg0. Please help me on this
<route routePolicyRef="loggingInInterceptor">
<from uri="cxf:bean:rtoemplyeeService"/>
<setHeader headerName="exchange">
<spel>${exchange}</spel>
</setHeader>
<log message="value of arg0======== "/>
<convertBodyTo type="java.lang.String" id="stringInput"/>
<bean ref="rtoEmpBean" method="getRTOEmployeeSalary" beanType="rtoEmpBean" id="govtRTOEmp"/>
</route>
I need to use the value of arg0 here.
We can use camel provided spring expression language to extract the value from exchange object. Since exchange object also resides in spring container.
below will be code src to extract the value of arg0 in camel context-
<setHeader headerName="arg0">
<spel>#{exchange.in.body.get(0)}</spel>
</setHeader>
This will set the value of arg0 of soap message in a header named arg0.
http://camel.apache.org/spel.html

Camel HazelCast component throwing HazelcastSerializationException

I have the following route in Camel:
<route>
<from uri="servlet:///user?matchOnUriPrefix=true"/>
<setHeader headerName="cachename">
<simple>${header.CamelHttpPath.split("/")[1]}</simple>
</setHeader>
<setHeader headerName="key1">
<simple>${header.CamelHttpPath.split("/")[2]}</simple>
</setHeader>
<to uri="direct:put"/>
</route>
<route>
<from uri="direct:put" />
<!-- If using version 2.8 and above set headerName to "CamelHazelcastOperationType" -->
<setHeader headerName="CamelHazelcastOperationType">
<constant>put</constant>
</setHeader>
<setHeader headerName="CamelHazelcastObjectId">
<simple>${header.key1}</simple>
</setHeader>
<process ref="requestTimeProc"/>
<to uri="hazelcast:map:default" />
</route>
And i am trying to put a string as value into the cache.
My HazelCast Configuration is :
<map name="default">
<in-memory-format>OBJECT</in-memory-format>
<backup-count>1</backup-count>
<async-backup-count>0</async-backup-count>
<time-to-live-seconds>0</time-to-live-seconds>
<max-idle-seconds>0</max-idle-seconds>
<eviction-policy>NONE</eviction-policy>
<max-size policy="PER_NODE">0</max-size>
<eviction-percentage>25</eviction-percentage>
<merge-policy>com.hazelcast.map.merge.PassThroughMergePolicy</merge-policy>
</map>
<serialization>
<portable-version>0</portable-version>
</serialization>
And i Am gettign the following exception:
2013-12-10 22:47:08,288 [tp-1763826860-0] ERROR DefaultErrorHandler - Failed delivery for (MessageId: ID-DJD7W4R1-54721-1386740808850-0-1 on ExchangeId: ID-DJD7W4R1-54721-1386740808850-0-2). Exhausted after delivery attempt: 1 caught: com.hazelcast.nio.serialization.HazelcastSerializationException: There is no suitable serializer for class org.apache.camel.converter.stream.InputStreamCache
com.hazelcast.nio.serialization.HazelcastSerializationException: There is no suitable serializer for class org.apache.camel.converter.stream.InputStreamCache
at com.hazelcast.nio.serialization.SerializationServiceImpl.toData(SerializationServiceImpl.java:172)[hazelcast-3.1.2.jar:3.1.2]
at com.hazelcast.nio.serialization.SerializationServiceImpl.toData(SerializationServiceImpl.java:157)[hazelcast-3.1.2.jar:3.1.2]
at com.hazelcast.map.MapService.toData(MapService.java:666)[hazelcast-3.1.2.jar:3.1.2]
at com.hazelcast.map.proxy.MapProxyImpl.put(MapProxyImpl.java:72)[hazelcast-3.1.2.jar:3.1.2]
at com.hazelcast.map.proxy.MapProxyImpl.put(MapProxyImpl.java:60)[hazelcast-3.1.2.jar:3.1.2]
at org.apache.camel.component.hazelcast.map.HazelcastMapProducer.put(HazelcastMapProducer.java:136)[camel-hazelcast-2.11.2.jar:2.11.2]
at org.apache.camel.component.hazelcast.map.HazelcastMapProducer.process(HazelcastMapProducer.java:71)[camel-hazelcast-2.11.2.jar:2.11.2]
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)[camel-core-2.11.2.jar:2.11.2]
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)[camel-core-2.11.2.jar:2.11.2]
at org.apache.camel.processor.SendProcessor$2.doInAsyncProducer(SendProcessor.java:122)[camel-core-2.11.2.jar:2.11.2]
Please let me know if i am missing anythign inthe configuration . I am using the default Hazelcast configuration provided int he Hazelcast jar. Any help will be highly appreciated
A few comments:
I would not change the 'default' map. This means that any map in the system, which doesn't have an explicit configuration, will now use this configuration. So figure out which map is used and configure explicitly for that map.
<map name="explicitName">
<in-memory-format>OBJECT</in-memory-format>
....
</map>
About the exception:
com.hazelcast.nio.serialization.HazelcastSerializationException: There is no suitable serializer for class org.apache.camel.converter.stream.InputStreamCache
It means that the InputStreamCache is put in the map, but Hazelcast doesn't know how to conver it to a stream of bytes. You can plug in a serializer for this class, see the following blogpost how to do so:
http://blog.hazelcast.com/blog/2013/10/16/kryo-serializer/
The big question for me is: why are you trynig to put an InputStreamCache in a map? My gut feeling says that this class is not something you want to put in a distribured Hazelcast map at all.

Camel File Endpoint - Getting the file name

I have a camel route:
from("file:///u01/www/images/nonprofits-test?move=.done&preMove=.processing&filter=#nonpFileFilter&minDepth=2&recursive=true")
Later on in the route I need to access the origin file name. How do I get that information? All of the headers contain information in like ${file:name}, but not the actual file name.
Thanks in advance!
The base problem is that simple language is not being evaluated correctly in while running Camel with grails. This is being discussed further on the Camel user list.
there is a header called "CamelFileName" that stores this
see camel-file2 headers section for more details...
If your simple language is not working it would be because you are not using <simple> tag try something like below.
<route id="movedFailedFileForRetry">
<from uri="file:///opt/failed?delete=true" />
<log loggingLevel="INFO" message="Moving failed file ${header.CamelFileName} for retry" />
<choice>
<when>
<simple>${headers.CamelFileName} == 'file1.txt'</simple>
<to uri="file:///opt/input1" />
</when>
<otherwise>
<to uri="file:///opt/input2" />
</otherwise>
</choice>
</route>
Hope it helps!!
${headers.CamelFileName} will provide you with the CamelFileName that is read for processing. We have many other header properties that you can find from the Camel Documentation.

Resources