骆驼不检索 SQS 消息属性

Camel doesn't retrieve SQS messages attributes

这是路线:

from("aws-sqs://myQueue?accessKey=RAW(xxx)&secretKey=RAW(yyy)&deleteAfterRead=false")
.log("Attributes: ${header.CamelAwsSqsAttributes}")
.process(new Processor() {
    @Override
    public void process(Exchange exchange) throws Exception {
        Map<String, String> messageAttributes = (Map<String, String>) exchange.getIn().getHeader("CamelAwsSqsAttributes");
        ...
    }
});

.log() 显示一张空地图,就像我从处理器打印 messageAttributes 一样。

我也试过 header "CamelAwsSqsMessageAttributes" 而不是 "CamelAwsSqsAttributes" 但仍然没有。

虽然我从 AWS 控制台看到了属性。
顺便说一句,我收到消息 body,我使用 Camel 2.15

我明白了,这里有一个获取队列属性和消息属性的例子:

main.bind("sqsAttributeNames", Collections.singletonList("All"));
main.bind("sqsMessageAttributeNames", Collections.singletonList("All"));

如果您不使用 org.apache.camel.main.Main
,则将这些对象添加到注册表中 那么:

from("aws-sqs://myQueue?accessKey=RAW(xxx)&secretKey=RAW(yyy)&deleteAfterRead=false&attributeNames=#sqsAttributeNames&messageAttributeNames=#sqsMessageAttributeNames")

当然,如果您不需要所有属性,您可以将 Collections.singletonList("All") 替换为您需要的属性列表。

我遇到了同样的问题。当我使用 camel-aws 2.16.x 并且我的端点配置如下

from("aws-sqs://myQueue?...&messageAttributeNames=#sqsMsgAttributeNames")
    .to(...)

然后我在我的 spring 配置文件中定义了一个字符串集合

@Bean
public Collection<String> sqsMsgAttributeNames() {
     return Arrays.asList("Attr1", "Attr2");
}

以上设置工作正常,但自从我升级到 camel-aws 2.17.3。它不再有效。如 Camel SQS Component 中所述,messageAttributeNames 将不再支持字符串集合,它应该是一个字符串,其属性由逗号分隔。

Note: The string containing attributes should not contain any white spaces otherwise camel-aws component will only read the first attribute. I went through the pain to debug on this. Besides, setting the attribute value to be "All" does not work for me, none of the message attributes will be read.

以下是我为使 camel-aws 的 SqsConsumer 再次工作所做的更改:

@Bean
public String sqsMsgAttributeNames() {
     return String.format("%s,%s", "Attr1", "Attr2");
}

这不是 Camel 的问题。它可以是 SQSaws-java-sdk-core 库的默认行为。 作为快速解决方案,可以使用 aws-sqs URL

aws-sqs://myQueue?<other attributes here>&attributeNames=All

请记住,与 SQS 不同,localstack 可以在没有 attributeNames 参数的情况下正常工作。