abi_pat
abi_pat

Reputation: 602

Handle the exceptions in RabbitMQ Spring Boot Application

I am using Spring Boot 1.4.1-RELEASE and RabbitMQ 3.2.3. My Application class looks like this -

@SpringBootApplication
@EnableAutoConfiguration
public class EventStoreMessageDeliveryApplication {

    public final static String queueName = "customer.default.queue"; // spring-boot

    @Bean
    Queue queue() {
        return new Queue(queueName, true);
    }

    @Bean
    FanoutExchange exchange() {

        return new FanoutExchange("customer.events.fanout.exchange", true, false); // spring-boot-exchange
    }

    @Bean
    Binding binding() {

        return new Binding(queueName, Binding.DestinationType.QUEUE, "customer.events.fanout.exchange", "*.*", null);
    }

    @Bean
    public ConnectionFactory connectionFactory() {

        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
        connectionFactory.setPublisherConfirms(true);

        return connectionFactory;
    }

    @Bean
    SimpleMessageListenerContainer container(MessageListenerAdapter listenerAdapter) {

        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory());
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        container.setRecoveryBackOff(new ExponentialBackOff(3000, 2));
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {

        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    public static void main(String[] args) throws InterruptedException {

        SpringApplication.run(EventStoreMessageDeliveryApplication.class, args);
    }
}

And my listener class looks like -

@Component
public class Receiver {

    private CountDownLatch latch = new CountDownLatch(1);

    public void receiveMessage(String message) {

        System.out.println("Received <" + message + ">");

            // do something

        latch.countDown();
    }

    public CountDownLatch getLatch() {

        return latch;
    }

}

I want to handle the exceptions like connection refused which may come when the broker is down. How can I handle such exceptions? I am not sure where I can get the handle for the exceptions.

Upvotes: 4

Views: 4223

Answers (2)

Alexander Falk
Alexander Falk

Reputation: 521

I have a suggestion and it could work out. Since you want to have an exception of connection refused against the RabbitMQ broker, it is up to the client to catch the exception.
In your example, which looks like the one from SpringIO docs, I would assume you could make the exception handling in the main (not recommended though):

@Component
    public class Runner implements CommandLineRunner {

        private final RabbitTemplate rabbitTemplate;
        private final Receiver receiver;

        public Runner(Receiver receiver, RabbitTemplate rabbitTemplate) {
            this.receiver = receiver;
            this.rabbitTemplate = rabbitTemplate;
        }

        @Override
        public void run(String... args) throws Exception {
            System.out.println("Sending message...");
            try {
                rabbitTemplate.convertAndSend(Application.topicExchangeName, "foo.bar.baz", "Hello from RabbitMQ!");
                receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
            }catch(AmqpException the_exception) {
                System.out.printl("Connection refused. Problem thrown when trying to connecto the RabbitMQ");
            }            
         }

    }

The AmqpException comes from the docs of the convertAndSend() method, which is being thrown if something went bad. Here you can capture your own custom message.
I hope this is what you are looking for or atleast guides you the correct destination.

/A

Upvotes: 0

Praneeth Ramesh
Praneeth Ramesh

Reputation: 3564

You can create a SimpleRabbitListenerContainerFactory. This is basically a listener for events from RabbitConnectionFactory.

@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory());
    factory.setErrorHandler(rabbitErrorHandler());
    return factory;
}

rabbitErrorHandler() can return a bean of implementation of org.springframework.util.ErrorHandler.

Reference docs

Upvotes: 1

Related Questions