You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

ServletListener Component

Available as of Camel 2.11

This component is used for bootstrapping Camel applications in web applications. For example beforehand people would have to find their own way of bootstrapping Camel, or rely on 3rd party frameworks such as Spring to do it.

There may be situations where you want a pure Camel based solution, without the extra overhead of Spring Framework.

Sidebar

This component works as a javax.servlet.ServletContextListener, which works in any web container of today, as well any older containers.
There may be solutions around the corner which is based on Servlet 3.x and annotation driven configuration, or using CDI or other means.
This component is old school and based on the pure API from Servlet 2.x, which means it works in any web container. Also the old ones, which was the goal.

TODO: Maven dependency

Using

To use this you need to configure the org.apache.camel.component.servletlistener.CamelServletContextListener in the WEB-INF/web.xml file as shown below:
TODO: myweb.xml

Options

The org.apache.camel.component.servletlistener.CamelServletContextListener supports the following options which can be configured as context-param in the web.xml file.

Option

Type

Description

propertyPlaceholder.XXX

 

To configure Using PropertyPlaceholder in Camel. You should prefix the option with "propertyPlaceholder.", for example to configure the location, use propertyPlaceholder.location as name. You can configure all the options from the Properties component.

jmx.XXX

 

To configure JMX. You should prefix the option with "jmx.", for example to disable JMX, use jmx.disabled as name. You can configure all the options from org.apache.camel.spi.ManagementAgent. As well the options mentioned on the JMX page.

name

String

To configure the name of the CamelContext.

streamCache

Boolean

Whether to enable Stream Caching.

trace

Boolean

Whether to enable Tracing.

delayer

Long

To set a delay value for Delay Interceptor.

handleFault

Boolean

Whether to enable handle fault.

errorHandlerRef

String

Refers to a context scoped Error Handler to be used.

autoStartup

Boolean

Whether to start all routes when starting Camel.

useMDCLogging

Boolean

Whether to use MDC Logging.

useBreadcrumb

Boolean

Whether to use Breadcrumb.

managementNamePattern

String

To set a custom naming pattern for JMX MBeans.

threadNamePattern

String

To set a custom naming pattern for threads.

properties.XXX

 

To set custom properties on CamelContext.getProperties. This is seldom in use.

routebuilder.XXX

 

To configure routes to be used. See below for more details.

CamelContextLifecycle

 

Refers to a FQN classname of a implementation of org.apache.camel.component.servletlistener.CamelContextLifecycle. Which allows to execute custom code before and after CamelContext has been started or stopped. See below for further details.

XXX

 

To set any option on CamelContext.

TODO: Div small table

Examples

See Servlet Tomcat No Spring Example.

Configuring routes

You need to configure which routes to use in the web.xml file. You can do this in a number of ways, though all the parameters must be prefixed with "routeBuilder".

Using a RouteBuilder class

By default Camel will assume the param-value is a FQN classname for a Camel RouteBuilder class, as shown below:

  <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:

  <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>

The name of the parameter does not have a meaning at runtime. It just need to be unique and start with "routeBuilder". In the example above we have "routeBuilder-routes". But you could just as well have named it "routeBuilder.foo".

Using package scanning

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:

  <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>

Using a XML file

Configuring propert placeholders

Here is a snippet of a web.xml configuration for setting up property placeholders to load myproperties.properties from the classpath

  <!-- 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>

Configuring JMX

Here is a snippet of a web.xml configuration for configuring JMX, such as disabling JMX.

  <!-- 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>

See Also

  • No labels