Introducing the new OpenCredo AMQ project - No Fluff Just Stuff

Introducing the new OpenCredo AMQ project

Posted by: Russell Miles on January 26, 2010

Today I have the immense pleasure of announcing the launch of the OpenCredo AMQ project. From the project announcement page:

"The goal of OpenCredo AMQ project is to facilitate communication between Java applications and AMQ servers (brokers)."


Headed up by OpenCredo founder Jonas Partner, our team have developed the following key features in this first version of the code:

  • AmqTemplate - This template, in the style of Spring's Template approach (JDBCTemplate for example), simplifies the use of AMQ from Java.
  • Spring Integration Support - Provides Spring Integration channel adapters and introduces a custom namespace to provide simple, configuration-based use of AMQ from your Spring Integration-based integration architecture.


The project is currently distributed as a Zip, Tar or is available from a Maven repository (or, of course, build from source)

 

Getting stuck in with AmqTemplate

 

A quick glance at the project's integration tests will give you a feel for how to use AmqTemplate. To get started, the first things you will have to do is get an AMQ broker running. AmqTemplate is tested against both QPid (0.5) and RabbitMQ 1.7.0 (1.7.1 should also work, but it hasn't been tested at this point in time) so head on over to their sites and get yourself a broker installed according to their instructions.

Once you have an AMQ broker up and running it's time to configure and use your AmqTemplate. To use a local broker you'd do this using regular Spring configuration like the following snippet.

    <bean id="rabbitMqTemplateBean"
                class="org.opencredo.amq.rabbitmq.RabbitMqTemplate">
        <constructor-arg ref="channelFactoryBean" />
    </bean>

    <bean id="channelFactoryBean"
        class="org.opencredo.amq.rabbitmq.DefaultChannelFactory">
        <constructor-arg ref="connectionFactoryBean" />
    </bean>

    <bean id="connectionFactoryBean"
 class="org.opencredo.amq.rabbitmq.SingleConnectionConnectionFactory">
        <constructor-arg value="localhost" />
    </bean>


The amqTemplate bean is then injected into any class that needs to interact with AMQ. In the following code snippet you can see the AmqTemplate being used to asynchronously send a simple string with an encoded timestamp and then receive it back.

   String routingKey = "DefaultExchangeTest";
   
    String msg = this.getClass().getSimpleName() + " msg: "
                    + (new Timestamp(

                                   System.currentTimeMillis())).toString();
   
    amqTemplate.convertAndSend(routingKey, msg);

    Object result = amqTemplate.receiveAndConvert(routingKey);

More samples can be seen in the org.opencredo.amq.amq-template project tests in the projects source.

"Spring" the AMQ integration

 

With Jonas being a committer on the Spring Integration project and project lead on OpenCredo AMQ, it was kinda inevitable that first-class support for this light-weight approach to implementing integration architecture in your applications would be provided.

All the options require a blog post of their very own to get into in detail, but to give you a taste of how natural this integration is the following sample shows a Spring Integration pipeline that uses our new AMQ namespace support to create a binding between a specified routing key and a particular AMQ queue, which then enables an AMQ outbound channel adapter to send messages from a Spring Integration channel to a specific AMQ exchange.

    <!-- AMQ config -->
    <beans:bean id="amqTemplate"
        class="org.opencredo.amq.rabbitmq.RabbitMqTemplate">
        <beans:constructor-arg  ref="amqChannelFactory" />
    </beans:bean>

    <beans:bean id="amqChannelFactory"
        class="org.opencredo.amq.rabbitmq.DefaultChannelFactory">
        <beans:constructor-arg  ref="amqConnectionFactory" />
    </beans:bean>

    <beans:bean id="amqConnectionFactory"
        class="org.opencredo.amq.rabbitmq.SingleConnectionConnectionFactory">
        <beans:constructor-arg value="localhost" />
    </beans:bean>

    <!--
        Defines binding between routing-key 'amq-key' and AMQP queue -
        'amq-queue'.
    -->
    <amq:amq-binding id="amqBinding"
                                    routing-key="amq-key">
        <amq:queue name="amq-queue" />
    </amq:amq-binding>

    <!-- Defines AMQP exchange of 'direct' type and its binding. -->
    <amq:amq-exchange id="amqExchange" type="direct">
        <amq:binding ref="amqBinding" />
    </amq:amq-exchange>

    <!--
        This is SI-AMQ adapter which sends message from SI channel
        'messagesChannel' to 'amqExchange' exchange.
    -->
    <amq:outbound-channel-adapter
        default-routing-key="amq-key"
        exchange="amqExchange"

        template="amqTemplate"
        channel="messagesChannel" />


That was just a taster and if you can't wait for the next blog post to see more check out the org.opencredo.amq.amq-samples project in the source.

 

Summary

 

OpenCredo AMQ makes it easier to play well with AMQ from both regular Java and Spring Integration environments.

And the development has not stopped there. The next version of the project, as can be seen from the project JIRA, plans to incorporate transaction management with the use of an AMQTransactionManager implementation.

 

Interested in getting involved?

 

OpenCredo is committed to open source and as such this project is a typical open source offering where anyone is encouraged to grab the code and get involved by submitting bug reports and patches through the project JIRA.

Russell Miles

About Russell Miles

A self-confessed polyglot programmer, Russ Miles is a senior consultant for SpringSource in the UK where he works with various companies to help them take full advantage of the Spring Framework. To ensure that he has as little spare time as possible, Russ contributes to various open source projects and has authored a number of books.

Russ is a keen contributor to open source projects and an author for O'Reilly Media. He has authored and co-authored 3 books; “AspectJ Cookbook”, “Learning UML 2.0” and “Head First Software Development”

Prior to joining SpringSource, Russ gained experience of enterprise development throughout all tiers of application architecture including high performance and usability presentation tier services for the Search and Mobile Portal industries right through to maximum availability application and data services for the Defence industry.

Russ holds an MSc. Software Engineering from Oxford University.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »