我正在使用Spring Cloud AWS messaging通过SQS发送/接收消息。
我的代码如下所示:
@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);
}
}发送者代码如下所示(通过控制器连接):
@Component
public class Sender {
@Autowired
private QueueMessagingTemplate queueMessagingTemplate;
public void send(MyMessage message) {
queueMessagingTemplate.convertAndSend("testQueue", message);
}
}我有一个application.yaml文件,它定义了似乎正确加载的AWS参数。因此,当我运行这个应用程序时,我得到以下警告/错误:
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创建消息的方式有问题?
发布于 2015-04-23 20:41:19
这似乎只是一个警告,并不影响消息的发送和/或接收。当我对一个真实的SQS队列进行测试时,我可以发送和接收消息。
然而,当在我的本地机器上使用elasticMQ作为真正的SQS的替代品时,它无法处理消息。这看起来是该工具的问题,而不是Spring的问题。
发布于 2016-02-19 01:07:05
如何回答问题this
出现该问题是因为构造函数调用了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标头
要停止发送标头id,需要覆盖MessageHeader和NotificationMessagingTemplate类
MessageHeaders类
public class MessageHeadersCustom extends MessageHeaders {
public MessageHeadersCustom() {
super(new HashMap<String, Object>(), ID_VALUE_NONE, null);
}
}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);
}
}最后,将进行调用的类需要使用您的实现
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";
}
}
}https://stackoverflow.com/questions/29814139
复制相似问题