...
Maven users will need to add the following dependency to their pom.xml
for this component:
Code Block |
|
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servletlistener</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
|
...
By default Camel will assume the param-value is a FQN classname for a Camel RouteBuilder class, as shown below:
Code Block |
|
<context-param>
<param-name>routeBuilder-MyRoute</param-name>
<param-value>org.apache.camel.component.servletlistener.MyRoute</param-value>
</context-param>
|
You can specify multiple classes in the same param-value as shown below:
Code Block |
|
<context-param>
<param-name>routeBuilder-routes</param-name>
<!-- we can define multiple values separated by comma -->
<param-value>
org.apache.camel.component.servletlistener.MyRoute,
org.apache.camel.component.servletlistener.routes.BarRouteBuilder
</param-value>
</context-param>
|
...
You can also tell Camel to use package scanning, which mean it will look in the given package for all classes of RouteBuilder types and automatic adding them as Camel routes. To do that you need to prefix the value with "packagescan:" as shown below:
Code Block |
|
<context-param>
<param-name>routeBuilder-MyRoute</param-name>
<!-- define the routes using package scanning by prefixing with packagescan: -->
<param-value>packagescan:org.apache.camel.component.servletlistener.routes</param-value>
</context-param>
|
...
You can also define Camel routes using XML DSL, though as we are not using Spring or Blueprint the XML file can only contain Camel route(s).
In the web.xml you refer to the XML file which can be from "classpath", "file" or a "http" url, as shown below:
Code Block |
|
<context-param>
<param-name>routeBuilder-MyRoute</param-name>
<param-value>classpath:routes/myRoutes.xml</param-value>
</context-param>
|
And the XML file is:
Code Block |
title |
routes/myRoutes.xml |
xml:title |
routes/myRoutes.xml |
|
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- the xmlns="http://camel.apache.org/schema/spring" is needed -->
<routes xmlns="http://camel.apache.org/schema/spring">
<route id="foo">
<from uri="direct:foo"/>
<to uri="mock:foo"/>
</route>
<route id="bar">
<from uri="direct:bar"/>
<to uri="mock:bar"/>
</route>
</routes>
|
...
Here is a snippet of a web.xml configuration for setting up property placeholders to load myproperties.properties
from the classpath
Code Block |
|
<!-- setup property placeholder to load properties from classpath -->
<!-- we do this by setting the param-name with propertyPlaceholder. as prefix and then any options such as location, cache etc -->
<context-param>
<param-name>propertyPlaceholder.location</param-name>
<param-value>classpath:myproperties.properties</param-value>
</context-param>
<!-- for example to disable cache on properties component, you do -->
<context-param>
<param-name>propertyPlaceholder.cache</param-name>
<param-value>false</param-value>
</context-param>
|
...
Here is a snippet of a web.xml configuration for configuring JMX, such as disabling JMX.
Code Block |
|
<!-- configure JMX by using names that is prefixed with jmx. -->
<!-- in this example we disable JMX -->
<context-param>
<param-name>jmx.disabled</param-name>
<param-value>true</param-value>
</context-param>
|
...
Then we need to register this class in the web.xml file as shown below, using the parameter name "CamelContextLifecycle". The value must be a FQN which refers to the class implementing the org.apache.camel.component.servletlistener.CamelContextLifecycle
interface.
Code Block |
|
<context-param>
<param-name>CamelContextLifecycle</param-name>
<param-value>org.apache.camel.component.servletlistener.MyLifecycle</param-value>
</context-param>
|
As we enlisted our HelloBean Bean using the name "myBean" we can refer to this Bean in the Camel routes as shown below:
Code Block |
|
public class MyBeanRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("seda:foo").routeId("foo")
.to("bean:myBean")
.to("mock:foo");
}
}
|
Important: 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
See Also