Tuesday, October 21, 2008

Integrating Apache Camel with JBoss ESB


JBoss ESB supports the EIP(Enterprise Integration Patterns) in many aspects and counting with sofisticated resources, for instance: Drools for CBR(Content Based Router), or jBPM for Business Processor, besides of components for implementing Splitters, Aggregators, Filters and so on. However, not only to prove that JBoss ESB is an open and flexible ESB, I decided integrate it with Apache Camel, and this entry will show you some aspects of this work.

Little About Apache Camel

From Camel website (http://activemq.apache.org/camel/)

"Apache Camel is a Spring based Integration Framework which implements the Enterprise Integration Patterns with powerful Bean Integration.

Camel lets you create the Enterprise Integration Patterns to implement routing and mediation rules in either a Java based Domain Specific Language (or Fluent API), via Spring based Xml Configuration files or via the Scala DSL. This means you get smart completion of routing rules in your IDE whether in your Java, Scala or XML editor.

Apache Camel uses URIs so that it can easily work directly with any kind of Transport or messaging model such as HTTP, ActiveMQ, JMS, JBI, SCA, MINA or CXF Bus API together with working with pluggable Data Format options. Apache Camel is a small library which has minimal dependencies for easy embedding in any Java application."

In addition to this, Camel has several components that allows us apply an "Event-Driven" approach, the list of providers or protocols is really interesting, that's why Camel called my atention some months back, and working with a SOA project, where one of the requirements is listen many "non-common" protocols! So I believe that Camel can help me to avoid reinvent de wheel. Moreover, you can ask me: "Hey sir, tell me some about you bunch of components that you can use on Camel?", and I can answer with the following:
  • Apache Mina
  • RelaxNG
  • XMPP
  • Microsoft MQ
  • JCR
  • JBI
  • IRC
  • HL7
  • ATOM
  • Others , see more here

A Real Use Case: Turning On Camel into JBoss ESB

Depending of the point of view, you can definetely think that it is crappy, although I personally believe that Camel can work with JBoss ESB, once you have many events(Messages) happening on that protocols mapped by Camel's componets, you can eventually catch one of these messages(events) via Apache Camel, where we consider an "Unaware ESB Message from an Unaware Client" and publish it into a provider(protocol) that JBoss ESB supports, such as JMS, File System, WebServices, JBoss Remoting an so on. See the following image where I try describe visually this bahaviour:

In my case, I wanna listen everything what happens into a UDP server connection, and due to performance I would like to use something on top of NIO: So Apache Mina is one of my options, I could create a new JBoss ESB Courier(provider), new ProviderListener and everything from scratch, but I decided test Apache Camel, and my experience is that it was incredible cool,easy and under my performance testing really interesting, comparing with the previous UDP Server made using basic Java IO implementation.

First of all, I used the JBoss ESB's internal scheduler infrastructure, I enabled a SchedulerProvider to start my "Camel Listener", on this point I just wanna enable the Apache Camel run into ESB(inESB). See in the following code the provider configuration:

After this I created a listener for my service, just to tell which "Timer Event Listener" I wanna invoke when my CRON expression had been executed. In the following code, you can see my ESB Timer listener activating my UDP component listener based in Camel:

The class you can see in my listener is basically the timer listener processor that will be fired when the cron expression happens. In that case I am implementing the JBoss ESB's interface: org.jboss.soa.esb.schedule.ScheduledEventListener, especifically the method onSchedule(), which is fired my my listener according the CRON expression in my scheduler provider. In particular, on that method I invoke the Camel capabilities, as you can see in the following code:

The Camel portion is really easy to understand: This frameworks works with URI concept, so the compoenents "endpoints" no matter for input or output are configured using those URI and a Java DSL for it, in the example you can see above, We hve a method process which is resposible for handling the message/information that arrives in the configured component(endpoint), in that case I am telling that this method will listen eveything that happens in a UDP server in my host in the port 2222, so the message that I receive I can do everything I want with, and after I route this information for another component, in that case we are forwarding the message to a filesystem, in that case a Filesystem can be a gateway for JBoss ESB, but we could route the info for a JMS or HTTP(JBR) gateway as well.

I believe I can improve a lot the integration between JBoss ESB and Camel, there are some issues, like redeployments in ESB and CamelContexts stoping and restarting that I can synchronize, but for awhile it is working pretty well.

I hope this post can be useful for you, if you need any further information please comment here.

More info:





James Strachan said...

Cool! :)

I've added a link to the Camel Articles page...


it'll show up on the main site soon when apache's web cache updates...

Edgar Silva said...

Thanks James!