amazon-sqsaws-java-sdkspring-messaging

Spring @SqsListener - get queue name


I understand that with Springs @SqsListener annotation I can listen to multiple queues with the same listener. As per documentation:

public @interface SqsListener {

    /**
     * List of queues. Queues can be defined by their logical/physical name or URL.
     * @return list of queues
     */
    String[] value() default {};

So an example in Kotlin could look something like this:

@SqsListener(value = ["queue1", "queue2"], deletionPolicy = SqsMessageDeletionPolicy.ALWAYS)
@MessageMapping
fun testListeningToMultipleQueues(@Payload myPayloadObject: MyPayloadObject, @Header("myCustomHeader") myCustomHeader: String) {
    logger.info("Message received, but not sure from which queue")
}

Is there some way inside the testListeningToMultipleQueues method to know wether the message came from queue1 or queue2?


Solution

  • You have to use this header:

    @Header("LogicalResourceId") receivedQueue: String
    

    Or lookupDestination if LogicalResourceId comes as an ARN.