Connect a Micronaut JMS Application to an AWS SQS Queue

Learn how to connect JMS Application to an AWS SQS Queue

Authors: Slavko Bodvanski

Micronaut Version: 4.3.8

1. Getting Started

In this guide, we will create a Micronaut application written in Kotlin.

2. What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • A decent text editor or IDE (e.g. IntelliJ IDEA)

  • JDK 17 or greater installed with JAVA_HOME configured appropriately

  • An AWS account with:

    • An IAM user with enough permissions to create and manage a queue instances in SQS.

    • The AWS CLI configured to use the IAM user above.

3. Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

4. Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide \
    --features=jms-sqs,localstack,awaitility \
    --build=maven \
    --lang=kotlin \
    --test=junit
If you don’t specify the --build argument, Gradle is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

If you use Micronaut Launch, select Micronaut Application as application type and add jms-sqs, localstack, and awaitility features.

If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features and apply those changes to your application.

5. Create an application

Let’s create a set of components that will use the Micronaut JMS to send and receive messages from AWS SQS

Amazon SQS is a reliable, highly-scalable hosted queue for storing messages as they travel between applications or microservices. Amazon SQS moves data between distributed application components and helps you decouple these components.

5.1. Configuration

Enable JMS SQS integration:

src/main/resources/application.properties
micronaut.jms.sqs.enabled=true

5.2. Creating a JMS Producer

Create a JMS Producer interface.

src/main/kotlin/example/micronaut/DemoProducer.kt
package example.micronaut

import io.micronaut.jms.annotations.JMSProducer
import io.micronaut.jms.annotations.Queue
import io.micronaut.jms.sqs.configuration.SqsConfiguration.CONNECTION_FACTORY_BEAN_NAME
import io.micronaut.messaging.annotation.MessageBody

@JMSProducer(CONNECTION_FACTORY_BEAN_NAME)  (1)
interface DemoProducer {

    @Queue("demo_queue")  (2)
    fun send(@MessageBody body: String?)  (3)
}
1 The JMSProducer annotation defines this interface as a client that sends messages.
2 The @Queue annotation indicates which queue the message should be published to.
3 The send method accepts a single parameter which is the payload of a message.

5.3. Creating a JMS Consumer

Create a JMS Consumer class.

src/main/kotlin/example/micronaut/DemoConsumer.kt
package example.micronaut

import io.micronaut.jms.annotations.JMSListener
import io.micronaut.jms.annotations.Queue
import io.micronaut.jms.sqs.configuration.SqsConfiguration.CONNECTION_FACTORY_BEAN_NAME
import io.micronaut.messaging.annotation.MessageBody
import org.slf4j.LoggerFactory
import java.util.concurrent.atomic.AtomicInteger

@JMSListener(CONNECTION_FACTORY_BEAN_NAME) (1)
class DemoConsumer {

    private val messageCount = AtomicInteger(0)

    @Queue(value = "demo_queue") (2)
    fun receive(@MessageBody body: String?) {  (3)
        LOG.info("Message has been consumed. Message body: {}", body)
        messageCount.incrementAndGet()
    }

    fun getMessageCount() = messageCount.toInt()

    companion object {
        private val LOG = LoggerFactory.getLogger(DemoConsumer::class.java)
    }
}
1 The @JMSListener defines the bean as a message listener.
2 The @Queue annotation indicates which queue to subscribe to.
3 The receive method accepts a single parameter which is the payload of a message.

5.4. Creating a Controller

Let’s create a Controller with an endpoint that we will call to verify that message has been sent by the JMS Producer (DemoProducer) and then finally received and consumed by the JMS Consumer (DemoConsumer).

src/main/kotlin/example/micronaut/DemoController.kt
package example.micronaut

import io.micronaut.http.HttpStatus
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Post
import io.micronaut.http.annotation.Status

@Controller  (1)
class DemoController(private val demoProducer: DemoProducer) {  (2)

    @Post("/demo")  (3)
    @Status(HttpStatus.NO_CONTENT)
    fun publishDemoMessages() {
        demoProducer.send("Demo message body")  (4)
    }
}
1 The class is defined as a controller with the @Controller annotation mapped to the path /.
2 Use constructor injection to inject a bean of type DemoProducer.
3 Maps a GET request to /demo path, which attempts to publish a message to a SQS queue instance.
4 Calls send method on DemoProducer instances providing the message payload.

6. Testing

To test, we will use LocalStack.

Develop and test your AWS applications locally to reduce development time and increase product velocity. Reduce unnecessary AWS spend and remove the complexity and risk of maintaining AWS dev accounts.

6.1. LocalStack Dependencies

Add the following dependencies to your test classpath:

pom.xml
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>localstack</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers</artifactId>
    <scope>test</scope>
