Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camel doesn't retrieve SQS messages attributes

Here is the route:

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");
        ...
    }
});

The .log() shows an empty map as well as if I print messageAttributes from the processor.

I also tried with the header "CamelAwsSqsMessageAttributes" instead of "CamelAwsSqsAttributes" but still nothing.

I see the attributes from the AWS console though.
By the way I get the message body, and I use Camel 2.15

like image 412
Maxime Laval Avatar asked Feb 09 '23 12:02

Maxime Laval


2 Answers

I figured it out, here is an example to get queue attributes and message attributes:

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

Or add those objects to the registry if you don't use org.apache.camel.main.Main
Then:

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

Of course you can replace Collections.singletonList("All") with the list of attributes you need if you don't want all of them.

like image 175
Maxime Laval Avatar answered Mar 23 '23 15:03

Maxime Laval


I faced the same issue. When I am using camel-aws 2.16.x and I have my endpoint configured as follow

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

Then I have defined a Collection of String in my spring configuration file

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

Above settings work fine but ever since I upgraded to camel-aws 2.17.3. It no longer works. As mentioned in Camel SQS Component, collection of string no longer will be supported for messageAttributeNames and it should be a String with attributes separated by comma.

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.

Below is the changes I made that allowed camel-aws's SqsConsumer to work again:

@Bean
public String sqsMsgAttributeNames() {
     return String.format("%s,%s", "Attr1", "Attr2");
}
like image 37
Andy Ng Avatar answered Mar 23 '23 14:03

Andy Ng