RabbitAdmin
该类封装了对 RabbitMQ 的管理操作
@Beanpublic RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){ return new RabbitAdmin(connectionFactory); }@Autowiredprivate 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));//删除 ExchangerabbitAdmin.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类型的exchangerabbitAdmin.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 发送和接收消息操作
@Autowiredprivate 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);// 发送消息到指定的交换器,指定的路由键,设置消息 IDrabbitTemplate.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();
------- 待完善 -------
作者:林塬
链接:https://www.jianshu.com/p/e647758a7c50
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