简体   繁体   English

Spring SQS 消息处理程序 - 在将现有消息发送到死信队列之前添加自定义消息属性

[英]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然后你可以使用SendMessageRequestsetMessageAttributes()

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.

相关问题 Spring Kafka死消息队列和重试 - Spring Kafka dead message Queue and retries 处理死信队列消息代理独立的方式 - Handling dead-letter queue message-broker independent way RabbitListener异常不将消息发送到死信队列 - RabbitListener Exception do not send message to dead letter queue Spring AMQP-使用带有TTL的死信机制进行消息重新排队 - Spring AMQP - Message re queuing using dead letter mechanism with TTL 如何在 Spring Cloud AWS SQS 中以编程方式设置消息处理程序? - How to set a Message Handler programmatically in Spring Cloud AWS SQS? 处理 AWS SQS 死信队列 - Handle AWS SQS Dead Letter Queue 将Spring Integration JMS消息发送到特定队列(动态) - Sending spring integration jms message to specific queue (dynamically) Spring集成消息处理程序:ActiveMQTextMessage - Spring integration message handler : ActiveMQTextMessage Spring Integration-如何在使用声明性方法发送回SOAP Web服务的确认之前添加关于主题的消息 - Spring Integration - How to add message on topic before sending back acknowledgement for SOAP web service using declarative approach 在Spring websockets中发送错误消息 - Sending Error message in Spring websockets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM