Direct交换器(发布与订阅)代码实现【java资源库:http://www.gxcode.top/code】

【java资源库:http://www.gxcode.top/code】
1.Direct交换器(发布与订阅)

一.需求图:

二.代码实现:
步骤1:创建maven项目(springboot)mq-direct-provider、mq-direct-consumer

步骤2:修改全局配置文件
mq-direct-provider:
#设置交换器的名称
mq.config.exchange=log.direct
#info 路由键
mq.config.queue.info.routing.key=log.info.routing.key
#error 路由键
mq.config.queue.error.routing.key=log.error.routing.key
mq-direct-consumer:
#设置交换器的名称
mq.config.exchange=log.direct
#info 队列名称
mq.config.queue.info=log.info
#info 路由键
mq.config.queue.info.routing.key=log.info.routing.key
#error 队列名称
mq.config.queue.error=log.error
#error 路由键
mq.config.queue.error.routing.key=log.error.routing.key

步骤2:代码编写
mq-direct-provider:
/**

  • 消息发送者
    **/
    @Component
    public class Sender {
    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;
    // exchange 交换器名称
    @Value(" m q . c o n f i g . e x c h a n g e " ) p r i v a t e S t r i n g e x c h a n g e ; / / r o u t i n g k e y 路 由 键 @ V a l u e ( " {mq.config.exchange}") private String exchange; // routingkey 路由键 @Value(" mq.config.exchange")privateStringexchange;//routingkey@Value("{mq.config.queue.error.routing.key}")
    private String routingkey1;
    @Value("${mq.config.queue.info.routing.key}")
    private String routingkey2;
/*发送消息的方法 */
public void send(String msg) {
 // 向消息队列发送消息
 // 参数一:交换器名称 , 参数二:路由键,   参数:消息
 this.rabbitAmqpTemplate.convertAndSend(this.exchange, this.routingkey1, msg);
 this.rabbitAmqpTemplate.convertAndSend(this.exchange, this.routingkey2, msg);
}

}
/**

  • 测试代码
    /
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes=AppStart.class)
    public class RabbitmqTest {
    @Autowired
    private Sender sender;
    /测试消息队列/
    @Test
    public void test1() throws Exception{
    while(true) {
    Thread.sleep(2000);
    this.sender.send(“Hello RabbitMQ”);
    }
    }
    }
    mq-direct-consumer:
    /
  • 消息接收者
  • @RabbitListener bindings:绑定队列
  • @QueueBinding value:绑定队列的名称 exchange:配置交换器
  • @Queue value:配置队列名称 autoDelete:是否是一个可删除的临时队列
  • @Exchange value:为交换器起个名称 type:指定具体的交换器类型
    /
    @Component
    @RabbitListener(bindings = @QueueBinding(
    value = @Queue(value = “ m q . c o n f i g . q u e u e . e r r o r " , a u t o D e l e t e = " t r u e " ) , / / 消 息 队 列 名 称 绑 定 e x c h a n g e = @ E x c h a n g e ( v a l u e = " {mq.config.queue.error}", autoDelete = "true"), //消息队列名称绑定 exchange = @Exchange(value = " mq.config.queue.error",autoDelete="true"),//exchange=@Exchange(value="{mq.config.exchange}”, type = ExchangeTypes.DIRECT), //交换器名称绑定
    key = “${mq.config.queue.error.routing.key}” //路由键,把消息队列和交换器建立连接
    ))
    public class ErrorReceiver {
    /
    *
    • 接收消息的方法,采用消息队列监听机制@RabbitListener
      */
      @RabbitHandler
      public void process(String msg) {System.out.println("Error…receiver: " + msg);}
      }

/**

  • 消息接收者
  • @RabbitListener bindings:绑定队列
  • @QueueBinding value:绑定队列的名称 exchange:配置交换器
  • @Queue value:配置队列名称 autoDelete:是否是一个可删除的临时队列
  • @Exchange value:为交换器起个名称 type:指定具体的交换器类型
    /
    @Component
    @RabbitListener(bindings = @QueueBinding(
    value = @Queue(value = “ m q . c o n f i g . q u e u e . i n f o " , a u t o D e l e t e = " t r u e " ) , / / 消 息 队 列 名 称 绑 定 e x c h a n g e = @ E x c h a n g e ( v a l u e = " {mq.config.queue.info}", autoDelete = "true"), //消息队列名称绑定 exchange = @Exchange(value = " mq.config.queue.info",autoDelete="true"),//exchange=@Exchange(value="{mq.config.exchange}”, type = ExchangeTypes.DIRECT), //交换器名称绑定
    key = “${mq.config.queue.info.routing.key}” //路由键,把消息队列和交换器建立连接
    ))
    public class InfoReceiver {
    /
    *
    • 接收消息的方法,采用消息队列监听机制@RabbitListener
      */
      @RabbitHandler
      public void process(String msg) {System.out.println("Info…receiver: " + msg);}
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java之书

会持续更新实用好的文章谢谢关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值