spring对Rabbitmq侦听器的异常处理


问题内容

使用spring,我是Rabbitmq的新手,我想知道我错了。

我写了一个Rabbitmq连接工厂,以及一个包含侦听器的侦听器容器。我还为侦听器容器提供了一个错误处理程序,但它似乎不起作用。

我的春豆:

<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类:

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方法,并且服务器应该正确启动,但是,当我尝试运行服务器时,该方法无法执行(例外是在控制台中抛出),并且服务器无法启动。我在哪里缺少什么,那可能是什么?


问题答案:

错误处理程序用于在消息传递过程中处理错误。由于您尚未连接,因此没有消息可用于处理错误。

要获取连接异常,您应该实现,ApplicationListener<ListenerContainerConsumerFailedEvent>并且如果将它作为Bean添加到应用程序上下文中,则会将失败作为事件接收。

如果实施,则会得到其他事件(消费者启动,消费者停止等)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" />

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);
    }

}