</dependency>

First, we create a configuration properties object to encapsulate the configuration of the SQS client, which we will provide via LocalStack.

src/test/kotlin/example/micronaut/SqsConfig.kt
package example.micronaut

import io.micronaut.context.annotation.ConfigurationBuilder
import io.micronaut.context.annotation.ConfigurationProperties

@ConfigurationProperties("aws") (1)
class SqsConfig {

    var accessKeyId: String? = null
    var secretKey: String? = null
    var region: String? = null

    @ConfigurationBuilder(configurationPrefix = "services.sqs")
    val sqs: Sqs = Sqs()

    class Sqs {
        var endpointOverride: String? = null
    }
}
1 The @ConfigurationProperties annotation takes the configuration prefix.

Create a BeanCreatedEventListener to override the SQS client endpoint with the LocalStack endpoint.

src/test/kotlin/example/micronaut/SqsClientBuilderListener.kt
package example.micronaut

import io.micronaut.context.event.BeanCreatedEvent
import io.micronaut.context.event.BeanCreatedEventListener
import jakarta.inject.Singleton
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.sqs.SqsClientBuilder
import java.net.URI
import java.net.URISyntaxException

@Singleton (1)
class SqsClientBuilderListener(private val sqsConfig: SqsConfig) (3)
    : BeanCreatedEventListener<SqsClientBuilder> { (2)

    override fun onCreated(event: BeanCreatedEvent<SqsClientBuilder>): SqsClientBuilder {
        val builder = event.bean
        try {
            return builder
                .endpointOverride(URI(sqsConfig.sqs.endpointOverride!!))
                .credentialsProvider(
                    StaticCredentialsProvider.create(
                        AwsBasicCredentials.create(sqsConfig.accessKeyId, sqsConfig.secretKey)
                    )
                )
                .region(Region.of(sqsConfig.region))
        } catch (e: URISyntaxException) {
            throw RuntimeException(e)
        }
    }
}
1 Use jakarta.inject.Singleton to designate a class as a singleton.
2 Creating a @Singleton that implements BeanCreatedEventListener allows you to provide extra configuration to beans after creation.
3 Use constructor injection to inject a bean of type SqsConfig.

Create a BeanCreatedEventListener to create a SQS queue named demo_queue.

src/test/kotlin/example/micronaut/SqsClientCreatedEventListener.kt
package example.micronaut

import io.micronaut.context.event.BeanCreatedEvent
import io.micronaut.context.event.BeanCreatedEventListener
import jakarta.inject.Singleton
import software.amazon.awssdk.services.sqs.SqsClient
import software.amazon.awssdk.services.sqs.model.CreateQueueRequest

@Singleton (1)
class SqsClientCreatedEventListener : BeanCreatedEventListener<SqsClient> { (2)

    override fun onCreated(event: BeanCreatedEvent<SqsClient>): SqsClient {
        val client = event.bean
        if (client.listQueues().queueUrls().stream().noneMatch { it: String -> it.contains(QUEUE_NAME) }) {
            client.createQueue(
                CreateQueueRequest.builder()
                    .queueName(QUEUE_NAME)
                    .build()
            )
        }
        return client
    }

    companion object {
        private const val QUEUE_NAME = "demo_queue"
    }
}
1 Use jakarta.inject.Singleton to designate a class as a singleton.
2 Creating a @Singleton that implements BeanCreatedEventListener allows you to provide extra configuration to beans after creation.

Create a test using the Testcontainers LocalStack module to start a LocalStack container and verify that the message has been sent and received.

src/test/kotlin/example/micronaut/MicronautguideTest.kt
package example.micronaut

import io.micronaut.core.annotation.NonNull
import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import io.micronaut.test.support.TestPropertyProvider
import jakarta.inject.Inject
import org.awaitility.Awaitility
import org.hamcrest.Matchers
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.testcontainers.containers.localstack.LocalStackContainer
import org.testcontainers.utility.DockerImageName

@MicronautTest (1)
@TestInstance(TestInstance.Lifecycle.PER_CLASS) (2)
internal class MicronautguideTest : TestPropertyProvider { (3)

    @Inject
    @field:Client("/")
    lateinit var httpClient : HttpClient

    @Inject
    lateinit var demoConsumer: DemoConsumer

    override fun getProperties(): @NonNull MutableMap<String, String> {
        if (!localstack.isRunning) {
            localstack.start()
        }
        return mapOf(
            "aws.access-key-id" to localstack.accessKey,
            "aws.secret-key" to localstack.secretKey,
            "aws.region" to localstack.region,
            "aws.services.sqs.endpoint-override" to localstack.getEndpointOverride(LocalStackContainer.Service.SQS)
                .toString()
        ).toMutableMap()
    }

