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

1. Getting Started

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

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 21 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=gradle \
    --lang=java \
    --test=junit
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, 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/java/example/micronaut/DemoProducer.java
package example.micronaut;

import io.micronaut.jms.annotations.JMSProducer;
import io.micronaut.jms.annotations.Queue;
import io.micronaut.messaging.annotation.MessageBody;

import static io.micronaut.jms.sqs.configuration.SqsConfiguration.CONNECTION_FACTORY_BEAN_NAME;

@JMSProducer(CONNECTION_FACTORY_BEAN_NAME) (1)
public interface DemoProducer {

    @Queue("demo_queue")   (2)
    void send(@MessageBody String body);  (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/java/example/micronaut/DemoConsumer.java
package example.micronaut;

import io.micronaut.jms.annotations.JMSListener;
import io.micronaut.jms.annotations.Queue;
import io.micronaut.messaging.annotation.MessageBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicInteger;

import static io.micronaut.jms.sqs.configuration.SqsConfiguration.CONNECTION_FACTORY_BEAN_NAME;

@JMSListener(CONNECTION_FACTORY_BEAN_NAME)  (1)
public class DemoConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(DemoConsumer.class);

    private final AtomicInteger messageCount = new AtomicInteger(0);

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

    int getMessageCount() {
        return messageCount.intValue();
    }
}
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/java/example/micronaut/DemoController.java
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)
public class DemoController {

    private final DemoProducer demoProducer;

    public DemoController(DemoProducer demoProducer) {  (2)
        this.demoProducer = demoProducer;
    }

    @Post("/demo") (3)
    @Status(HttpStatus.NO_CONTENT)
    public void 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:

build.gradle
testImplementation("org.testcontainers:junit-jupiter")
testImplementation("org.testcontainers:localstack")
testImplementation("org.testcontainers:testcontainers")

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

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

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

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

    private String accessKeyId;
    private String secretKey;
    private String region;
    @ConfigurationBuilder(configurationPrefix = "services.sqs")
    final Sqs sqs = new Sqs();

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public Sqs getSqs() {
        return sqs;
    }

    public static class Sqs {

        private String endpointOverride;

        public String getEndpointOverride() {
            return endpointOverride;
        }

        public void setEndpointOverride(String endpointOverride) {
            this.endpointOverride = endpointOverride;
        }
    }
}
1 The @ConfigurationProperties annotation takes the configuration prefix.

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

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

import io.micronaut.context.event.BeanCreatedEvent;
import io.micronaut.context.event.BeanCreatedEventListener;
import io.micronaut.core.annotation.NonNull;
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 implements BeanCreatedEventListener<SqsClientBuilder> { (2)

    private final SqsConfig sqsConfig;

    SqsClientBuilderListener(SqsConfig sqsConfig) { (3)
        this.sqsConfig = sqsConfig;
    }

    @Override
    public SqsClientBuilder onCreated(@NonNull BeanCreatedEvent<SqsClientBuilder> event) {
        SqsClientBuilder builder = event.getBean();
        try {
            return builder
                .endpointOverride(new URI(sqsConfig.getSqs().getEndpointOverride()))
                .credentialsProvider(
                    StaticCredentialsProvider.create(
                        AwsBasicCredentials.create(sqsConfig.getAccessKeyId(), sqsConfig.getSecretKey())
                    )
                )
                .region(Region.of(sqsConfig.getRegion()));
        } catch (URISyntaxException e) {
            throw new 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/java/example/micronaut/SqsClientCreatedEventListener.java
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)
public class SqsClientCreatedEventListener implements BeanCreatedEventListener<SqsClient> { (2)
    private static final String QUEUE_NAME = "demo_queue";
    @Override
    public SqsClient onCreated(BeanCreatedEvent<SqsClient> event) {
        SqsClient client = event.getBean();
        if (client.listQueues().queueUrls().stream().noneMatch(it -> it.contains(QUEUE_NAME))) {
            client.createQueue(
                CreateQueueRequest.builder()
                    .queueName(QUEUE_NAME)
                    .build()
            );
        }
        return client;
    }
}
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/java/example/micronaut/MicronautguideTest.java
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.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.utility.DockerImageName;

import java.util.Collections;
import java.util.Map;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;

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

    private static DockerImageName localstackImage = DockerImageName.parse("localstack/localstack:latest");
    private static LocalStackContainer localstack = new LocalStackContainer(localstackImage)
            .withServices(LocalStackContainer.Service.SQS);

    @Override
    public @NonNull Map<String, String> getProperties() {
        if (!localstack.isRunning()) {
            localstack.start();
        }
        return Map.of("aws.access-key-id", localstack.getAccessKey(),
                "aws.secret-key", localstack.getSecretKey(),
                "aws.region", localstack.getRegion(),
                "aws.services.sqs.endpoint-override", localstack.getEndpointOverride(LocalStackContainer.Service.SQS).toString());
    }
    @Inject
    @Client("/")
    HttpClient httpClient;

    @Inject
    DemoConsumer demoConsumer;

    @Test
    void testItWorks() {
        assertEquals(0, demoConsumer.getMessageCount());
        httpClient.toBlocking().exchange(HttpRequest.POST("/demo", Collections.emptyMap()));
        await().until(() -> demoConsumer.getMessageCount(), equalTo(1));
        assertEquals(1, demoConsumer.getMessageCount());
    }
}
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 ./gradlew 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, 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.

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 21
sdk install java 21.0.5-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 21
sdk install java 21.0.2-graalce

10.2. Native Executable Generation

To generate a native executable using Gradle, run:

./gradlew nativeCompile

The native executable is created in build/native/nativeCompile directory and can be run with build/native/nativeCompile/micronautguide.

It is possible to customize the name of the native executable or pass additional parameters to GraalVM:

build.gradle
graalvmNative {
    binaries {
        main {
            imageName.set('mn-graalvm-application') (1)
            buildArgs.add('-Ob') (2)
        }
    }
}
1 The native executable name will now be mn-graalvm-application
2 It is possible to pass extra build arguments to native-image. For example, -Ob enables the quick build mode.

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…​).