首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring-cloud-aws Spring创建SQS不支持的消息头属性

spring-cloud-aws Spring创建SQS不支持的消息头属性
EN

Stack Overflow用户
提问于 2015-04-23 13:05:31
回答 2查看 2.6K关注 0票数 1

我正在使用Spring Cloud AWS messaging通过SQS发送/接收消息。

我的代码如下所示:

代码语言:javascript
复制
@SpringBootApplication
@Import(MessagingConfig.class)
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}


@Configuration
public class MessagingConfig {

    @Bean
    public QueueMessagingTemplate queueMessagingTemplate(AmazonSQS amazonSqs, ResourceIdResolver resourceIdResolver) {
        return new QueueMessagingTemplate(amazonSqs, resourceIdResolver);
    }

}

发送者代码如下所示(通过控制器连接):

代码语言:javascript
复制
@Component
public class Sender {

    @Autowired
    private QueueMessagingTemplate queueMessagingTemplate;

    public void send(MyMessage message) {
        queueMessagingTemplate.convertAndSend("testQueue", message);
    }
}

我有一个application.yaml文件,它定义了似乎正确加载的AWS参数。因此,当我运行这个应用程序时,我得到以下警告/错误:

代码语言:javascript
复制
Message header with name 'id' and type 'java.util.UUID' cannot be sent as message attribute because it is not supported by SQS.

是我做错了什么,还是Spring为SQS创建消息的方式有问题?

EN

回答 2

Stack Overflow用户

发布于 2015-04-23 20:41:19

这似乎只是一个警告,并不影响消息的发送和/或接收。当我对一个真实的SQS队列进行测试时,我可以发送和接收消息。

然而,当在我的本地机器上使用elasticMQ作为真正的SQS的替代品时,它无法处理消息。这看起来是该工具的问题,而不是Spring的问题。

票数 2
EN

Stack Overflow用户

发布于 2016-02-19 01:07:05

如何回答问题this

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

MessageHeaders类

代码语言:javascript
复制
MessageHeaders(Map<String, Object> headers) { } on line 39

为了不发送id头,您需要调用构造函数MessageHeaders类。

代码语言:javascript
复制
MessageHeaders(Map<String, Object> headers, UUID id, Long timestamp){} on line 43

因为此构造函数有条件,所以不会自动创建id标头

要停止发送标头id,需要覆盖MessageHeader和NotificationMessagingTemplate类

MessageHeaders类

代码语言:javascript
复制
public class MessageHeadersCustom extends MessageHeaders {
    public MessageHeadersCustom() {
        super(new HashMap<String, Object>(), ID_VALUE_NONE, null);
    }
}

NotificationMessagingTemplate类

代码语言:javascript
复制
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);
    }
}

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

代码语言:javascript
复制
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";
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29814139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档