    @Test
    fun testItWorks() {
        Assertions.assertEquals(0, demoConsumer.getMessageCount())
        httpClient.toBlocking().exchange<Map<Any, Any>, Any>(HttpRequest.POST("/demo", emptyMap()))
        Awaitility.await().until({ demoConsumer.getMessageCount() }, Matchers.equalTo(1))
        Assertions.assertEquals(1, demoConsumer.getMessageCount())
    }

    companion object {
        private val localstackImage: DockerImageName = DockerImageName.parse("localstack/localstack:latest")
        private val localstack: LocalStackContainer = LocalStackContainer(localstackImage)
            .withServices(LocalStackContainer.Service.SQS)
    }
}
1 Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info.
2 Classes that implement TestPropertyProvider must use this annotation to create a single class instance for all tests (not necessary in Spock tests).
3 When you need dynamic properties definition, implement the TestPropertyProvider interface. Override the method .getProperties() and return the properties you want to expose to the application.

7. Amazon Web Services (AWS)

If you don’t have one already, create an AWS Account.

7.1. AWS CLI

Follow the AWS documentation for installing or updating the latest version of the AWS CLI.

7.2. Administrator IAM user

Instead of using your AWS root account, it is recommended that you use an IAM administrative user. If you don’t have one already, follow the steps below to create one:

aws iam create-group --group-name Administrators
aws iam create-user --user-name Administrator
aws iam add-user-to-group --user-name Administrator --group-name Administrators
aws iam attach-group-policy --group-name Administrators --policy-arn $(aws iam list-policies --query 'Policies[?PolicyName==`AdministratorAccess`].{ARN:Arn}' --output text)
aws iam create-access-key --user-name Administrator

Then, run aws configure to configure your AWS CLI to use the Administrator IAM user just created.

8. Creating a queue instance in Amazon Simple Queue Service (Amazon SQS)

You will create a queue with the AWS CLI. See the AWS CLI sqs command for more information.

8.1. Create a queue instance

aws sqs create-queue --queue-name demo_queue

Copy and save the response of the command. You will need the QueueUrl to delete the queue after you finish with it.

9. Running the Application

With almost everything in place, you can start the application and try it out. First, set environment variables to configure the queue connection. Then you can start the app.

Create environment variables for AWS_ACCESS_KEY_ID, and AWS_SECRET_ACCESS_KEY, as defined in AWS SDK Java v2 - Credential settings retrieval order:

export AWS_ACCESS_KEY_ID=<the access key from the AWS configuratipn step>
export AWS_SECRET_ACCESS_KEY=<the secret key from the AWS configuratipn step>
Window System
Command Prompt

Change 'export' to 'set'

Example: set AWS_ACCESS_KEY_ID=aws_access_key

PowerShell

Change 'export ' to '$' and use quotes around the value

Example: $AWS_ACCESS_KEY_ID="aws_access_key"

To run the application, use the ./mvnw mn:run command, which starts the application on port 8080.

You can test the application in a web browser or with cURL.

Run from a terminal window to publish and consume a message:

curl "http://localhost:8080/demo"

9.1. Stopping the Instance and cleaning up

Once you are done with this guide, you can stop/delete the AWS resources created to avoid incurring unnecessary charges.

aws sqs delete-queue --queue-url <QUEUE_URL>

Replace the <QUEUE_URL> placeholder with a queue URL value returned from the create-queue command.

10. Generate a Micronaut Application Native Executable with GraalVM

We will use GraalVM, the polyglot embeddable virtual machine, to generate a native executable of our Micronaut application.

Compiling native executables ahead of time with GraalVM improves startup time and reduces the memory footprint of JVM-based applications.

Only Java and Kotlin projects support using GraalVM’s native-image tool. Groovy relies heavily on reflection, which is only partially supported by GraalVM.

10.1. GraalVM installation

The easiest way to install GraalVM on Linux or Mac is to use SDKMan.io.

Java 17
sdk install java 17.0.8-graal
Java 17
sdk use java 17.0.8-graal

For installation on Windows, or for manual installation on Linux or Mac, see the GraalVM Getting Started documentation.

The previous command installs Oracle GraalVM, which is free to use in production and free to redistribute, at no cost, under the GraalVM Free Terms and Conditions.

Alternatively, you can use the GraalVM Community Edition:

Java 17
sdk install java 17.0.8-graalce
Java 17
sdk use java 17.0.8-graalce

10.2. Native executable generation

To generate a native executable using Maven, run:

./mvnw package -Dpackaging=native-image

The native executable is created in the target directory and can be run with target/micronautguide.

Start the native executable and execute the same cURL request as before.

11. Next steps

Explore more features with Micronaut Guides.

Read more about Micronaut JMS.

12. License

All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…​).