1. 程式人生 > 實用技巧 >Rabbmitmq傳送訊息Message的兩種寫法

Rabbmitmq傳送訊息Message的兩種寫法

String msg = RandomStringUtils.randomAlphanumeric(6);
//常規寫法
MessageProperties messageProperties = new MessageProperties();
messageProperties.setMessageId(UUID.randomUUID().toString());
messageProperties.setContentType(CONTENT_TYPE_TEXT_PLAIN);
messageProperties.setContentEncoding("utf8");
Message message 
= new Message(msg.getBytes(), messageProperties); rabbitTemplate.convertAndSend(topicExchange,"demo.email.x",message); //lambda 表示式寫法 @FunctionalInterface public interface MessagePostProcessor { Message postProcessMessage(Message var1) throws AmqpException; default Message postProcessMessage(Message message, Correlation correlation) {
return this.postProcessMessage(message); } } //MessagePostProcessor 是函式介面,可以上lambda rabbitTemplate.convertAndSend(topicExchange,"demo.email.x",msg,messages->{ messages.getMessageProperties().setMessageId(UUID.randomUUID().toString()); messages.getMessageProperties().setContentType(CONTENT_TYPE_TEXT_PLAIN); messages.getMessageProperties().setContentEncoding(CharEncoding.UTF_8);
return messages; });