消息生产者
@Component
public class FanoutProducer {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(String queueName, JSONObject jsonObject) {
String jsonString = jsonObject.toJSONString();
Message message = MessageBuilder.withBody(jsonString.getBytes())
.setContentType(MessageProperties.CONTENT_TYPE_JSON).setContentEncoding("utf-8")
.setMessageId(StringUtil.getUUID()).build();
rabbitTemplate.convertAndSend(queueName, message);
}
消息消费者
@Component
public class SmsMessageConsumer{
@RabbitListener(queuesToDeclare = @Queue("code"))
public void receive(String message) {
System.out.println(message);
}
}
测试
fanoutProducer.send("code", jsonObject);