spring中rabbitmq监听器的异常处理 [英] exception handling for rabbitmq listener in spring

查看:261
本文介绍了spring中rabbitmq监听器的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与spring一起工作,我是rabbitmq的新手,我想知道我错在哪里.

Working with spring, I am new to rabbitmq, i want to know where i am wrong.

我编写了一个rabbitmq 连接工厂和一个包含监听器的监听器容器.我还为侦听器容器提供了一个错误处理程序,但它似乎不起作用.

I have written a rabbitmq connection factory, and a listener container containing a listener. I have also provided the listener container with an error handler but it doesnt seems to work.

我的春豆:

<rabbit:connection-factory id="RabbitMQConnectionFactory" virtual-host="${rabbitmq.vhost}" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}"/>
<rabbit:listener-container missing-queues-fatal="false" declaration-retries="0" error-handler="errorHandlinginRabbitMQ" recovery-interval="10000" auto-startup="${rabbitmq.apc.autostartup}" max-concurrency="1" prefetch="1" concurrency="1" connection-factory="RabbitMQConnectionFactory" acknowledge="manual">
    <rabbit:listener ref="apcRabbitMQListener" queue-names="${queue.tpg.rabbitmq.destination.apc}" exclusive="true" />
</rabbit:listener-container>
<bean id="errorHandlinginRabbitMQ" class="RabbitMQErrorHandler"/>

这是我的 RabbitMQErrorHandler 类:

This is my RabbitMQErrorHandler class:

public class RabbitMQErrorHandler implements ErrorHandler
{
    @Override
    public void handleError(final Throwable exception)
    {
        System.out.println("error occurred in message listener and handled in error handler" + exception.toString());
    }
}

我的假设是,如果我向连接工厂提供无效凭据,RabbitMQErrorHandler 类的 handleError 方法应该执行,并且服务器应该正确启动,但是,当我尝试运行服务器时,该方法不会执行(在控制台中抛出异常)并且服务器无法启动.我在哪里遗漏了什么?可能是什么?

What i assume is, if i provide invalid credentials to the connection factory, handleError method of the RabbitMQErrorHandler class should execute, and the server should start properly, however, when i try to run the server, the method does not executes(the exception is thrown in console) and the server is not able to start. Where am i missing something and what that might be?

推荐答案

error handler 用于处理消息传递过程中的错误;由于您尚未连接,因此没有消息可用于处理错误.

The error handler is for handling errors during message delivery; since you haven't connected yet, there is no message for which to handle an error.

要获得连接异常,您应该实现 ApplicationListener,如果您将它作为 bean 添加到应用程序上下文中,您将收到作为事件的失败.

To get connection exceptions, you should implement ApplicationListener<ListenerContainerConsumerFailedEvent> and you will receive the failure as an event if you add it as a bean to the application context.

如果您实现 ApplicationListener,您将获得其他事件(消费者启动、消费者停止等).

You will get other events (consumer started, consumer stopped etc) if you implement ApplicationListener<AmqpEvent>.

编辑

<rabbit:listener-container auto-startup="false">
    <rabbit:listener id="fooContainer" ref="foo" method="handleMessage" 
               queue-names="si.test.queue" />
</rabbit:listener-container>

<bean id="foo" class="com.example.Foo" />

福:

public class Foo {

    public final CountDownLatch latch = new CountDownLatch(1);

    public void handleMessage(String foo) {
        System.out.println(foo);
        this.latch.countDown();
    }

}

应用:

@SpringBootApplication
@ImportResource("context.xml")
public class So43208940Application implements CommandLineRunner {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(So43208940Application.class, args);
        context.close();
    }

    @Autowired
    private SimpleMessageListenerContainer fooContainer;

    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Autowired
    private RabbitTemplate template;

    @Autowired
    private Foo foo;

    @Override
    public void run(String... args) throws Exception {
        this.connectionFactory.setUsername("junk");
        try {
            this.fooContainer.start();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        Thread.sleep(5000);
        this.connectionFactory.setUsername("guest");
        this.fooContainer.start();
        System.out.println("Container started");
        this.template.convertAndSend("si.test.queue", "foo");
        foo.latch.await(10, TimeUnit.SECONDS);
    }

}

这篇关于spring中rabbitmq监听器的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