Search code examples
javapythonamazon-web-servicesspring-cloudamazon-sns

In Spring Cloud for AWS SNS throwing java.lang.IllegalArgumentException on receiving SNS message


Exception I am getting:

2021-06-16 05:28:57.497 ERROR 2751 --- [nio-8081-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: @NotificationMessage annotated parameters are only allowed for method that receive a notification message.] with root cause

java.lang.IllegalArgumentException: @NotificationMessage annotated parameters are only allowed for method that receive a notification message.
        at org.springframework.cloud.aws.messaging.endpoint.NotificationSubjectHandlerMethodArgumentResolver.doResolverArgumentFromNotificationMessage(NotificationSubjectHandlerMethodArgumentResolver.java:39) ~[spring-cloud-aws-messaging-1.0.0.RELEASE.jar!/:1.0.0.RELEASE]
        at org.springframework.cloud.aws.messaging.endpoint.AbstractNotificationMessageHandlerMethodArgumentResolver.resolveArgument(AbstractNotificationMessageHandlerMethodArgumentResolver.java:49) ~[spring-cloud-aws-messaging-1.0.0.RELEASE.jar!/:1.0.0.RELEASE]
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-4.3.14.RELEASE.jar!/:4.3.14.RELEASE]

Subscription for the topic is done successfully.

Code of method that is supposed to handle message (Java SpringCloud)

    @NotificationMessageMapping
    public void handleNotificationMessage(@NotificationSubject String subject, @NotificationMessage String message) {
        System.out.println("in handleNotificationMessage(): " + message + ", " + subject);
        System.out.println("timestamp: "+ System.currentTimeMillis());
    }

Python code publishing the message

        client = boto3.client(
            "sns",
            aws_access_key_id=Constants.environment_properties().AWS_ACCESS_KEY_ID,
            aws_secret_access_key=Constants.environment_properties().AWS_SECRET_ACCESS_KEY,
            region_name=Constants.environment_properties().REGION_NAME
        )
        message = {
            "s3_file_key": s3_object_key,
            "file_timestamp": file_timestamp,
            "user_id": user_json["id"],
            "username": user_json["username"],
            "device_id": user_json["device_id"]
        }
        # Send your message.
        client.publish(
            TopicArn=Constants.environment_properties().SNS_TOPIC_ARN,
            Message=json.dumps({'default': json.dumps(message)}),
            Subject="Raw data",
            MessageStructure='json'
        )

Solution

  • I was also facing the same issue. Please check by disabling the "Raw Message Delivery" in topic subscription you have created. I don't know the exact reason but it's worked for me