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?
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