5

I am able to send messages to SQS queue from my springboot but not able to receive using sqslistener annotation, can someone help?

public void send(String message) {


    queueMessagingTemplate.convertAndSend("test-queue", MessageBuilder.withPayload(message).build());
}

@SqsListener(value = "test-queue", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void receive(String message)
{
    System.out.println("message: " + message);
}

I have verified send by goign to AWS console, i can see my messages in queue, but they are not coming to receive method. config:

@Bean
public AmazonSQSAsyncClient amazonSQSAsyncClient()
{

    AmazonSQSAsyncClient amazonSQSAsyncClient= new AmazonSQSAsyncClient(amazonAWSCredentials());

    if (!StringUtils.isEmpty(amazonSqsEndpoint)) {
        amazonSQSAsyncClient.setEndpoint(amazonSqsEndpoint);

    }

}

@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory() {
    SimpleMessageListenerContainerFactory msgListenerContainerFactory = new SimpleMessageListenerContainerFactory();
    msgListenerContainerFactory.setAmazonSqs(amazonSQSAsyncClient());
    return msgListenerContainerFactory;
}

@Bean
public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSqs) {
    return new QueueMessagingTemplate(amazonSQSAsyncClient());
}

@Bean
public BasicAWSCredentials amazonAWSCredentials() {
    return new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey);
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
Bhargav
  • 697
  • 3
  • 11
  • 29
  • "Does not work" is simply not enough information for anyone to help you. Have you turned on DEBUG logging? What do you see? – Gary Russell May 08 '17 at 13:03
  • Hi, thats the strange part, there is no error, once i send the message there is nothing printed, message goes and sits in the queue and listener doesnt get the messages at all! – Bhargav May 09 '17 at 05:25

5 Answers5

4

In my case i was missing an annotation @EnableSqs in config class

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Nomadev21
  • 41
  • 1
2

Turns out i had a typo in the queue names in SQS console and my code, my bad.

Bhargav
  • 697
  • 3
  • 11
  • 29
2

Though it is late, it may help someone. In my case, I used gradle with these configuration

  implementation "io.awspring.cloud:spring-cloud-starter-aws-messaging:2.3.0"
  implementation "io.awspring.cloud:spring-cloud-aws-dependencies:2.3.0"

It doesn't work , below works perfectly for me:

compile "io.awspring.cloud:spring-cloud-starter-aws-messaging:2.3.0"
compile "io.awspring.cloud:spring-cloud-aws-dependencies:2.3.0"
mnhmilu
  • 2,327
  • 1
  • 30
  • 50
  • I had the same issue, it was working fine in mvn project but when I migrated it to gradle project it stopped working. – nandeesh May 11 '22 at 07:19
1

In my case, the wrong region is specified. For some reason, sending didn't complain & pushed the message to the queue, but the listener is not getting called. Fixing the region in the application.yml file solved the issue.

Harsha
  • 831
  • 1
  • 9
  • 16
0

In my case QueueMessageHandler bean wasn't initialized.

In the documentation they are using maven dependency:

<dependency>
     <groupId>io.awspring.cloud</groupId>
     <artifactId>spring-cloud-aws-messaging</artifactId>
     <version>{spring-cloud-version}</version>
</dependency>

But in my case I'm using gradle, and also require dependency:

implementation(platform("io.awspring.cloud:spring-cloud-aws-dependencies:2.4.2"))
implementation("io.awspring.cloud:spring-cloud-aws-messaging")
implementation("io.awspring.cloud:spring-cloud-aws-autoconfigure")

Or you could define QueueMessageHandler in your own config file

  @Bean
  public QueueMessageHandler queueMessageHandler(QueueMessageHandlerFactory factory) {
    return factory.createQueueMessageHandler();
  }
Ivan Danilov
  • 41
  • 1
  • 5