An example of Apache Camel that performs a set of commands every 5000 milliseconds in the following order:
- Step1: the method getValue of the bean SampleBean is called (bean:samplebean?method=getValue);
- Step2: the result of the Step1 is transformed with a Processor that append a string to the input (see Apache Camel Processor);
- Step3: the result of the Step2 is used as input to call a Web Service whose endpoint is defined in the route-context.xml (see toUpperCaseEndpoint); The web service returns a value in uppercase of the input string;
- Step4: the result of the Step3 is transformed by a new Processor that is responsible for adapting the format to the next endpoint (
"stream:file?fileName=C:\\Users\\mydoc\\example.txt );
- Step5: the result of the Step4 is used to be written in a file named example.txt
"stream:file?fileName=C:\\Users\\mydoc\\example.txt).
Example of Camel Context
The definition of the camel endpoint used in the route definition.
route-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/cxf"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://camel.apache.org/schema/cxf
http://camel.apache.org/schema/cxf/camel-cxf.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
">
<camelContext xmlns="http://camel.apache.org/schema/spring">
<package>it.example.route</package>
</camelContext>
<cxf:cxfEndpoint id="toUpperCaseEndpoint"
address="http://localhost:8081/camel-jboss/ToUpperCaseBean"
wsdlURL="http://localhost:8081/camel-jboss/ToUpperCaseBean?wsdl"
serviceClass="it.example.ws.ToUpperCase"
serviceName="t:ToUpperCaseBeanService"
endpointName="t:ToUpperCaseBeanPort"
xmlns:t="http://ws.apachecxf.simonefolinoblogspot.com/"/>
<bean id="samplebean" class="it.example.bean.SampleBean"/>
</beans>
Example of Java Bean
SampleBean.java
package it.example.bean;
public class SampleBean {
public String getValue() {
return "value of sample bean";
}
public void syso(){
System.out.print("syso");
}
}
SEI (Service Endpoint Interface)
ToUpperCase.java
package it.example.ws;
import javax.jws.WebService;
@WebService
public interface ToUpperCase {
String toUpperCase(String s);
}
Web Service Implementation
ToUpperCaseBean.java
@Stateless(name="custom/ToUpperCaseBean", mappedName="custom/ToUpperCaseBean")
@WebService(endpointInterface="it.example.ws.ToUpperCase",targetNamespace="http://ws.apachecxf.simonefolinoblogspot.com/")
public class ToUpperCaseBean implements ToUpperCase {
@WebMethod(operationName="toUpperCase")
@WebResult(name = "return", targetNamespace = "http://ws.apachecxf.simonefolinoblogspot.com/")
public String toUpperCase(String value) {
System.out.println("value="+value);
String upperCase = value.toUpperCase();
System.out.println("upperCase="+upperCase);
return upperCase;
}
}
}
Example of RouteBuilder
WebServiceRouteBuilder.java
package it.example.route;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
public class WebServiceRouteBuilder extends RouteBuilder {
@Override
public void configure() {
from("timer://foo?fixedRate=true&period=5000").to("bean:samplebean?method=getValue").process(
new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
if (exchange != null) {
Message in = exchange.getIn();
if (in != null) {
in.setBody(in.getBody() +" i am the body");
exchange.getOut().setBody(in);
}
}
}
}).to("cxf:bean:toUpperCaseEndpoint").setHeader("toUpperCase").body().process(
new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
if (exchange != null) {
Message in = exchange.getIn();
if (in != null) {
exchange.getOut().setBody(in.getBody());
}
}
}
}).to("stream:file?fileName=C:\\Users\\mydoc\\example.txt");
}
}
Route Execution Output
This is the output of the execution of the camel route.
VALUE OF SAMPLE BEAN I AM THE BODY