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

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

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:

You might also enjoy:

Dock Tales: Docker Authoring, with Special Guest Mule ESB 30 March 2015

Ansible Crash Course 09 March 2016

Microservices with Apache Camel, Spring Boot and Docker 31 March 2016

Developing Bulk APIs with Mule, RAML and APIKit 02 December 2014

Jumpstarting Camel Blueprint Testing 09 March 2016

comments powered by Disqus