Servlet Tomcat ExampleAvailable as of Camel 2.7 This example is located in the examples/camel-example-servlet-tomcat directory of the Camel distribution. If you use maven then you can easily package the example from the command line:
mvn package
AboutThis example demonstrates how you can use Servlet to expose a http service in a Camel route. ImplementationIn the web.xml file in the src/main/webapp/WEB-INF folder the CamelServlet is defined. This is mandatory to do when using the Servlet component. web.xml <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> <!-- Camel servlet --> <servlet> <servlet-name>CamelServlet</servlet-name> <servlet-class>org.apache.camel.component.servlet.CamelHttpTransportServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Camel servlet mapping --> <servlet-mapping> <servlet-name>CamelServlet</servlet-name> <url-pattern>/camel/*</url-pattern> </servlet-mapping> </web-app> The route is a simple Content Based Router defined in the DSL XML as shown: camel-config.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <!-- incoming requests from the servlet is routed --> <from uri="servlet:///hello"/> <choice> <when> <!-- is there a header with the key name? --> <header>name</header> <!-- yes so return back a message to the user --> <transform> <simple>Hello ${header.name} how are you?</simple> </transform> </when> <otherwise> <!-- if no name parameter then output a syntax to the user --> <transform> <constant>Add a name parameter to uri, eg ?name=foo</constant> </transform> </otherwise> </choice> </route> </camelContext> </beans> Running the exampleThis example runs in Apache Tomcat, so you will have to package the .war file and copy it to the webapp folder of Tomcat, which is the hot deploy folder. Note: You have to use the version number of Camel you use. In this documentation we are using 2.7.0. There is a main page at: http://localhost:8080/camel-example-servlet-tomcat-2.7.0 which has more instructions. See Also |