从 Java 中的具有消息属性的队列接收消息时出现 AWS SQS 错误

AWS SQS error while receiving message from queue in Java with message attributes

我正在使用 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 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 class 将属性包装到 CDATA 部分。解析到XML时出现异常。解决这个问题的方法是使用 byte[] 数据类型而不是 String 数据类型。

创建 MessageAttributeValue class 的正确方法是:

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

问题出在 LocalStack 上。您可以通过访问 link Small deviation in SQS implementation

查看问题