[英]Spring SQS Message Handler - add custom message attributes to existing message before sending it to Dead-Letter-Queue
Currently, the messages on source queue are sent to dead-letter-queue on every exception.目前,源队列上的消息在每次异常时都会发送到死信队列。 My goal is to add custom attributes to the failed message so that engineers have more information about failure when they monitor the dead-letter-queue.
我的目标是为失败的消息添加自定义属性,以便工程师在监控死信队列时获得更多关于失败的信息。
When I tried to add attributes to existing message, I receive an java.lang.UnsupportedOperationException exception.当我尝试向现有消息添加属性时,我收到java.lang.UnsupportedOperationException异常。
Here is the aspect code where I add message attributes to existing message.这是我将消息属性添加到现有消息的方面代码。 I am using custom aspect which is triggered before sending the message with AmazonSQSClient.
我正在使用在使用 AmazonSQSClient 发送消息之前触发的自定义方面。
@Before(value = "execution(* com.amazonaws.services.sqs.AmazonSQS*Client.sendMessage*(com.amazonaws.services.sqs.model.SendMessageRequest,..)) && args(request,..)",
argNames = "request")
public void before(SendMessageRequest request) {
Map<String, MessageAttributeValue> messageAttributes = request.getMessageAttributes();
if (messageAttributes == null) return;
messageAttributes.put("MoveToDlqFlag", createMessageAttribute("true"));
}
This is the code where exception happens.这是发生异常的代码。
@SqsListener(value = {"${sqs.queue.source-queue}"}, deletionPolicy = ON_SUCCESS)
public void handleMessageReceived(String rawMessage, @Header("SenderId") String senderId, @Headers Map<String, Object> header) {
var orderMessageWrapper = messageWrapperUtil.create(rawMessage, Order.class);
var order = orderMessageWrapper.getMessage();
var receiveCount = (Integer) header.get("ApproximateReceiveCount");
...
}
Is there a way to add message attributes to existing message before sending to dead-letter-queue?有没有办法在发送到死信队列之前将消息属性添加到现有消息? Maybe spring provides a configuration where it is possible.
也许 spring 提供了可能的配置。
If messageAttributes
map throws java.lang.UnsupportedOperationException
, then maybe you could try to create a new mutable map , which is a copy of immutable map : If
messageAttributes
map throws java.lang.UnsupportedOperationException
, then maybe you could try to create a new mutable map , which is a copy of immutable map :
Map<String, MessageAttributeValue> messageAttributes = new HashMap<>(request.getMessageAttributes());
and then you can use setMessageAttributes()
of SendMessageRequest
然后你可以使用
SendMessageRequest
的setMessageAttributes()
So I hope that this solution would work (unless setMessageAttributes
doesn't throw UnsupportedOperationException
too)所以我希望这个解决方案能够奏效(除非
setMessageAttributes
也不会抛出UnsupportedOperationException
)
Map<String, MessageAttributeValue> messageAttributes = request.getMessageAttributes() == null ? new HashMap<>() : new HashMap<>(request.getMessageAttributes());
messageAttributes.put("MoveToDlqFlag", createMessageAttribute("true"));
request.setMessageAttributes(messageAttributes);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.