RabbitMq入门(四)交换器Direct(发布与订阅完全匹配)

目录

创建项目 

消息接收者(服务)项目搭建

    配置文件yml

    消息处理类

            info消息处理类

            error消息处理类

消息发送者项目创建

    配置文件yml

     消息发送类

测试类

启动测试

      启动消息接收者

      启动消息发送者测试类

          修改消息发送类里的路由键

测试结果


以一个处理日志的例子进行讲解。

创建项目 

创建两个项目,一个项目为消息发送者,一个项目为消息接收者。

消息接收者(服务)项目搭建

    配置文件yml

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#设置交换器的名称
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

    消息处理类

            info消息处理类

package com.lgh.rabbit_mq.config;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接收者
 * @RabbitListener  bindings:绑定队列
 * @QueueBinding    value:绑定队列的名称
 *                  exchange:配置交换器
 *
 * @Queue           value:配置队列名称
 *                  autoDelete:是否是一个可删除的临时队列
 *
 * @Exchange        value:为交换器起个名称
 *                  type:指定具体的交换器类型
 */
@Component
@RabbitListener(
        bindings=@QueueBinding(
                value=@Queue(
                        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 InfoReceive {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("Info........receiver:" + msg);
    }
}

            error消息处理类

package com.lgh.rabbit_mq.config;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接收者
 * @RabbitListener  bindings:绑定队列
 * @QueueBinding    value:绑定队列的名称
 *                  exchange:配置交换器
 *
 * @Queue           value:配置队列名称
 *                  autoDelete:是否是一个可删除的临时队列
 *
 * @Exchange        value:为交换器起个名称
 *                  type:指定具体的交换器类型
 */
@Component
@RabbitListener(
        bindings=@QueueBinding(
                value=@Queue(
                        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 ErrorReceive {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("error........receiver:" + msg);
    }
}

消息发送者项目创建

    配置文件yml

           只配置出交换器的名称。对应的路由键即可

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#设置交换器的名称
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

     消息发送类

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 *   消息发送者
 **/
@Component
public class Sender {

    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;

    //exchange交换器名称
    @Value("${mq.config.exchange}")
    private String exchange;

    //routingkey路由键
    @Value("${mq.config.queue.error.routing.key}")
    private String routingkey;

    /*
        发送消息的方法
    */
    public void send(String msg){
        //向消息队列发送消息
        // 参数一:交换器名称。
        // 参数二:路由键
        // 参数三:消息
        this.rabbitAmqpTemplate.convertAndSend(this.exchange,this.routingkey,msg);
    }

}

测试类

package com.lgh.rabbit_mq;

import com.lgh.rabbit_mq.config.Sender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMqApplicationTests {

	@Autowired
	Sender sender;

	@Test
	public void contextLoads() {
		sender.send("info");
	}


}

启动测试

      启动消息接收者

      启动消息发送者测试类

          修改消息发送类里的路由键

@Value("${mq.config.queue.error.routing.key}")

测试结果

项目demo上传了 https://download.csdn.net/download/qq_32786139/11045075

但是为什么上传的时候没有找到设置积分选项呢。然后就是默认的积分了...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值