Spring Boot Application failed on start when using @SqsListener

5k views Asked by At

I'm trying to implement SQS listener in my Spring Boot app (Kotlin). I'm using spring-cloud-aws-messaging. Here is an article that walked me through implementation.

Problem: application is unable to start.

Logs says:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: Invocation of init method failed; nested exception is com.amazonaws.services.sqs.model.AmazonSQSException: null (Service: AmazonSQS; Status Code: 500; Error Code: 500 ; Request ID: null)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1803)
...

Kindly asking for help :)

docker-compose.yml

version: "2"

services:
  localstack:
    image: localstack/localstack:0.11.5
    ports:
      - "8085:8080"
      - "4569-4576:4569-4576"
    environment:
      - SERVICES=sqs:4573
      - DOCKER_HOST=unix:///var/run/docker.sock
      - DEFAULT_REGION=eu-west-1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - ./docker/dev/localstack-init-scripts:/docker-entrypoint-initaws.d

docker-entrypoint-initaws.sh

#!/usr/bin/env bash
set -x
awslocal sqs create-queue --queue-name my-queue-name
set +x

InfrastructureConfiguration.kt

@EnableSqs
class InfrastructureConfiguration(...) {
  @Primary
  @Bean
  fun amazonSQSAsync(): AmazonSQSAsync {
    val credentials: AWSCredentials = BasicAWSCredentials("accessKey", "secretKey")

    return AmazonSQSAsyncClientBuilder
      .standard()
      .withEndpointConfiguration(
        AwsClientBuilder.EndpointConfiguration(
          "http://localstack:4573",
          Regions.fromName("eu-west-1").toString()
      )
    ).withCredentials(AWSStaticCredentialsProvider(credentials)).build()
  }

  @Primary
  @Bean
  fun simpleMessageListenerContainerFactory(amazonSQSAsync: AmazonSQSAsync): 
  SimpleMessageListenerContainerFactory {
    val factory = SimpleMessageListenerContainerFactory()
    factory.setAmazonSqs(amazonSQSAsync)
    factory.setAutoStartup(false)
    factory.setMaxNumberOfMessages(10)
    factory.setWaitTimeOut(20)
    return factory
  }
}

AmazonSqs.kt

val logger = KotlinLogging.logger {}

@Lazy
@Component
class AmazonSqs {
  companion object {
    const val QUEUE_NAME = "my-queue-name"
  }

  @SqsListener(QUEUE_NAME)
  fun receiveMessage(message: String?) {
    logger.info("Received message: {}")
  }
}
1

There are 1 answers

0
wiktorkudej On

The problem lies in localstack. I've run docker-compose up again and noticed that port 4573 is deprecated.

localstack_1  | Starting edge router (https port 4566)...
localstack_1  | Starting mock SQS service on http ports 4566 (recommended) and 
4573 (deprecated)...

The solution is to expose port 4566 in docker-compose and use it instead.

ports:
  - "8085:8080"
  - "4566-4576:4566-4576"