Chapter 5. Deploying an Apache Camel WS Endpoint
Abstract
This tutorial describes how to deploy an Apache CXF Web services endpoint in a WAR file, where the Web service endpoint is implemented by binding to an Apache Camel route using the Camel CXF component.
5.1. Apache Camel CXF Example Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Overview Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Figure 5.1, “Camel CXF Example Deployed in a Web Server” gives an overview of the Camel CXF example deployed in a Web server, which lets you see how the Web service's URL is constructed from settings at different configuration layers. The Web server's host and port, the WAR file name, the
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 5.1. Camel CXF Example Deployed in a Web Server
camel-example-cxf-tomcat example Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The code for this example is available from the standard Apache Camel distribution, under the
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The Camel CXF component binds an Apache CXF endpoint to a Camel route. That is, the Camel CXF endpoint itself is a fully-fledged Apache CXF Web service with all of the (potentially very complex) configuration options that are available from Apache CXF (including SSL security, WS-Security, and other WS-* standards). In contrast to the usual case, where you would bind the WS endpoint to a Java class (for example, using the JAX-WS binding), the Camel CXF component binds the WS endpoint to a Camel route, so that incoming SOAP message are encapsulated in a Camel
Exchange object, and can then propagate through the route.
To create a Camel CXF endpoint in a Camel route, define a CXF endpoint URI with either of the following syntaxes:
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
For more details about the Camel CXF component, please consult the following documents from the JBoss Fuse library:
- Web Services and Routing with Camel CXF
- The CXF chapter from the EIP Component Reference.
web.xml file Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
To deploy the Apache Camel CXF example, you must provide a properly configured
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
camel-example-cxf-tomcat/src/main/webapp/WEB-INF/web.xml
Example 5.1, “web.xml File for the camel-example-cxf-tomcat Example” shows the contents of the
web.xml file.
Example 5.1. web.xml File for the camel-example-cxf-tomcat Example
The key settings in the preceding
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
http://Host:Port/WARFileName/URLPatternCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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/*
http://localhost:8080/camel-example-cxf-tomcat/webservices/*Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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.
Note
Strictly speaking, it is not absolutely necessary to create a Spring container explicitly using the
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 Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The Spring XML file for this example,
camel-config.xml, contains the following XML code:
In this example, the Spring XML file is used just as a convenient mechanism to bootstrap the Camel context. The XML syntax is used to create the Camel context, but the code for the
RouteBuilder class is defined in the Java class, org.apache.camel.example.cxf.CamelRoute.
Note
The resource import in this file was required in earlier versions of Apache CXF, in order to import some standard, boilerplate definitions. But in recent versions of Apache CXF, this import is not required, and you can safely remove it from the Spring XML file.
Camel route class Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Example 5.2, “Route Definitions in the CamelRoute Class” shows the Camel routes for this example, defined using the Java DSL.
Example 5.2. Route Definitions in the CamelRoute Class
The most important feature of this class is the Camel CXF endpoint URI, which appears at the start of the first route (in the
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
cxf:/incident?serviceClass=org.apache.camel.example.cxf.incident.IncidentService
Where we have substituted the literal name of the
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/incident
http://localhost:8080/camel-example-cxf-tomcat/webservices/incident
The
serviceClass 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.