exception handling for rabbitmq listener in spring

ε祈祈猫儿з 提交于 2019-12-11 02:38:12

问题


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

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.

My spring beans:

<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"/>

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

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?


回答1:


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.

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.

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

EDIT

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

}

App:

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

}


来源:https://stackoverflow.com/questions/43208940/exception-handling-for-rabbitmq-listener-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!