Search code examples
javaspringamazon-web-servicesamazon-sqslocalstack

SQS listener not handling messages


I created an SQS publisher (publishing messages as expected), the matter is that on the other hand my listener is not receiving any message:

application.yml

cloud:
  aws:
    stack:
      auto: false
    region:
      static: us-east-1
      auto: false
    credentials:
      access-key: 12345666
      secret-key: 12345666
    queue:
      uri: http://localhost:4566
      name: sample-queue.fifo

Listener configuration class:

@Configuration
public class SQSConfig {

    @Value("${cloud.aws.region.static}")
    private String region;

    @Value("${cloud.aws.credentials.access-key}")
    private String accessKeyId;

    @Value("${cloud.aws.credentials.secret-key}")
    private String secretAccessKey;

    @Value("${cloud.aws.queue.uri}")
    private String sqsUrl;
    @Bean
    @Primary
    public AmazonSQSAsync amazonSQSAsync() {
        return AmazonSQSAsyncClientBuilder.standard()
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(sqsUrl, region))
                .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKeyId, secretAccessKey)))
                .build();
    }

    @Bean
    public QueueMessagingTemplate queueMessagingTemplate() {
        return new QueueMessagingTemplate(amazonSQSAsync());
    }

    @Bean
    protected MessageConverter messageConverter(ObjectMapper objectMapper) {

        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setObjectMapper(objectMapper);
        converter.setSerializedPayloadClass(String.class);
        converter.setStrictContentTypeMatch(false);
        return converter;
    }
}

Listener class

@Component
public class SQSListener {

    @SqsListener("sample-queue.fifo")
    public void receiveMessage(Map<String, Object>message) {
        log.info("Message received: {}", message);
    }
}

pom.xml

Spring version:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.4</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

AWS version:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-aws-messaging</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>

The application starts without any errors, and also no errors are logged


Solution

  • Solution found here: https://javatodev.com/how-to-use-amazon-sqs-with-spring-boot/ by using the amazonSQSClient.receiveMessage() method.