简体   繁体   English

处理RabbitMQ Spring Boot应用程序中的异常

[英]Handle the exceptions in RabbitMQ Spring Boot Application

I am using Spring Boot 1.4.1-RELEASE and RabbitMQ 3.2.3. 我使用的是Spring Boot 1.4.1-RELEASE和RabbitMQ 3.2.3。 My Application class looks like this - 我的Application类看起来像这样 -

@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. 我不知道在哪里可以获得异常的句柄。

You can create a SimpleRabbitListenerContainerFactory. 您可以创建SimpleRabbitListenerContainerFactory。 This is basically a listener for events from RabbitConnectionFactory. 这基本上是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 . rabbitErrorHandler()可以返回org.springframework.util.ErrorHandler的实现bean。

Reference docs 参考文档

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. 由于您希望对RabbitMQ代理拒绝连接,因此客户端可以捕获异常。
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): 在您的示例中,看起来像是来自SpringIO文档的示例 ,我假设您可以在main中进行异常处理(不推荐):

@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. AmqpException来自convertAndSend convertAndSend()方法的文档,如果出现问题,则会抛出该方法。 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 /一种

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM