RabbitAdmin 该类封装了对 RabbitMQ 的管理操作
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory createConnectionFactory) {
return new RabbitAdmin(createConnectionFactory);
}
DirectProducer
@Component
@Slf4j
public class DirectProducer{
@Autowired
private RabbitAdmin rabbitAdmin;
public void message() {
Queue adminQueue = new Queue("adminQueue", true);
FanoutExchange fanoutExchange = new FanoutExchange("adminExchange", true, false);
rabbitAdmin.declareExchange(fanoutExchange);
rabbitAdmin.declareQueue(adminQueue);
rabbitAdmin.declareBinding(BindingBuilder.bind(adminQueue).to(fanoutExchange));
String message = "rabbitmqAdmin";
RabbitTemplate rabbitTemplate = rabbitAdmin.getRabbitTemplate();
rabbitTemplate.convertAndSend("adminExchange", "", message, cur -> {
//设置消息持久化
cur.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
return cur;
});
Message receive = rabbitTemplate.receive("adminQueue");
log.info(">>>>>>>>接收到消息为:{}", receive.getBody());
}
}
RabbitTemplate