Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the asciidoc in the repository: https://github.com/apache/camel/blob/master/README.md https://github.com/apache/camel/blob/master/components/readme.adoc
Servlet Tomcat No Spring ExampleAvailable as of Camel 2.11 This example is located in the If you use maven then you can easily package the example from the command line: mvn package AboutThis example demonstrates how you can create light-weight web applications without the need for Spring Framework. ImplementationIn the
<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>
<!-- you can configure any of the properties on CamelContext, eg setName will be configured as below -->
<context-param>
<param-name>name</param-name>
<param-value>MyCamel</param-value>
</context-param>
<!-- location of Camel route xml files -->
<context-param>
<param-name>routeBuilder-MyRoute</param-name>
<!-- define the routes as a resource from the classpath by prefixing the value with classpath: -->
<!-- note: instead of using a XML file we can also define the routes in Java code in a RouteBuilder class -->
<param-value>classpath:camel-config.xml</param-value>
</context-param>
<context-param>
<!-- define a lifecycle which could help user to setup the registry which could be use as a reference in camel route -->
<!-- If you use org.apache.camel.component.servletlistener.JndiCamelServletContextListener then the CamelContextLifecycle
must use the JndiRegistry as well. And likewise if the servlet is org.apache.camel.component.servletlistener.SimpleCamelServletContextListener
then the CamelContextLifecycle must use the SimpleRegistry -->
<param-name>CamelContextLifecycle</param-name>
<param-value>org.apache.camel.example.servletlistener.MyLifecycle</param-value>
</context-param>
<!-- the listener that kick-starts Camel -->
<listener>
<listener-class>org.apache.camel.component.servletlistener.JndiCamelServletContextListener</listener-class>
</listener>
<!-- Camel servlet used in the Camel application -->
<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>
Its the ServletListener Component that is used to bootstrap Camel in the web application. See more details at ServletListener Component. The Camel routeThe route is a simple Content Based Router defined in the DSL XML as shown: Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<!-- here we have the Camel route(s). -->
<!-- we must still use the http://camel.apache.org/schema/spring namespace so Camel can load the routes
though Spring JARs is not required -->
<routes 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 -->
<to uri="bean:myBean"/>
</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>
</routes>
Running the exampleThis example runs in any web container such as Apache Tomcat. For example to deploy in Apache Tomcat you will have to package the .war file and copy it to the There is a main page at: See Also |
