赞
踩
rabbitmq 设置ack不生效,重复答应的问题
在配置文件中设置了manual依然无效
spring: rabbitmq: host: *** port: *** username: *** password: *** virtualHost: /ntrip listener: simple: # Minimum number of consumers. concurrency: 1 # Maximum number of consumers. max-concurrency: 20 # 手动确定(默认自动确认) acknowledge-mode: manual # 从spring-amqp 2.0版开始,默认的prefetch值是250 prefetch: 50 # 消息被拒后(即未消费),重新(true)放入队列 default-requeue-rejected: true retry: # 是否开启消费者重试(为false时关闭消费者重试,这时消费端代码异常会一直重复收到消息) enabled: true # 消费端发现了异常,尝试了规定次数后,这条“问题消息”就会被解决(如果设置了死信队列,就被送到了死信队列;否则直接扔掉)。如果不开启消费者重试尝试模式,那么会无限的循环下去控制台一直报错 max-attempts: 3 initial-interval: 5000ms # 启用发布者返回。 publisher-returns: true # 与 CorrelationData 一起使用以关联已发送的确认 publisher-confirm-type: correlated
代码设置监听:
/** * 监听 routingKey * * @param message 消息详情 * @param channel 当前mq会话通道 */ @RabbitListener(queues = RabbitMQConfiguration.SEND_MAIL_QUEUE) public void sendMailQueueListener(String content, Message message, Channel channel) { try { String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey(); log.info("收到路由key:{},发送对象:{}", receivedRoutingKey, content); RandomGenerator randomGenerator = new RandomGenerator(6); String generate = randomGenerator.generate(); EmailUtil.sendMail(content, "ntrip验证码", generate); HashMap<String, Object> mailCodeMap = MapUtil.newHashMap(16); mailCodeMap.put(content, generate); RedisUtil.hmset(RedisConstants.REGISTER_MAIL_CODE, mailCodeMap, RedisConstants.REGISTER_CODE_EXPIRE); } finally { try { // 消息id,手动ack channel.basicAck(message.getMessageProperties().getDeliveryTag(), true); } catch (IOException e) { log.error("消息:{}发送ack失败", message.getMessageProperties().getMessageId()); log.error(e.getMessage(), e); } } }
依然一直异常:
Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - unknown delivery tag 1, class-id=60, method-id=80)
参照这个链接依然无效,不过大家可以去试试,这个帖子说得挺详细的。
没有细心去看源码,在看服务启动日志的时候发现,发现ack一直都是AUTO,估计是这个注解没有读取配置文件的 spring.rabbitmq.listener.simple.acknowledge-mode 。
我的解决办法:在注解上添加ackMode
@RabbitListener(queues = RabbitMQConfiguration.SEND_MAIL_QUEUE, ackMode = "MANUAL")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。