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

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


seven + = 9



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close