0

Springboot application is posting 3 default headers to an AWS SNS Topic. When a AWS SQS is triggered by this topic, the message received is:

{
    "Messages": [
        {
            "MessageId": "5ee14960-8a12-4c42-9f36-4950fbbaeccb",
            "ReceiptHandle": "5ee14960-8a12-4c42-9f36-4950fbbaeccb#a55079a4-dfad-42da-90d4-0f4f08d80412",
            "MD5OfBody": "202cb962ac59075b964b07152d234b70",
            "Body": "123",
            "Attributes": {
                "SentTimestamp": "1675942270504",
                "ApproximateReceiveCount": "1",
                "ApproximateFirstReceiveTimestamp": "1675942277557",
                "SenderId": "127.0.0.1",
                "MessageDeduplicationId": "",
                "MessageGroupId": ""
            },
            "MD5OfMessageAttributes": "52e6944a23aeac893a02a1522152997d",
            "MessageAttributes": {
                "id": {
                    "StringValue": "75700b58-4e1e-63e9-3d49-9813cba025c9",
                    "DataType": "String"
                },
                "contentType": {
                    "StringValue": "text/plain;charset=UTF-8",
                    "DataType": "String"
                },
                "timestamp": {
                    "StringValue": "1675942270483",
                    "DataType": "Number.java.lang.Long"
                }
            }
        }
    ]
}

You can see the headers there: id, contentType and timestamp. I would like to remove these headers/MessageAttributes because it is causing antoher problem to the AWS SQS Consumer: It cannot decode the field timestamp and the application fail to receive the message.

If the message is posted by AWS Console, there is no MessageAttribute sent. I would like to do the same in my springboot application.

There is a similar thread about this: spring-cloud-aws Spring creates message header attribute not supported by SQS but it is an old topic. The solution provided there does not seem to work anymore. The Spring messaging is giving me an Exception: The MessageHeader is immutable.

Here is my SNSPublisher:

public class MessageHeadersCustom extends MessageHeaders {
  public MessageHeadersCustom() {
    //-1L for SpringMessaging dont post timestamp
    super(new HashMap<String, Object>(), ID_VALUE_NONE, -1L);
  }
}

....

public class NotificationMessagingTemplateCustom extends NotificationMessagingTemplate {

  public NotificationMessagingTemplateCustom(AmazonSNS amazonSns) {
    super(amazonSns);
  }

  @Override
  public void sendNotification(Object message, String subject) {
    this.convertAndSend(subject, message, new MessageHeadersCustom());
  }
}

....

@Slf4j
@Component
public class SNSPublisher {

  private final NotificationMessagingTemplateCustom notificationMessagingTemplateCustom;

  public SNSPublisher(final NotificationMessagingTemplateCustom notificationMessagingTemplateCustom) {
    this.notificationMessagingTemplateCustom = notificationMessagingTemplateCustom;
  }

  public <T> void publish(final T payload, final String topic) {

    log.info("Publishing event to topic '{}': {}", topic, payload);

    notificationMessagingTemplateCustom.sendNotification(payload.toString(), topic);
  }

}

My pom.xml:

<properties>
        <aws-sdk-version>2.5.66</aws-sdk-version>
        <spring-cloud-aws.version>2.2.6.RELEASE</spring-cloud-aws.version>
</properties>

<dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>amazon-sqs-java-messaging-lib</artifactId>
            <version>2.0.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-sns</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-aws-messaging</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
</dependencies>
dsicari
  • 590
  • 6
  • 13

0 Answers0