spring boot封装rabbitmq6种模式
时间: 2023-08-18 21:07:13 浏览: 132
spring-boot集成RabbitMQ
在Spring Boot中,可以使用spring-boot-starter-amqp库来轻松地集成RabbitMQ,并且封装了多种模式的实现方式。
1. 简单模式(Simplest Model):
```java
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendSimpleMessage(String message) {
rabbitTemplate.convertAndSend("simple-queue", message);
}
@RabbitListener(queues = "simple-queue")
public void receiveSimpleMessage(String message) {
System.out.println("Received message: " + message);
}
```
2. 工作队列模式(Work queues Model):
```java
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendWorkMessage(String message) {
rabbitTemplate.convertAndSend("work-queue", message);
}
@RabbitListener(queues = "work-queue")
public void receiveWorkMessage(String message) throws InterruptedException {
System.out.println("Received message: " + message);
Thread.sleep(1000); // 模拟消费者耗时操作
}
```
3. 发布/订阅模式(Publish/Subscribe Model):
```java
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendPublishMessage(String message) {
rabbitTemplate.convertAndSend("fanout-exchange", "", message);
}
@RabbitListener(queues = "fanout-queue-1")
public void receivePublishMessage1(String message) {
System.out.println("Received message from queue 1: " + message);
}
@RabbitListener(queues = "fanout-queue-2")
public void receivePublishMessage2(String message) {
System.out.println("Received message from queue 2: " + message);
}
```
4. 路由模式(Routing Model):
```java
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendRoutingMessage(String message, String routingKey) {
rabbitTemplate.convertAndSend("direct-exchange", routingKey, message);
}
@RabbitListener(queues = "direct-queue-1")
public void receiveRoutingMessage1(String message) {
System.out.println("Received message from queue 1: " + message);
}
@RabbitListener(queues = "direct-queue-2")
public void receiveRoutingMessage2(String message) {
System.out.println("Received message from queue 2: " + message);
}
```
5. 主题模式(Topic Model):
```java
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendTopicMessage(String message, String routingKey) {
rabbitTemplate.convertAndSend("topic-exchange", routingKey, message);
}
@RabbitListener(queues = "topic-queue-1")
public void receiveTopicMessage1(String message) {
System.out.println("Received message from queue 1: " + message);
}
@RabbitListener(queues = "topic-queue-2")
public void receiveTopicMessage2(String message) {
System.out.println("Received message from queue 2: " + message);
}
```
6. 随机模式(Headers Model):
```java
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendHeadersMessage(String message, Map<String, Object> headers) {
MessageProperties messageProperties = new MessageProperties();
messageProperties.getHeaders().putAll(headers);
Message messageObj = MessageBuilder.withBody(message.getBytes())
.andProperties(messageProperties)
.build();
rabbitTemplate.send("headers-exchange", "", messageObj);
}
@RabbitListener(queues = "headers-queue-1")
public void receiveHeadersMessage1(String message) {
System.out.println("Received message from queue 1: " + message);
}
@RabbitListener(queues = "headers-queue-2")
public void receiveHeadersMessage2(String message) {
System.out.println("Received message from queue 2: " + message);
}
```
以上是Spring Boot中封装RabbitMQ的6种模式的简单示例。实际使用时,可能需要更加复杂的逻辑,例如消息确认、消息重试、死信队列等功能。
阅读全文