title: Spring boot rabbitMq setExceptionHandler
body: <p>How can I convert the following code using the Spring framework?</p>
  <pre><code>ConnectionFactory factory = new ConnectionFactory();
  factory.setExceptionHandler(new BrokerExceptionHandler(logger, instance));
  
  public final class BrokerExceptionHandler extends StrictExceptionHandler {
     @Override
     public void handleReturnListenerException(Channel channel, Throwable exception) {
          logger.log(Level.SEVERE, &quot;ReturnListenerException detected: ReturnListener.handleReturn&quot;, exception);
          this.publishAlert(exception, &quot;ReturnListener.handleReturn&quot;);
          logger.log(Level.SEVERE, &quot;Close application&quot;, exception);
          System.exit(-1);
     }
    ....
  }
  </code></pre>
  <p>Basically I need to specify a custom exception handler if a rabbitMQ exception occurs and then stop the application</p>
  <p>How can I publish a rabbitMq message every time that there is an exception?</p>
  <p><strong>EDIT</strong></p>
  <p>I modified my configuration class in this way:</p>
  <pre><code>@Bean
  SimpleMessageListenerContainer containerPredict(ConnectionFactory connectionFactory,
    MessageListenerAdapter listenerPredictAdapter) {
       SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
       container.setConnectionFactory(connectionFactory);
       container.setDefaultRequeueRejected(false);
       container.setErrorHandler(new BrokerExceptionHandler());
       container.setQueueNames(getQueueName());
       container.setMessageListener(listenerAdapter);
       return container;
  }
  </code></pre>
  <p>and this is my BrokerExceptionHandler class</p>
  <pre><code>public class BrokerExceptionHandler implements ErrorHandler {
     private final Logger logger = Logger.getLogger(getClass().getSimpleName());
  
     @Autowired
     private Helper helper;
  
     @Override
     public void handleError(Throwable t) {
       logger.log(Level.SEVERE, &quot;Exception Detected. Publishing error alert&quot;);
       String message = &quot;Exception detected. Message: &quot; + t.getMessage());
       
       // Notify the error to the System sending a new RabbitMq message
       System.out.println(&quot;---&gt; Before convertAndSend&quot;);
       rabbitTemplate.convertAndSend(exchange, routing, message);
       System.out.println(&quot;---&gt; After convertAndSend&quot;);
     }
  }
  </code></pre>
  <p>I can see the log <code>Exception Detected. Publishing error alert</code> and <code>---&gt; Before convertAdnSend</code> in the console, but the new alert is not published and the log <code>---&gt; After convertAndSend</code> doesn't appear in the console.</p>
  <p>Here it is the log:</p>
  <blockquote>
  <p>2018-10-17 09:32:02.849 ERROR 1506 --- [tainer****-1] BrokerExceptionHandler                   : Exception Detected. Publishing error alert</p>
  <p>---&gt; Before convertAndSend</p>
  <p>2018-10-17 09:32:02.853  INFO 1506 --- [tainer****-1] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer@4f5b08d: tags=[{amq.ctag-yUcUmg5BCo20ucG1wJZoWA=myechange}], channel=Cached Rabbit Channel: AMQChannel(amqp://admin@XXX.XXX.XXX.XXX:5672/testbed_simulators,1), conn: Proxy@3964d79 Shared Rabbit Connection: SimpleConnection@61f39bb [delegate=amqp://admin@XXX.XXX.XXX.XXX:5672/testbed_simulators, localPort= 51528], acknowledgeMode=AUTO local queue size=0</p>
  <p>2018-10-17 09:32:02.905  INFO 1506 --- [tainer****-2] o.s.amqp.rabbit.core.RabbitAdmin         : Auto-declaring a non-durable, auto-delete, or exclusive Queue (myexchange) durable:false, auto-delete:true, exclusive:true. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.</p>
  <p>2018-10-17 09:32:02.905  INFO 1506 --- [tainer****-2] o.s.amqp.rabbit.core.RabbitAdmin         : Auto-declaring a non-durable, auto-delete, or exclusive Queue (myexchange) durable:false, auto-delete:true, exclusive:true. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.</p>
  </blockquote>
  <p><strong>EDIT</strong></p>
  <p>Debugging I see that before to send the new message, the following code is called:</p>
  <p><code>File: SimpleMessageListenerContainer.class line 1212</code></p>
  <pre><code>if (!isActive(this.consumer) || aborted) {
    .....
  }
  else {
    ---&gt; logger.info(&quot;Restarting &quot; + this.consumer);
         restart(this.consumer);
  }
  </code></pre>
  <p><strong>EDIT 2</strong></p>
  <p>Example code:  <a href="http://github.com/fabry00/spring-boot-rabbitmq" rel="nofollow noreferrer">http://github.com/fabry00/spring-boot-rabbitmq</a></p>