When your web application contains a number of services or microservices, you may need them to communicate with each other. This is usually achieved using REST or message queues.
A message queue(MQ) is a software component used for passing data (messages) between services. It exposes an interface for adding, reading, and removing messages.
There are a lot of different MQ systems. If you use Amazon Web Services(AWS) platform one of the most simple and cheap in most cases MQ solution is Amazon Simple Message Queue Service (SQS)
Lets try to use SQS in Grails app.
In AWS need to create:
Add queue name and credentials to application.yml:
Then we must add following dependencies to build.gradle
The Java SDK for SQS is compatible with JMS. Thanks to the fact that the Grails are built on top of Spring we can integrate it with the JMS integration framework that Spring provides.
Next, we will configure Grails with Spring JMS and Amazon SQS by defining a Spring Configuration class:
The @EnableJms annotation will make Spring JMS scan for JMS listeners defined in the source code and use them with the beans in the configuration.
Also we to add @ComponentScan into Application.groovy :
This will scan the package of grails.aws.sqs.example.config for any Spring components.
After all done we can create methods to send and receive messages
The @JmsListener annotation from configuration class marks this method as the target of a JMS message listener.
Destination is queue name that we were set in application.yml
Thats all. It is quite simple to use Amazon SQS and JMS in Grails app, isn’t is? But if we dig deeper into SQS and JMS capabilities – there are big number of different settings that will help to set up message queue processing to any your needs.