Chapter 6. Deploying an Apache Camel WS Endpoint
Abstract
6.1. Apache Camel CXF Example
Overview
url-pattern setting from web.xml, and the URI of the Camel CXF endpoint are combined to give the URL, http://localhost:8080/camel-example-cxf-tomcat/webservices/incident.
Figure 6.1. Camel CXF Example Deployed in a Web Server
camel-example-cxf-tomcat example
examples/camel-example-cxf-tomcat directory. For details of how to install the Apache Camel distribution, see the section called “Install Apache Camel”.
Camel CXF component
Exchange object, and can then propagate through the route.
cxf:Address[?Options]- Specifies the WSDL endpoint address and a (potentially large) number of options to configure the endpoint.
cxf:bean:BeanID[?Options]- References a bean with the ID,
BeanID, defined using thecxf:cxfEndpointelement (where thecxfprefix is bound to thehttp://camel.apache.org/schema/cxfnamespace). The advantage of this approach is that all of the configuration complexity is encapsulated in the bean. Typically, this means that very few options (or none) need to be specified on the endpoint URI.NoteThecxf:cxfEndpointelement, which binds a WS endpoint to a Camel route, should not be confused with thejaxws:endpointelement, which binds a WS endpoint directly to a Java class.
More about the Camel CXF component
- Web Services and Routing with Camel CXF
- The CXF chapter from the EIP Component Reference.
web.xml file
web.xml file. In the camel-example-cxf-tomcat project, the web.xml file is stored at the following location:
camel-example-cxf-tomcat/src/main/webapp/WEB-INF/web.xml
web.xml file.
Example 6.1. web.xml File for the camel-example-cxf-tomcat Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>My Web Application</display-name>
<!-- location of spring xml files -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:camel-config.xml</param-value>
</context-param>
<!-- the listener that kick-starts Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- CXF servlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<!-- If you want to leverage the Servlet3's async feature in Tomcat,
please enable this feature
<async-supported>true</async-supported>
-->
</servlet>
<!-- all our webservices are mapped under this URI pattern -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
</web-app>web.xml file are:
servlet/servlet-class- Specifies the
org.apache.cxf.transport.servlet.CXFServletclass, which implements a special servlet that enables you to deploy Apache CXF WS endpoints. servlet-mapping/url-pattern- Determines which URLs are routed to this servlet. In general, the servlet URL has the following form:
http://Host:Port/WARFileName/URLPattern
Where the base URL,http://Host:Port, is determined by the configuration of the Web server, theWARFileNameis the root of theWARFileName.warWAR file, and theURLPatternis specified by the contents of theurl-patternelement.Assuming that the Web server port is set to 8080, thecamel-example-cxf-tomcatexample servlet will match URLs of the following form:http://localhost:8080/camel-example-cxf-tomcat/webservices/*
listener/listener-class- This element launches a Spring container.
context-param- This element specifies the location of the Spring XML file,
camel-config.xml, in the WAR. The Spring container will read this parameter and load the specified Spring XML file, which contains the definition of the Camel route.
listener-class element here, because the CXFServlet class already creates its own Spring container. If you put the Spring XML file in the location expected by the CXFServlet class (that is, WEB-INF/cxf-servlet.xml) instead of the location used by this example (that is, WEB-INF/classes/camel-config.xml), you could remove the Spring container settings from this web.xml file.
Spring XML file
camel-config.xml, contains the following XML code:
<?xml version="1.0" encoding="UTF-8"?>
<beans ...>
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<bean id="myRoutes" class="org.apache.camel.example.cxf.CamelRoute"/>
<camelContext xmlns="http://camel.apache.org/schema/spring">
<routeBuilder ref="myRoutes"/>
</camelContext>
</beans>RouteBuilder class is defined in the Java class, org.apache.camel.example.cxf.CamelRoute.
Camel route class
Example 6.2. Route Definitions in the CamelRoute Class
// Java
package org.apache.camel.example.cxf;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.example.cxf.incident.IncidentService;
import org.apache.camel.example.cxf.incident.InputReportIncident;
import org.apache.camel.example.cxf.incident.OutputReportIncident;
import org.apache.camel.example.cxf.incident.OutputStatusIncident;
// this static import is needed for older versions of Camel than 2.5
// import static org.apache.camel.language.simple.SimpleLanguage.simple;
public class CamelRoute extends RouteBuilder {
// CXF webservice using code first approach
private String uri = "cxf:/incident?serviceClass=" + IncidentService.class.getName();
@Override
public void configure() throws Exception {
from(uri)
.to("log:input")
// send the request to the route to handle the operation
// the name of the operation is in that header
.recipientList(simple("direct:${header.operationName}"));
// report incident
from("direct:reportIncident")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
// get the id of the input
String id = exchange.getIn().getBody(InputReportIncident.class).getIncidentId();
// set reply including the id
OutputReportIncident output = new OutputReportIncident();
output.setCode("OK;" + id);
exchange.getOut().setBody(output);
}
})
.to("log:output");
// status incident
from("direct:statusIncident")
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
// set reply
OutputStatusIncident output = new OutputStatusIncident();
output.setStatus("IN PROGRESS");
exchange.getOut().setBody(output);
}
})
.to("log:output");
}
}from(uri) DSL command). The Camel CXF endpoint is defined using the following endpoint URI:
cxf:/incident?serviceClass=org.apache.camel.example.cxf.incident.IncidentService
IncidentService class in this URI. The relative path, /incident, defines the tail of the servlet URL for this Web service. Hence, the full servlet URL for the Web service is the following:
http://localhost:8080/camel-example-cxf-tomcat/webservices/incidentserviceClass option specifies the name of the Service Endpoint Interface (SEI) for this Web service. By default, the CXF endpoint is set up to use the POJO mode, using the SEI to check the syntax of incoming messages.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.