Spring Cloud AWS - 无效标头发布 SNS 通知

2024-01-04

我正在尝试使用org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate(来自 Spring Cloud AWS)将通知发布到 SNS 主题。

每次发布通知时都会生成一条警告消息:

警告 [org.springframework.cloud.aws.messaging.core.TopicMessageChannel] 名称为“id”、类型为“java.util.UUID”的消息头无法作为消息属性发送,因为 SNS 不支持它。

问题似乎是这样的org.springframework.messaging.MessageHeaders自动生成一个 java.util.UUID 类型的 id 标头,这不是 Spring Cloud 知道如何处理的东西。

除了抑制日志之外,是否有办法避免自动标头生成(我可以在这里没有 UUID)或避免警告?

类似的事情也影响着 SQS:

相关问题:spring-cloud-aws Spring 创建 SQS 不支持的消息头属性 https://stackoverflow.com/questions/29814139/spring-cloud-aws-spring-creates-message-header-attribute-not-supported-by-sqs 相关错误:在发送到 SQS 通道的任何请求上警告“'java.util.UUID'无法作为消息属性发送...” https://github.com/spring-cloud/spring-cloud-aws/issues/86

我的控制器看起来像这样:

package com.stackoverflow.sample.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/whatever")
public class SampleController {

    @Autowired
    private NotificationMessagingTemplate template;

    @RequestMapping(method = RequestMethod.GET)
        public String handleGet() {
            this.template.sendNotification("message", "subject");
            return "yay";
        }
    }
}

我的 Spring 配置如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:aws-context="http://www.springframework.org/schema/cloud/aws/context"
        xmlns:aws-messaging="http://www.springframework.org/schema/cloud/aws/messaging"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/cloud/aws/context http://www.springframework.org/schema/cloud/spring-cloud-aws-context.xsd
        http://www.springframework.org/schema/cloud/aws/messaging http://www.springframework.org/schema/cloud/spring-cloud-aws-messaging.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.stackoverflow.sample" />
    <mvc:annotation-driven />

    <aws-context:context-credentials>
        <aws-context:instance-profile-credentials/>
        <aws-context:simple-credentials access-key="MyKey" secret-key="mySecret" />
    </aws-context:context-credentials>

    <aws-messaging:notification-messaging-template id="notificationMessagingTemplate" region="us-west-2" default-destination="myTopic" />
</beans>

出现该问题是因为调用了MessageHeaders类的构造函数

消息头类

MessageHeaders(Map<String, Object> headers) { } on line 39

为了不发送 id 标头,您需要调用构造函数 消息头类

MessageHeaders(Map<String, Object> headers, UUID id, Long timestamp){} on line 43

因为这个构造函数有条件不会自动创建 id header

要停止发送标头 id,您需要重写 MessageHeader 和 NotificationMessagingTemplate 类

消息头类

public class MessageHeadersCustom extends MessageHeaders {
    public MessageHeadersCustom() {
        super(new HashMap<String, Object>(), ID_VALUE_NONE, null);
    }
}

类通知消息模板

public class NotificationMessagingTemplateCustom extends NotificationMessagingTemplate {

    public NotificationMessagingTemplateCustom(AmazonSNS amazonSns) {
        super(amazonSns);
    }

    @Override
    public void sendNotification(Object message, String subject) {

        MessageHeaders headersCustom = new MessageHeadersCustom();
        headersCustom.put(TopicMessageChannel.NOTIFICATION_SUBJECT_HEADER, subject);

        this.convertAndSend(getRequiredDefaultDestination(), message, headersCustom);
    }
}

最后,将进行调用的类需要使用您的实现

package com.stackoverflow.sample.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/whatever")
public class SampleController {

    @Autowired
    private NotificationMessagingTemplateCustom template;

    @RequestMapping(method = RequestMethod.GET)
        public String handleGet() {
            this.template.sendNotification("message", "subject");
            return "yay";
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Cloud AWS - 无效标头发布 SNS 通知 的相关文章

随机推荐