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

3
我正在尝试使用Spring Cloud AWS中的org.springframework.cloud.aws.messaging.core.NotificationMessagingTemplate将通知发布到SNS主题。每次发布通知时,都会生成一个警告消息:“WARN [org.springframework.cloud.aws.messaging.core.TopicMessageChannel] Message header with name 'id' and type 'java.util.UUID' cannot be sent as message attribute because it is not supported by SNS.” 问题似乎在于org.springframework.messaging.MessageHeaders自动生成了一个类型为java.util.UUID的id标头,这不是spring cloud知道如何处理的内容。除了抑制日志之外,是否有避免自动标头生成(我可以在此处不使用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.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>
1个回答

4
这个问题的出现是因为构造函数调用了MessageHeaders类。
MessageHeaders类。
MessageHeaders(Map<String, Object> headers) { } on line 39

如果不发送id头信息,您需要调用MessageHeaders类的构造函数。

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

因为这个构造函数的条件不会自动创建id头,所以需要覆盖MessageHeader和NotificationMessagingTemplate类来停止发送id头。
类MessageHeaders。
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";
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接