【RabbitMQ】Spring boot Demo(九)
1、项目配置文件
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wchw</groupId>
<artifactId>rabbitmq-demo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rabbitmq-demo2</name>
<description>rabbitmq demo2</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8080
spring:
rabbitmq:
username: admin
password: wsxadmin
virtual-host: /
host: test.rabbitmq.com
port: 5672
Fanout模式用例
生产者
@Configuration
public class RabbitMqFanoutConfiguration {
// 1、声明 Fanout 交换机
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange(Constants.FANOUT_EXCHANGE_NAME, true, false);
}
// 2、声明队列
@Bean(Constants.FANOUT_QUEUE1_NAME)
public Queue queue1() {
return new Queue(Constants.FANOUT_QUEUE1_NAME);
}
@Bean(Constants.FANOUT_QUEUE2_NAME)
public Queue queue2() {
return new Queue(Constants.FANOUT_QUEUE2_NAME);
}
// 3、绑定队列
@Bean("queue1Binding")
public Binding queue1Binding() {
return BindingBuilder.bind(queue1()).to(fanoutExchange());
}
@Bean("queue2Binding")
public Binding queue2Binding() {
return BindingBuilder.bind(queue2()).to(fanoutExchange());
}
}
@Service
public class FanoutSendMsgService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMsg(String msg) {
rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE_NAME, "", msg);
}
}
消费者
@Configuration
public class FanoutMsgListener {
/**
* 使用手动确认
* @param channel
* @param message
*/
@RabbitListener(queues = Constants.FANOUT_QUEUE1_NAME, ackMode = "MANUAL")
public void receiveQueue1Msg(Channel channel, Message message) {
try {
System.out.println("receiveQueue1Msg ====>>> " + new String(message.getBody()));
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
if (message.getMessageProperties().getRedelivered()) {
System.out.println("消息已重复处理失败,拒绝再次接收!");
// 拒绝消息,requeue=false 表示不再重新入队,如果配置了死信队列则进入死信队列
try {
channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
} catch (IOException e1) {
e1.printStackTrace();
}
} else {
System.out.println("消息即将再次返回队列处理!");
// requeue为是否重新回到队列,true重新入队
try {
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
} catch (IOException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
}
@RabbitListener(queues = Constants.FANOUT_QUEUE2_NAME)
public void receiveQueue2Msg(Message message) {
try {
System.out.println("receiveQueue2Msg ====>>> " + new String(message.getBody()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Direct 模式用例
生产者
@Configuration
public class RabbitMqDirectConfiguration {
// 1、声明 Direct 交换机
@Bean
public DirectExchange directExchange() {
return new DirectExchange(Constants.DIRECT_EXCHANGE_NAME, true, false);
}
// 2、声明队列
@Bean(Constants.QUEUE_DIRECT_EMAIL)
public Queue queue1() {
return QueueBuilder.durable(Constants.QUEUE_DIRECT_EMAIL).build();
}
@Bean(Constants.QUEUE_DIRECT_MESSAGE)
public Queue queue2() {
return QueueBuilder.durable(Constants.QUEUE_DIRECT_MESSAGE).build();
}
// 3、绑定队列
@Bean("queue1BindingDirect")
public Binding queue1Binding() {
return BindingBuilder.bind(queue1()).to(directExchange()).with("info");
}
@Bean("queue2BindingDirect")
public Binding queue2Binding() {
return BindingBuilder.bind(queue2()).to(directExchange()).with("error");
}
}
@Service
public class DirectSendMsgService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMsg(String msg, String routingKey) {
rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE_NAME, routingKey, msg);
}
}
消费者
@Configuration
public class DirectMsgListener {
@RabbitListener(queues = Constants.QUEUE_DIRECT_EMAIL)
public void receiveQueue1Msg(Channel channel, Message message) {
System.out.println("receiveQueue1Msg ====>>> " + new String(message.getBody()));
}
@RabbitListener(queues = Constants.QUEUE_DIRECT_MESSAGE)
public void receiveQueue2Msg(Message message) {
try {
System.out.println("receiveQueue2Msg ====>>> " + new String(message.getBody()));
} catch (Exception e) {
e.printStackTrace();
}
}
}
好学若饥,谦卑若愚
标签:
Middleware
, Coding
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?