Spring Cloud AWS - 无效 Header 发布 SNS 通知

Spring Cloud AWS - Invalid Header posting SNS notification

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

每次通知 post 都会生成一条警告消息:

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

问题似乎是 org.springframework.messaging.MessageHeaders 自动生成一个 java.util.UUID 类型的 ID header,spring 云不知道如何处理。

有没有办法避免自动 header 生成(我可以在没有 UUID 的情况下生活)或避免警告,除了抑制日志?

类似的事情也在影响 SQS:

相关问题: spring-cloud-aws Spring creates message header attribute not supported by SQS Related Bug: Warning "'java.util.UUID' cannot be sent as message attribute ..." on any request sent to SQS channel

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

package com.Whosebug.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.Whosebug.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 class

消息标题class

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

并且不发送 id header 你需要调用构造函数 邮件标题 class

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

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

要停止发送 header ID,您需要覆盖 MessageHeader 和 NotificationMessagingTemplate classes

class 邮件标题

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

class NotificationMessagingTemplate

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

最后,您的 class 将调用需要使用您的实现

package com.Whosebug.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";
        }
    }
}