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