Java / Maven

Connect a Micronaut JMS Application to an AWS SQS Queue

Learn how to connect JMS Application to an AWS SQS Queue

Slavko Bodvanski
On this guide
In this section

Getting Started

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

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 21 or greater installed with JAVA_HOME configured appropriately

  • An AWS account with:

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

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

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.

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,testcontainers,testcontainers-floci,awaitility \
    --build=maven \
    --lang=java \
    --test=junit
Note
If you don’t specify the --build argument, Gradle with the Kotlin DSL 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, testcontainers, testcontainers-floci, and awaitility features.

Note
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.

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.

Configuration

Enable JMS SQS integration:

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

Creating a JMS Producer

Create a JMS Producer interface.

java/src/main/java/example/micronaut/DemoProducer.java

Creating a JMS Consumer

Create a JMS Consumer class.

java/src/main/java/example/micronaut/DemoConsumer.java

Creating a Controller

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

java/src/main/java/example/micronaut/DemoController.java

Testing

To test, we will use Floci.

Floci tests run through Docker and Testcontainers. The Floci container mounts the Docker daemon socket at /var/run/docker.sock, so run these tests only in trusted local or trusted CI Docker environments.

Floci Dependencies

To test with Floci, add the following dependencies:

pom.xml
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers-junit-jupiter</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>io.floci</groupId>
    <artifactId>testcontainers-floci</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 Floci.

java/src/test/java/example/micronaut/SqsConfig.java

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

java/src/test/java/example/micronaut/SqsClientBuilderListener.java

Create a BeanCreatedEventListener to create a SQS queue named demo_queue.

java/src/test/java/example/micronaut/SqsClientCreatedEventListener.java

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

java/src/test/java/example/micronaut/MicronautguideTest.java

Amazon Web Services (AWS)

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

AWS CLI

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

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.

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.

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.

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 configuration step>
export AWS_SECRET_ACCESS_KEY=<the secret key from the AWS configuration step>
Note
Windows 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"

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.

Generate a Micronaut Application Native Executable with GraalVM

We will use GraalVM, an advanced JDK with ahead-of-time Native Image compilation, to generate a native executable of this Micronaut application.

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

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

GraalVM Installation

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

Java 25
sdk install java 25.0.2-graal

For installation on Windows, or for a 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 25
sdk install java 25.0.2-graalce

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.

It is possible to customize the name of the native executable or pass additional build arguments using the Maven plugin for GraalVM Native Image building. Declare the plugin as follows:

pom.xml

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

Next Steps

Explore more features with Micronaut Guides.

Read more about Micronaut JMS.

License

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