springboot中使用rabbitt的详细方法
作者:我想要她 时间:2023-06-17 09:57:43
RabbitMQ的示例,涉及到Direct、Fanout、Topic和Headers交换机以及普通队列、延迟队列和死信队列
在pom.xml文件中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置RabbitMQ连接信息,在application.properties文件中添加以下配置:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
创建消息队列
创建一个普通的Direct交换机和队列:
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queue() {
return new Queue("directQueue", false);
}
@Bean
public DirectExchange directExchange() {
return new DirectExchange("directExchange");
}
@Bean
public Binding binding(Queue queue, DirectExchange directExchange) {
return BindingBuilder.bind(queue).to(directExchange).with("directRoutingKey");
}
}
创建一个Fanout交换机和队列:
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queueA() {
return new Queue("fanoutQueueA", false);
}
@Bean
public Queue queueB() {
return new Queue("fanoutQueueB", false);
}
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
@Bean
public Binding bindingA(Queue queueA, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(queueA).to(fanoutExchange);
}
@Bean
public Binding bindingB(Queue queueB, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(queueB).to(fanoutExchange);
}
}
创建一个Topic交换机和队列:
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queueA() {
return new Queue("topicQueueA", false);
}
@Bean
public Queue queueB() {
return new Queue("topicQueueB", false);
}
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("topicExchange");
}
@Bean
public Binding bindingA(Queue queueA, TopicExchange topicExchange) {
return BindingBuilder.bind(queueA).to(topicExchange).with("topic.key.*");
}
@Bean
public Binding bindingB(Queue queueB, TopicExchange topicExchange) {
return BindingBuilder.bind(queueB).to(topicExchange).with("topic.#");
}
}
创建一个Headers交换机和队列:
@Configuration
public class RabbitMQConfig {
@Bean
public Queue queue() {
return new Queue("headerQueue", false);
}
@Bean
public HeadersExchange headersExchange() {
return new HeadersExchange("headerExchange");
}
@Bean
public Binding binding(Queue queue, HeadersExchange headersExchange) {
Map<String, Object> headers = new HashMap<>();
headers.put("header1", "value1");
headers.put("header2", "value2");
return BindingBuilder.bind(queue).to(headersExchange).whereAll(headers).match();
}
}
创建一个延迟队列和死信队列:
@Configuration
public class RabbitMQConfig {
@Bean
public Queue delayQueue() {
Map<String, Object> args = new HashMap<>();
args.put("x-message-ttl", 10000); // 消息过期时间为10秒
args.put("x-dead-letter-exchange", "deadLetterExchange");
args.put("x-dead-letter-routing-key", "deadLetterRoutingKey");
return new Queue("delayQueue", false, false, false, args);
}
@Bean
public Queue deadLetterQueue() {
return new Queue("deadLetterQueue", false);
}
@Bean
public DirectExchange directExchange() {
return new DirectExchange("directExchange");
}
@Bean
public DirectExchange deadLetterExchange() {
return new DirectExchange("deadLetterExchange");
}
@Bean
public Binding binding(Queue delayQueue, DirectExchange directExchange) {
return BindingBuilder.bind(delayQueue).to(directExchange).with("delayRoutingKey");
}
@Bean
public Binding deadLetterBinding(Queue deadLetterQueue, DirectExchange deadLetterExchange) {
return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange).with("deadLetterRoutingKey");
}
}
发送和接收消息
@Service
public class RabbitMQService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMessage(String message) {
rabbitTemplate.convertAndSend("directExchange", "directRoutingKey", message);
}
@RabbitListener(queues = "directQueue")
public void receiveDirectMessage(String message) {
System.out.println("Received message from directQueue: " + message);
}
public void sendFanoutMessage(String message) {
rabbitTemplate.convertAndSend("fanoutExchange", "", message);
}
@RabbitListener(queues = "fanoutQueueA")
public void receiveFanoutMessageA(String message) {
System.out.println("Received message from fanoutQueueA: " + message);
}
@RabbitListener(queues = "fanoutQueueB")
public void receiveFanoutMessageB(String message) {
System.out.println("Received message from fanoutQueueB: " + message);
}
public void sendTopicMessage(String message) {
rabbitTemplate.convertAndSend("topicExchange", "topic.key.message", message);
}
@RabbitListener(queues = "topicQueueA")
public void receiveTopicMessageA(String message) {
System.out.println("Received message from topicQueueA: " + message);
}
@RabbitListener(queues = "topicQueueB")
public void receiveTopicMessageB(String message) {
System.out.println("Received message from topicQueueB: " + message);
}
public void sendHeaderMessage(String message) {
MessageProperties messageProperties = new MessageProperties();
messageProperties.setHeader("header1", "value1");
messageProperties.setHeader("header2", "value2");
Message msg = MessageBuilder.withBody(message.getBytes()).andProperties(messageProperties).build();
rabbitTemplate.send("headerExchange", "", msg);
}
@RabbitListener(queues = "headerQueue")
public void receiveHeaderMessage(Message message) {
System.out.println("Received message from headerQueue: " + new String(message.getBody()));
}
public void sendDelayMessage(String message) {
rabbitTemplate.convertAndSend("directExchange", "delayRoutingKey", message);
}
@RabbitListener(queues = "deadLetterQueue")
public void receiveDeadLetterMessage(String message) {
System.out.println("Received message from deadLetterQueue: " + message);
}
}
消息确认模式
在RabbitMQ中,消息确认模式有两种:自动确认和手动确认。自动确认是指当消息被成功接收时,RabbitMQ会自动确认消息。手动确认是指当消费者成功处理消息后,显式地向RabbitMQ发送确认消息。
1 使用手动确认模式:
@Configuration
public class RabbitMQConfig {
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
return factory;
}
}
@Service
public class RabbitMQService {
@RabbitListener(queues = "directQueue")
public void receiveDirectMessage(Message message, Channel channel) throws IOException {
try {
System.out.println("Received message from directQueue: " + new String(message.getBody()));
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
}
}
}
在上述代码中,我们使用了MANUAL(手动确认)模式,并在消息处理成功后使用channel.basicAck()方法显式地确认消息。如果发生任何异常,我们使用channel.basicNack()方法拒绝消息并重新加入队列。
希望这个示例能够帮助您了解如何在Spring Boot中使用RabbitMQ,并使用不同的交换机和队列类型以及消息确认模式。
来源:https://blog.csdn.net/u010480873/article/details/130797346
标签:springboot,rabbitt
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中Builder模式的实现详解
2022-08-06 15:37:24
C#中的委托和事件
2023-11-27 12:33:34
![](https://img.aspxhome.com/file/2023/9/113699_0s.jpg)
Android编程之控件状态配置文件实例
2023-07-26 02:38:54
C#中怎样从指定字符串中查找并替换字符串?
2023-09-30 14:26:24
![](https://img.aspxhome.com/file/2023/6/83236_0s.jpg)
简单说说Java SE、Java EE、Java ME三者之间的区别
2022-01-20 06:05:10
Java8 Instant时间戳使用小记
2023-05-31 10:33:59
Android开发中自定义 editText下划线
2023-03-30 13:40:35
![](https://img.aspxhome.com/file/2023/6/118956_0s.png)
Android 用HttpURLConnection访问网络的方法
2023-05-18 17:33:01
C++实现特殊矩阵的压缩存储算法
2023-11-17 23:53:06
![](https://img.aspxhome.com/file/2023/3/120793_0s.png)
解决Jenkins集成SonarQube遇到的报错问题
2023-11-24 08:54:10
![](https://img.aspxhome.com/file/2023/6/64396_0s.png)
Java I/O 操作及优化详细介绍
2022-07-30 14:46:42
![](https://img.aspxhome.com/file/2023/2/75752_0s.jpg)
java中怎样表示圆周率
2022-11-30 11:20:00
![](https://img.aspxhome.com/file/2023/8/79848_0s.png)
Java制作智能拼图游戏原理及代码
2022-08-02 21:46:57
基于C#动手实现网络服务器Web Server
2023-01-21 20:13:28
![](https://img.aspxhome.com/file/2023/0/106490_0s.jpg)
深入理解Java 类加载全过程
2023-10-20 12:34:23
Android自定义指示器时间轴效果实例代码详解
2023-06-13 01:06:33
![](https://img.aspxhome.com/file/2023/8/139388_0s.jpg)
Jetpack navigation组件超详细讲解
2021-07-17 08:49:58
![](https://img.aspxhome.com/file/2023/6/125536_0s.png)
Java中一个线程执行死循环有什么后果
2022-05-24 00:28:35
Android简单使用PopupWindow的方法
2023-09-13 19:51:22
![](https://img.aspxhome.com/file/2023/2/94252_0s.jpg)
C#读取二进制文件方法分析
2022-04-23 11:17:43