spring rabbitmq等待确认超时

时间:2017-09-20 08:54:06

标签: spring-rabbitmq

spring-rabbit version:1.7.4.RELEASE

这是我的代码:

@Configuration
public class RabbitmqConfiguration {

    public RabbitmqConfiguration(RabbitTemplate rabbitTemplate,ConfirmCallback confirmCallback) throws Exception {
        rabbitTemplate.setConfirmCallback(confirmCallback);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter(mapper));
    }

}

@Component
@Slf4j
public class OrderStatusChangeComponentImpl implements OrderStatusChangeComponent,ConfirmCallback{
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private OrderMessageLogComponent orderMessageLogComponent;
    @Autowired
    private Gson gson;

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback#
     * confirm(org.springframework.amqp.rabbit.support.CorrelationData, boolean,
     * java.lang.String)
     */
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        long nowTime = System.nanoTime();

    String uuid = correlationData.getId();
    if (ack) {
        orderMessageLogComponent.deleteOrderMessageLogByUUID(uuid);
    } else {
        log.error(cause, nowTime);
    }
}

我测试rabbitmq通过jmeter发送msg大约512个线程和1000个循环;

我看到日志有这么多错误。 通道关闭:清洁通道关闭;协议方法:#method<channel.close>(reply-code=406, reply-text=TIMEOUT WAITING FOR ACK, class-id=0, method-id=0)

最后我的应用程序无法连接rabbitmq。 顺便说一句,我的rabbitmq服务器是健康的。

2 个答案:

答案 0 :(得分:0)

尝试在send()内执行RabbitTemplate.invoke(),并在超时时间内调用template.waitForConfirmsOrDie()

如果您使用invoke()并且不这样做,框架将只等待5000毫秒进行确认。

如果您没有使用invoke(),则不清楚如何获得该错误。

答案 1 :(得分:0)

public void confirm(CorrelationData correlationData, boolean ack, String cause) {
    long nowTime = System.nanoTime();
    String uuid = correlationData.getId();
    if (ack) {
        orderMessageLogComponent.deleteOrderMessageLogByUUID(uuid);
    } else {
        log.error("消息发送失败,消息唯一标识为{},具体原因为{},当前时间为{}", uuid, cause, nowTime);
    }
}

@Override
@Async(MsgSendAsyncConfig.MSGSEND_SYNC_POOL)
public void deleteOrderMessageLogByUUID(String uuid) {
    orderMessageLogService.deleteOrderMessageLogByUUID(uuid);
}