1 min read

Amazon Web Services SQS in Grails with Spring JMS

Amazon Web Services SQS in Grails with Spring JMS
Photo by Bundo Kim / Unsplash

I recently needed a quick and simple queue implementation for a Grails project but didn't want the hassle of maintaining an extra application like ApacheMQ or RabbitMQ.

Since this Grails project was already to be housed in Amazon EC2 I decided to pursue the use of AWS's SQS (Simple Queue Service).

Although some Grails SQS plugins already existed, none provided the ease of functionality that the ApacheMQ and the Grails JMS plugin would.

After a little internet research, I came across the Nevado JMS library that included a Spring JMS Provider for AWS SQS.

Once wired it up, it worked perfectly.

I started by including the required dependencies in my grails-app/conf/BuildConfig.groovy

...
dependencies {
    ...
    compile 'com.amazonaws:aws-java-sdk:1.4.1'
    compile 'org.skyscreamer:nevado-jms:1.1.1'
    ...
}
plugins {
    ...
    compile ":jms:1.2"
    ...
}
...

And then configured the Nevado JMS Provider in my grails-app/conf/spring/resources.groovy

beans = {
    sqsConnectorFactory(
 org.skyscreamer.nevado.jms.connector.typica.TypicaSQSConnectorFactory 
    )
    jmsConnectionFactory(
        org.skyscreamer.nevado.jms.NevadoConnectionFactory
    ) {
         sqsConnectorFactory = ref(sqsConnectorFactory)
         awsAccessKey = "YOUR_AWS_ACCESS_KEY"
         awsSecretKey = "YOUR_AWS_SECRET_KEY"
    }
}

That's about all the configuration there is.

You can then send messages using the JmsService...

import grails.plugin.jms.*

class SomeService {
    static exposes = ["jms"]
    JmsService jmsService
    String SQS_QUEUE_NAME = 'mySqsQueue'

    def sendMessage() {
        jmsService.send SQS_QUEUE_NAME, "This is my message!"
    }

    @Queue(name=SQS_QUEUE_NAME)
    def mySqsListener(msg) {
        println msg
    }
 }

The mySqsLister() is always listening to SQS for new messages.