Platform Engineering

A Groovy way to implement a JMS Request Response client using Camel

Posted by Admin on 16 September 2014

tech, camel, groovy, jms

While working for one particular client we were under the particular constraint of not having access to the JMS management console (in this instance the ActiveMQ Console). So when it came time to test out integration workflows we needed a quick and easy way to call the SOAP over JMS services. The Deloitte Platform Engineering folks are well versed in their Groovy scripting and came up with this awesome Groovy script:

I don’t think many people could argue that the above code could be made more compact. We certainly don’t think we could improve its succinctness - but we can certainly attempt to keep it almost as short and pack a lot of (pun intended) Groovy extras. Personally, I am a fan of the Apache Camel, so my immediate thought was what would this script look like with a Camel on it? We would gain the benefits of Camel, one of them being great logging (just add log4j.properties) - and also JMS replies and temporary queues are handled by the framework. Add a little, gain a lot.

Adding a Camel

The Groovy script allows you to specify a filepath and a header to send via std input. The Camel route then loads in the file and sends the contents as the body. The header value is added to the message as the SOAPJMS_soapAction.

By adding the following log4j.properties file in the same directory as the script:

  #simple console logger to show the jms replies from jms request-response pattern using camel
  log4j.rootLogger=INFO, out
   
  # uncomment the following line to turn on Camel debugging
  #log4j.logger.org.apache.camel=DEBUG
  #turn on DEBUG logging to see jms replies
  log4j.logger.org.apache.camel.component.jms.reply=DEBUG
   
  # CONSOLE appender
  log4j.appender.out=org.apache.log4j.ConsoleAppender
  log4j.appender.out.layout=org.apache.log4j.PatternLayout
  log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
view rawlog4j.properties hosted with ❤ by GitHub

we get the log output from the script as follows:

To complete the loop we have added the server side which will provide a response:

Some like it Hawt

Hawt.io is a web console which plays nice with Apache Camel. I extracted these images from it to show the client Groovy JMS and server Groovy JMS flows.

Hawt Camel Routes

Project Setup

Of course, getting to this point requires a fair bit of setup, you need to ensure you have the following installed along with a JDK:

  • Maven
  • Ivy
  • Groovy
  • ActiveMQ - you will need a local ActiveMQ broker running in order to run the scripts
  • Hawt.io - allows you to view and inspect your Camel routes (and your ActiveMQ queues)

To code and debug Groovy I find IntelliJ Community Editon very helpful, and the IvyIDEA Plugin allows seamless integration with Ivy in order to download the dependencies through the IDE. Just ensure you configure Groovy and Ivy in your global libraries.

I hope this has been a Groovy Camel ride, it gets even hawter with other Camel components. It goes to show that a lot of functionality can be packed into small scripts when you stand on the shoulders of open source giants. I extend my great thanks to the Camel open source crew.

Further Reading: