问题描述
我正在使用 AmazonSQS 发送消息。
AmazonSQS 客户端:
var sqs = AmazonSQSClientBuilder
.standard()
.withEndpointConfiguration(
buildEndpointConfiguration( configuration ) )
.withCredentials( buildCredentialsProvider( configuration ) )
.build();
设置属性:
var attributes = new HashMap<String,MessageAttributeValue>();
headers.forEach( ( key,value ) -> {
var headerValue = new MessageAttributeValue()
.withStringValue( value )
.withDataType( "String" );
attributes.put( key,headerValue );
} );
提出请求:
var request = new SendMessageRequest()
.withQueueUrl( eventQueueEndpoint )
.withMessageBody( messageBody )
.withMessageAttributes( attributes );
结果:
var result = sqs.sendMessage( request );
设置消息属性后,出现错误。使用测试容器进行测试时的错误 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)
如何修复?
解决方法
终于,经过长时间的研究,我找到了答案。默认情况下,AmazonSQS 的 MessageAttributeValue 类将属性包装到 CDATA 部分。在将其解析为 XML 时,发生了异常。解决这个问题的方法是使用byte[]数据类型而不是String数据类型。
创建MessageAttributeValue 类的正确方法是:
var attribute = new MessageAttributeValue()
.withDataType( "Binary" )
.withBinaryValue( ByteBuffer.wrap( message.getBytes( UTF8 ) ) );
问题出在 LocalStack 上。您可以通过访问链接 Small deviation in SQS implementation
查看问题