Rabbitmq發送消息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;
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM