Enterprise Java

Camel 2.11 – Camel web applications without Spring

As I write this we have just added a new component to the upcoming Apache Camel 2.11, that allow 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.

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 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 as well ensure resources is 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 dont 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 container. There is a ton of them out there running. And even so the old school configuration with a web.xml file works just as good 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 what we did was to redo this example without the need for Spring.

The two examples is 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 container of choice.
 

Reference: Camel 2.11 – Camel web applications without Spring from our JCG partner Claus Ibsen at the Claus Ibsen riding the Apache Camel blog.

Claus Ibsen

Claus Ibsen is a principal software engineer from Red Hat. Claus is working full time as Apache Camel committer. And is author of the "Camel in Action" book.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button