0
votes

I use spring amqp to send msg, and add the resend logic if ack=false, with the same rabbitTemplate

 @Bean
public RabbitTemplate customRabbitTemplate(ConnectionFactory connectionFactory) {
   RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
   rabbitTemplate.setMessageConverter(jackson2JsonMessageConverter());
   rabbitTemplate.setMandatory(true);
   rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {

       RabbitMetaMessage metaMessage = (RabbitMetaMessage) mqMsgUtil.getMetaMsg(cacheKey);
       //1 receive ack
       if (ack) {
       // send success   
           mqMsgUtil.setMsgSuccess(cacheKey);
           SUCESS_SEND = true;
       // send failed
       } else {

           reSendMsg(cacheKey, metaMessage, rabbitTemplate);
       }
   });

  public void reSendMsg(String msgId, RabbitMetaMessage rabbitMetaMessage,RabbitTemplate rabbitTemplate) {
     rabbitTemplate.convertAndSend(rabbitMetaMessage)
     .....
  }

I can "1 receive ack" when I send msg first time, but when in seSendMsg and send msg again with RabbitTemplate, I can not receive ack again. How this happans?

1

1 Answers

1
votes

Sending a message on the ack callback is not allowed; it causes a deadlock in the client library. You need to do the re-send on a different thread.

In the next release (2.1) we have changed the code to invoke the callback on a different thread to avoid the user code having to do it. See AMQP-817 and its linked answer.