title: How to fix error "Only one ConfirmCallback is supported by each RabbitTemplate" on subsequent requests
body: <p>I am new to RabbitMQ and currently working on RabbitMQ publish confirms acknowledgments in my web application. On every new request, sending message to RabbitMQ server to process it asynchronously and also enabled confirms to get acknowledgments from the RMQ server. </p>
  
  <p>When trying in local, on the first request it is fine but on subsequent requests, getting following error "Only one ConfirmCallback is supported by each RabbitTemplate". </p>
  
  <p>After some research, if I add a check (template.isConfirmListener()) before setting confirm callback, not getting error. But how can we override Confirm Callbacks for different requests if required? Is this expected behaviour or am I am doing anything wrong here? </p>
  
  <p><strong>Environment:</strong></p>
  
  <ol>
  <li>Spring Boot version 2.1.7.RELEASE</li>
  <li>Java 8</li>
  </ol>
  
  <p>Please find the code as below.</p>
  
  <p><strong>Controller file</strong></p>
  
  <pre><code>package com.example.apis;
  
  import java.util.UUID;
  
  import org.springframework.amqp.rabbit.connection.CorrelationData;
  import org.springframework.amqp.rabbit.core.RabbitTemplate;
  import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.ResponseBody;
  
  
  
  @Controller
  public class ProducerController {
  
      @Autowired
      private RabbitTemplate template;
  
      private String exchange = "test";
      private String routingKey = "test";
  
      @GetMapping("/send")
      @ResponseBody
      public String sendRequestToRMQ() {
  
  
          template.convertAndSend(exchange, routingKey, "Test Message", getCorrelationData());
          template.setMandatory(true);
          template.setConfirmCallback(new ConfirmCallback() {
  
              @Override
              public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                  System.out.println("Confirm Callback!");
                  System.out.println(correlationData.getId());
                  System.out.println(ack);
              }
          });
  
          return "Success";
      }
  
      @GetMapping("/send1")
      @ResponseBody
      public String sendRequestToRMQ2() {
  
  
          template.convertAndSend(exchange, routingKey, "Test Message", getCorrelationData());
          template.setMandatory(true);
          template.setConfirmCallback(new ConfirmCallback() {
  
              @Override
              public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                  System.out.println("Confirm Callback!");
                  System.out.println(correlationData.getId());
                  System.out.println(ack);
              }
          });
  
          return "Success";
      }
  
      private CorrelationData getCorrelationData() {
          return new CorrelationData(UUID.randomUUID().toString());
      }
  }
  </code></pre>
  
  <p><strong>Configuration file</strong></p>
  
  <pre><code>package com.example.conf;
  
  import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
  import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  @Configuration
   public class ConnectionConfig {
       @Value("${spring.rabbitmq.host:localhost}")
       public String host;
  
       @Value("${spring.rabbitmq.port:5672}")
       public int port;
  
       @Value("${spring.rabbitmq.username:guest}")
       public String username;
  
       @Value("${spring.rabbitmq.password:guest}")
       public String password;
  
       @Value("${spring.rabbitmq.virtual-host:/}")
       public String virtualHost;
  
       @Bean
       public ConnectionFactory getConnectionFactory(){
           CachingConnectionFactory factory=new CachingConnectionFactory();
           factory.setHost(host);
           factory.setPort(port);
           factory.setUsername(username);
           factory.setPassword(password);
           factory.setVirtualHost(virtualHost);
           factory.setPublisherConfirms(true);
           factory.setPublisherReturns(true);
           return factory;
       }
   }
  </code></pre>
  
  <p>Please help me. Thanks</p>