以下是详解SpringBoot整合RabbitMQ如何实现消息确认的完整攻略,包含两个示例说明。
示例1:手动确认消息
步骤1:添加依赖
在使用SpringBoot整合RabbitMQ时,您需要添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
步骤2:配置文件
在application.properties文件中添加以下配置:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
步骤3:发送消息
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Sender {
private final RabbitTemplate rabbitTemplate;
@Autowired
public Sender(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void send(String message) {
rabbitTemplate.convertAndSend("my_exchange", "my_routing_key", message);
}
}
步骤4:接收消息
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
@RabbitListener(queues = "my_queue")
public void receive(Message message) {
String content = new String(message.getBody());
System.out.println("Received message: " + content);
}
}
步骤5:手动确认消息
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
private final RabbitTemplate rabbitTemplate;
@Autowired
public Receiver(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@RabbitListener(queues = "my_queue")
public void receive(Message message) {
String content = new String(message.getBody());
System.out.println("Received message: " + content);
rabbitTemplate.convertAndSend("my_exchange", "my_acknowledgement_key", message.getMessageProperties().getCorrelationId());
}
}
示例2:自动确认消息
步骤1:添加依赖
同示例1。
步骤2:配置文件
同示例1。
步骤3:发送消息
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Sender {
private final RabbitTemplate rabbitTemplate;
@Autowired
public Sender(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
public void send(String message) {
rabbitTemplate.convertAndSend("my_exchange", "my_routing_key", message);
}
}
步骤4:接收消息
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
@RabbitListener(queues = "my_queue")
public void receive(String message) {
System.out.println("Received message: " + message);
}
}
步骤5:自动确认消息
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
@RabbitListener(queues = "my_queue", ackMode = "AUTO")
public void receive(String message) {
System.out.println("Received message: " + message);
}
}
以上就是SpringBoot整合RabbitMQ如何实现消息确认的完整攻略,包含两个示例说明。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解SpringBoot整合RabbitMQ如何实现消息确认 - Python技术站