Simple AWS SQS listener

example workflow Maven Publish

It is very easy to get started with AWS SQS. However, official documentation does not offer production-ready examples of efficient polling mechanisms. Unlike other messaging tools, SQS requires you to write your own polling code using official SDK. Making it efficient, scalable and multithreaded is not trivial and requires a lot of boilerplate code.

This library uses official AWS SDK (v2) and abstracts away polling from the queue with fluent configuration interface.

Features

Dependency Management

Maven

maven central

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>io.github.vladcar</groupId>
      <artifactId>simple-sqs-listener-bom</artifactId>
      <version>find latest version in maven central</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

Import core module for plain java usage

<dependency>
  <groupId>io.github.vladcar</groupId>
  <artifactId>simple-sqs-listener-core</artifactId>
</dependency>

Import this if you use Spring Boot (includes core module transitively)

<dependency>
  <groupId>io.github.vladcar</groupId>
  <artifactId>simple-sqs-listener-spring-boot</artifactId>
</dependency>

Example Usage

Plain Java

 SqsClient sqsClient = SqsClient.create();

 SqsQueue queue = SqsQueue.builder()
    .url("https://my-queue-url.com")
    .maxBatchSize(10)
    .visibilityTimeoutSeconds(90)
    .longPolling(true)
    .autoAcknowledge(true)
    .messageHandler((meessage) -> {
      // handle message
    })
    .errorHandler((message, exception) -> {
      // handle error
    })
    .build();

 SqsQueueMessageListener listener = new SqsQueueMessageListener(sqsClient);
 listener.setQueue(queue);
 listener.setConsumerCount(3);
 
 listener.initialize();

Spring Boot configuration

Prerequisites
package com.example;

import com.vladc.sqslistener.annotation.EnableSqs;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.services.sqs.SqsClient;

@EnableSqs
@Configuration
public class Config {

  @Bean
  public SqsClient sqsClient() {
    return SqsClient.builder()
        .build();
  }
}
package com.example;

import com.vladc.sqslistener.annotation.SqsMessageHandler;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.services.sqs.model.Message;

@Component
public class TestHandler {

  @SqsMessageHandler(queueName = "test-queue")
  public void handleMessage(Message message) {
    System.out.println(message);
  }
}

Complete spring-boot configuration in examples