application.yml 配置 无需在yml 文件中配置 ack 等 属性
spring:
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
初始化一个 rabbitmq 的配置组件
@Component
public class RabbitConfig {
}
配置消费者的 SimpleRabbitListenerContainerFactory
@Bean(name = "listenerContainer")
public SimpleRabbitListenerContainerFactory listenerContainer(){
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
// 手动确认模式
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
//todo ....
return factory;
}
配置消费者监听组件
@Component
public class RabbitListener{
}
消费者监听配置
@RabbitListener(queues = "队列名称" ,containerFactory = "listenerContainer")
@RabbitHandler
public void ConsumeMessage(Channel channel, Message message) {
try {
//确认收到消息
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
//.... 可以丢弃消息或重入队列
}
}
生产者无需处理是否确认消息
网友评论