0

I am sending a message using AmazonSQS.

AmazonSQS client:

 var sqs = AmazonSQSClientBuilder
        .standard()
        .withEndpointConfiguration(
            buildEndpointConfiguration( configuration ) )
        .withCredentials( buildCredentialsProvider( configuration ) )
        .build();

Set attributes:

    var attributes = new HashMap<String, MessageAttributeValue>();

    headers.forEach( ( key, value ) -> {
        var headerValue = new MessageAttributeValue()
            .withStringValue( value )
            .withDataType( "String" );

        attributes.put( key, headerValue );
    } );

Make a request:

   var request = new SendMessageRequest()
        .withQueueUrl( eventQueueEndpoint )
        .withMessageBody( messageBody )
        .withMessageAttributes( attributes );

Result:

var result = sqs.sendMessage( request );

After I set message attributes, got errors. The errors while testing with testcontainers Testcontainers Localstack

com.amazonaws.SdkClientException: Unable to unmarshall response (Encountered unexpected event: [Stax Event #12]). Response Code: 200, Response Text: 

at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleResponse(AmazonHttpClient.java:1750)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleSuccessResponse(AmazonHttpClient.java:1446)

Caused by: java.lang.RuntimeException: Encountered unexpected event: [Stax Event #12]
at com.amazonaws.transform.StaxUnmarshallerContext.readText(StaxUnmarshallerContext.java:127)

How it can be fixed?

  • Which part of your code/setup deals with spring-messaging? Are you using any of the spring provided frameworks to integrate with AWS or.....? I mean something like spring-cloud-function - https://docs.spring.io/spring-cloud-function/docs/3.1.3/reference/html/aws.html? – Oleg Zhurakousky Jun 14 '21 at 13:43
  • I am using 'org.springframework.cloud:spring-cloud-aws-messaging' dependency for messaging using SQS. Creating AmazonSQS instance manually. Because it sends to different servers. – Nasibulloh Yandashev Jun 15 '21 at 07:42
  • I do not se anything in your code or stack trace even remotely pointing to anything Spring, hence my question. . . – Oleg Zhurakousky Jun 16 '21 at 07:58
  • The error while parsing xml data. Something is weird. If I set attributes it is not parsed, if send the event without attributes, no any problem with parsing – Nasibulloh Yandashev Jun 16 '21 at 08:12

1 Answers1

0

Finally, After long researches, I found the answer. By default AmazonSQS's MessageAttributeValue class wraps attributes into CDATA section. While parsing it to the XML, the exception occurred. The way to solve this problem is using byte[] data type instead of String data type.

The correct way to create a MessageAttributeValue class is :

var attribute = new MessageAttributeValue()
        .withDataType( "Binary" )
        .withBinaryValue( ByteBuffer.wrap( message.getBytes( UTF8 ) ) );

The issue was with LocalStack. You can see the issue by visiting the link Small deviation in SQS implementation