title: How can I catch connection exception using Spring RabbitMQ and MongoDB?
body: <p>I'm using Spring and I'm getting the exception when the RabbitMQ is down the exception is thrown. I want to be able to catch it in order to print anything like "Retrying Connection".   </p>
  
  <pre><code>org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused (Connection refused)
          at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:62) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:510) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:751) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:215) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2085) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2058) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2038) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:407) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:391) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1820) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1801) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.initialize(SimpleMessageListenerContainer.java:1337) [spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1183) [spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
          at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
          at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
          at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.7.3.jar!/:5.7.3]
          at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1113) ~[amqp-client-5.7.3.jar!/:5.7.3]
          at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1063) ~[amqp-client-5.7.3.jar!/:5.7.3]
          at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:526) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
          at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:473) ~[spring-rabbit-2.2.2.RELEASE.jar!/:2.2.2.RELEASE]
  </code></pre>
  
  <p>The same happens with MongoDB when losing connection which I would like do the same.</p>
  
  <pre><code>com.mongodb.MongoSocketException: mongo: Name does not resolve
              at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:211) ~[mongo-java-driver-3.10.1.jar!/:na]
              at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:75) ~[mongo-java-driver-3.10.1.jar!/:na]
              at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongo-java-driver-3.10.1.jar!/:na]
              com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.10.1.jar!/:na]
              at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:131) ~[mongo-java-driver-3.10.1.jar!/:na]
              at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]
  Caused by: java.net.UnknownHostException: mongo: Name does not resolve
              at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_212]
              at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:929) ~[na:1.8.0_212]
              at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324) ~[na:1.8.0_212]
              at java.net.InetAddress.getAllByName0(InetAddress.java:1277) ~[na:1.8.0_212]
              at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_212]
              at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_212]
              at com.mongodb.ServerAddress.getSocketAddresses(ServerAddress.java:203) ~[mongo-java-driver-3.10.1.jar!/:na]
              ... 5 common frames omitted
  </code></pre>
  
  <p>Is there a way to catch them using spring?</p>