Platinum Partner
java,news,enterprise-integration,frameworks,apache camel

Camel 2.11 - Camel web applications without Spring

We at Fusesource have just added a new component to the upcoming Apache Camel 2.11, that allows you to run web applications with Camel without having to rely on Spring Framework to boostrap Camel. Or any other 3rd party frameworks for that matter.

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 showcase

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


CamelServletContextListener

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

Having the two examples

We can take a peak at the difference between the two of them, when deploying to fresh installation of Apache Tomcat 7.0.30 using Java 1.6. 
servlet-tomcat-example
size: 5.8 MB
jars: 19
classes loaded: 4507
current heap size: 10 MB
servlet-tomcat-no-spring-example
size: 2.9 MB
jars: 10
classes loaded: 3698
current heap size: 8 MB
I used jconsole to look at the number of loaded classes in the JVM with the Apache Tomcat and the deployed example. The heap size was measured as the lowest value I could get after performing GC.
So we cut 10 JARs and 3MB from the distribution. As well avoid loading in the shy of 1000 classes.

Remarks

I just want to emphasize that the goal of this new camel-servletlistener component is to allow people to easily bootstrap their Camel applications in any web container, without the need to be tied to any particular 3rd party framework such as Spring. The Apache Camel project aims to be neutral and agnostic; allowing people to run Camel in any way and with any container of their choice.

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}