I am new to Apache Camel. I have a question related to how method is passed to a bean defined in a route.
I have the following route definition:
<bean id="myBean" class="camel.MyBean"/>
<camelContext id="testContext" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file:data/inbox?noop=true"/>
<to uri="myBean"/>
<to uri="file:data/outbox"/>
</route>
</camelContext>
The class MyBean is defined as follows:
public class MyBean {
public void myMethod(Exchange exchange) {
System.out.println("MyBean myMethod " + exchange.getIn().toString());
}
}
This works fine, I got the Exchange that contains a "In message" which contains the file. The exchange.getIn().toString() gives me the file name.
If I rewrite MyBean and makes it becomes:
public class MyBean {
public void myMethod1(String string) {
System.out.println("MyBean myMethod1 " + string);
}
}
It also works. The incoming message is somehow converted to a String that contains the contents of the incoming text file.
If I then rewrite the MyBean and makes it:
public class MyBean {
public void myMethod2(Integer integer) {
System.out.println("MyBean myMethod2 " + integer);
}
}
Then it stopped working. I think it might be because Camel has no way to convert a file to an integer.
I then have a question that: although Camel does all the type conversion for us under the cover, we do still need to know what data type it carries in order for us to design the node on the route. And there seems no rule to follow. What is the general rule of Camel that specifies how this type conversion is conducted?
Thank you very much.
First of all, Systems will be designed to understand a input data when it is in specific format which means there will a contract defined for the other systems(who want to communicate).
To answer your question, Lets assume that your application developed is capable of processing only XML input and you might have 'N' users who may need to use you application for processing who will be sending data in CSV,pipe-delimited etc., In this case, you must receive all those messages and based on the content type you have to parse and convert to xml message which will be processed by your core processor.In this case, the core processor actually does the actual processing.
Camel provides Conten based Router for this.
Check this link https://camel.apache.org/content-based-router.html
Hope this helps