ServletListener ComponentPage edited by Claus IbsenChanges (11)
Full ContentServletListener ComponentAvailable 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.
Maven users will need to add the following dependency to their pom.xml for this component: <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>
To use this you need to configure the org.apache.camel.component.servletlistener.CamelServletContextListener in the WEB-INF/web.xml file as shown below: <web-app> <!-- the test parameter is only to be used for unit testing --> <!-- you should not use this option for production usage --> <context-param> <param-name>test</param-name> <param-value>true</param-value> </context-param> <!-- 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> <!-- configure a route builder to use --> <!-- Camel will pickup any parameter names that start with routeBuilder (case ignored) --> <context-param> <param-name>routeBuilder-MyRoute</param-name> <param-value>org.apache.camel.component.servletlistener.MyRoute</param-value> </context-param> <!-- register Camel as a listener so we can bootstrap Camel when the web application starts --> <listener> <listener-class>org.apache.camel.component.servletlistener.SimpleCamelServletContextListener</listener-class> </listener> </web-app> OptionsThe org.apache.camel.component.servletlistener.CamelServletContextListener supports the following options which can be configured as context-param in the web.xml file.
ExamplesSee Servlet Tomcat No Spring Example. Configuring routesYou 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 classBy 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 scanningYou 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 fileYou 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). <context-param> <param-name>routeBuilder-MyRoute</param-name> <param-value>classpath:routes/myRoutes.xml</param-value> </context-param> And the XML file is: <?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> Notice that in the XML file the root tag is <routes> which must use the namespace "http://camel.apache.org/schema/spring". This namespace is having the spring in the name, but that is because of historical reasons, as Spring was the first and only XML DSL back in the time. At runtime no Spring JARs is needed. Maybe in Camel 3.0 the namespace can be renamed to a generic name. Configuring propert placeholdersHere 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 JMXHere 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> JNDI or Simple as Camel RegistryThis component uses either JNDI or Simple as the Registry. This is done from Java code by implementing the org.apache.camel.component.servletlistener.CamelContextLifecycle. Using custom CamelContextLifecycleIn the code below we use the callbacks beforeStart and afterStop to enlist our custom bean in the Simple Registry, and as well to cleanup when we stop. /** * Our custom {@link CamelContextLifecycle} which allows us to enlist beans in the {@link JndiContext} * so the Camel application can lookup the beans in the {@link org.apache.camel.spi.Registry}. * <p/> * We can of course also do other kind of custom logic as well. */ public class MyLifecycle implements CamelContextLifecycle<SimpleRegistry> { @Override public void beforeStart(ServletCamelContext camelContext, SimpleRegistry registry) throws Exception { // enlist our bean(s) in the registry registry.put("myBean", new HelloBean()); } @Override public void afterStart(ServletCamelContext camelContext, SimpleRegistry registry) throws Exception { // noop } @Override public void beforeStop(ServletCamelContext camelContext, SimpleRegistry registry) throws Exception { // noop } @Override public void afterStop(ServletCamelContext camelContext, SimpleRegistry registry) throws Exception { // unbind our bean when Camel has been stopped registry.remove("myBean"); } } 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. <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: 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
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache Camel > ServletListener Component confluence
- [CONF] Apache Camel > ServletListener Component confluence
- [CONF] Apache Camel > ServletListener Component confluence
- [CONF] Apache Camel > ServletListener Component confluence
- [CONF] Apache Camel > ServletListener Component confluence
- [CONF] Apache Camel > ServletListener Component confluence
- [CONF] Apache Camel > ServletListener Component confluence