RabbitAdmin
该类封装了对 RabbitMQ 的管理操作
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
return new RabbitAdmin(connectionFactory);
}
@Autowired
private RabbitAdmin rabbitAdmin;
Exchange 操作
//创建四种类型的 Exchange,均为持久化,不自动删除
rabbitAdmin.declareExchange(new DirectExchange("direct.exchange",true,false));
rabbitAdmin.declareExchange(new TopicExchange("topic.exchange",true,false));
rabbitAdmin.declareExchange(new FanoutExchange("fanout.exchange",true,false));
rabbitAdmin.declareExchange(new HeadersExchange("header.exchange",true,false));
//删除 Exchange
rabbitAdmin.deleteExchange("header.exchange");
Queue 操作
//定义队列,均为持久化
rabbitAdmin.declareQueue(new Queue("debug",true));
rabbitAdmin.declareQueue(new Queue("info",true));
rabbitAdmin.declareQueue(new Queue("error",true));
//删除队列 rabbitAdmin.deleteQueue("debug");
//将队列中的消息全消费掉
rabbitAdmin.purgeQueue("info",false);
Binding 绑定
//绑定队列到交换器,通过路由键
rabbitAdmin.declareBinding(new Binding("debug",Binding.DestinationType.QUEUE,
"direct.exchange","key.1",new HashMap()));
rabbitAdmin.declareBinding(new Binding("info",Binding.DestinationType.QUEUE,
"direct.exchange","key.2",new HashMap()));
rabbitAdmin.declareBinding(new Binding("error",Binding.DestinationType.QUEUE,
"direct.exchange","key.3",new HashMap()));
//进行解绑
rabbitAdmin.removeBinding(BindingBuilder.bind(new Queue("info")).
to(new TopicExchange("direct.exchange")).with("key.2"));
//使用BindingBuilder进行绑定
rabbitAdmin.declareBinding(BindingBuilder.bind(new Queue("info")).
to(new TopicExchange("topic.exchange")).with("key.#"));
//声明topic类型的exchange
rabbitAdmin.declareExchange(new TopicExchange("exchange1",true,false));
rabbitAdmin.declareExchange(new TopicExchange("exchange2",true,false));
//exchange与exchange绑定
rabbitAdmin.declareBinding(new Binding("exchange1",Binding.DestinationType.EXCHANGE,
"exchange2","key.4",new HashMap()));
RabbitTemplate
Spring AMQP 提供了 RabbitTemplate 来简化 RabbitMQ 发送和接收消息操作
@Autowired
private RabbitAdmin rabbitAdmin;
RabbitTemplate 初始化
设置 RabbitTemplate 的默认交换器、默认路由键、默认队列
发送消息
send (自定义消息 Message)
Message message = new Message("hello".getBytes(),new MessageProperties());
// 发送消息到默认的交换器,默认的路由键
rabbitTemplate.send(message);
// 发送消息到指定的交换器,指定的路由键
rabbitTemplate.send("direct.exchange","key.1",message);
// 发送消息到指定的交换器,指定的路由键
rabbitTemplate.send("direct.exchange","key.1",message,new CorrelationData(UUID.randomUUID().toString()));
convertAndSend(自动 Java 对象包装成 Message 对象,Java 对象需要实现 Serializable 序列化接口)
User user = new User("linyuan");
// 发送消息到默认的交换器,默认的路由键
rabbitTemplate.convertAndSend(user);
// 发送消息到指定的交换器,指定的路由键,设置消息 ID
rabbitTemplate.convertAndSend("direct.exchange","key.1",user,new CorrelationData(UUID.randomUUID().toString()));
// 发送消息到指定的交换器,指定的路由键,在消息转换完成后,通过 MessagePostProcessor 来添加属性
rabbitTemplate.convertAndSend("direct.exchange","key.1",user,mes -> {
mes.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
return mes;
});
接收消息
receive(返回 Message 对象)
// 接收来自指定队列的消息,并设置超时时间
Message msg = rabbitTemplate.receive("debug",2000l);
receiveAndConvert(将返回 Message 转换成 Java 对象)
User user = (User) rabbitTemplate.receiveAndConvert();