Camel 2.11 - Camel web applications without Spring
Join the DZone community and get the full member experience.Join For Free
I'm a bit ashamed to say that in the year 2013 we are a bit late to the party to offer this out of the box in Apache Camel.
In modern web containers supporting the Servlet 3.0 spec you can programmatically bootstrap your code using the new @WebListener annotation. But even so there is still work to be done, as you would need to setup and configure Camel and ensure resources are unregistered and stopped when stopping the web application.
I have not found a neat way of using @WebListener and allowing external configuration of your framework such as Apache Camel. I don't really want to hardcode my configuration, or have to invent my own way of loading external configuration and configure my application. Anyone know of a solution to this problem?
Anyway we also wanted a solution that works for old school Servlet 2.x based containers. There are a ton of them out there running. Even so, the old school configuration with a web.xml file works just as well in the newer Servlet 3.0 containers. So we wanted an out of the box solution that fit both worlds.
Example to showcaseWe have a number of examples at Apache Camel, and one is a simple web application that is ready to be deployed as a .war file in Apache Tomcat, or any other web container.
This example is using the Spring Framework to boostrap a Spring XML file, where we have Camel embedded. So we decided to reproduce this example without the need for Spring.
The two examples are documented at the Apache Camel site at:
CamelServletContextListenerIn Camel 2.11 we have a new component named camel-servletlistener, which offers a new CamelServletContextListener class which you configure in the web.xml file. This will then boostrap a CamelContext, and use the context init parameters as its configuration.
This context listener has a lot of configuration, so you can easily configure Camel to your needs.
The options is listed in the Camel docs at: http://camel.apache.org/servletlistener-component.html
And in case you need to execute any custom logic when Camel is starting/stopping then we have an API for that, a CamelContextLifecycle interface. There is more details and examples at the Camel docs.
To see this in action, I suggest after reading this blog, check out the new example, as well the documentation for the camel-servletlistener component.
Dissecting the exampleHaving the two examples
Published at DZone with permission of Claus Ibsen, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.