目录
以一个处理日志的例子进行讲解。
创建项目
创建两个项目,一个项目为消息发送者,一个项目为消息接收者。
消息接收者(服务)项目搭建
配置文件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
但是为什么上传的时候没有找到设置积分选项呢。然后就是默认的积分了...