2015-04-23 131 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"; 
     } 
    } 
}