如何在连接错误后使用springboot应用程序为rabbitmq建立连接

ztmd8pv5  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(297)

在将springboot应用程序重新部署到tomcatserver 9中后,我遇到了一些奇怪的异常:
myapplication:2021-05-17 09:54:39306错误[amqp连接主机:端口]
o、 s.a.r.c.cachingconnectionfactory:1576-通道关闭:连接错误,此后我无法从rabbitmq获取任何消息。
如果我重新启动我的应用程序,那么没有例外。
我是否需要更改springboot应用程序中的某些内容,以便在出现这些错误后自动重新连接?
或者我错过了什么?
这是我的SimpleRableBitListenerContainerFactory设置:

@Bean
public SimpleRabbitListenerContainerFactory poxStatusFeedbackInterceptContainerFactory(final SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                                       final ConnectionFactory connectionFactory) {
    SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory() {
        @Override
        protected void initializeContainer(SimpleMessageListenerContainer instance, RabbitListenerEndpoint endpoint) {
            instance.setAfterReceivePostProcessors(new MyPostProcessor()); // this does some prevalidation of messages
            super.initializeContainer(instance, endpoint);
        }
    };
    configurer.configure(factory, connectionFactory);
    return factory;
}

这是application.yml设置:

spring:
  rabbitmq:
    host: .......
    port: ....
    virtual-host: myhost
    username: sa
    password: sa
    listener:
      simple:
        retry:
          enabled: true
          initial-interval: 3s
          max-interval: 10s
          multiplier: 2
          max-attempts: 3
bvjxkvbb

bvjxkvbb1#

这将解决问题:

((CachingConnectionFactory) connectionFactory).setRequestedHeartBeat(60);

可在此处找到:https://www.rabbitmq.com/heartbeats.html

相关问题