2015-04-23 63 views
1

我使用Spring Cloud AWS消息传递使用SQS发送/接收消息。spring-cloud-aws Spring创建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创建消息的方式的问题吗?

回答

2

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

但是,在我的本地盒子上使用elasticMQ作为真正的SQS的替代品时,它无法处理消息。它看起来像是一个使用该工具而不是Spring的问题。

0

如何回答这个问题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头自动

停止发送你需要重写的MessageHeader和NotificationMessagingTemplate类

类MessageHeaders头ID

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