Using SqsListener with SNS and SQS

This works without the @NotificationMessage as well. This way you don't need to send the "Type" and "Message" part, that is required to work with this annotation.

First create a class with the needed attributes.

public class SqsMessage {

   private String myTask;

   public SqsMessage() {
   }

   public SqsMessage(@JsonProperty("MyTask") String myTask ) {
       this.myTask = myTask ;
   }

   //Getter + Setter 
}

Next set up the Listener

@SqsListener(value = {"MyQueue"}, deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void receiveMessage(SqsMessage payload, @Headers Map<String, Object> header) {
   logger.info("Got message with task: " + payload.getTask() 
    + " with custom attribute " + header.get("CustomAttribute").toString());
}

Now you can send a JSON like

{"MyTask":"My task"}

The @JsonProperty("MyTask") annoation in the POJO's constructor can be optional, depending on your spring version and if your attribute has the same name as in the Json string. It's not necessary for example, if your attribute is called task and your Json string is {"task":"My task"}.


Yes it should. And it does actually.

In order to have the correct HandlerMethodArgumentResolver invoked (in this case NotificationMessageArgumentResolver) on deserialization, which in turn invokes the correct converter NotificationRequestConverter you simply need to add the annotation org.springframework.cloud.aws.messaging.config.annotation.NotificationMessage to your method signature. E.g.

@SqsListener(value = "my-queue", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
public void handle(final @NotificationMessage MyObject obj) throws Exception {
// ...
}

This way the Message part of your SNS gets extracted and converted to MyObject.