简体   繁体   English

在Spring AMQP中关闭与SimpleMessageListenerContainer相关的连接

[英]Close connections related to SimpleMessageListenerContainer in Spring AMQP

I am currently working on an event-based async AMQP message listener, like this: 我目前正在研究基于事件的异步AMQP消息侦听器,如下所示:

@Configuration
public class ExampleAmqpConfiguration {

    @Bean(name = "container")
    public SimpleMessageListenerContainer messageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(rabbitConnectionFactory());
        container.setQueueName("some.queue");
        container.setMessageListener(exampleListener());
        return container;
    }

    @Bean
    public ConnectionFactory rabbitConnectionFactory() {
        CachingConnectionFactory connectionFactory =
            new CachingConnectionFactory("localhost");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        return connectionFactory;
    }

    @Bean
    public MessageListener exampleListener() {
        return new MessageListener() {
            public void onMessage(Message message) {
                System.out.println("received: " + message);
            }
        };
    }
}

I have changed the container bean's autostart property to false. 我已将容器bean的autostart属性更改为false。 And I have autowired this bean to an event lister that starts the container when StartEvent happens: 而且我已将该bean自动连接到事件列表器,该事件列表器在StartEvent发生时启动容器:

@EventListener
public void startContainer(StartEvent startEvent) {
     this.container.start();
}

At the same time, I have also autowired the bean to another event that stops the container and shutdowns the container, hoping that the container will be stopped and that there will be no lingering connection: 同时,我还将Bean自动连接到另一个事件,该事件将停止容器并关闭容器,希望该容器将被停止并且没有持久的连接:

@EventListener
public void endContainer(EndEvent endEvent) {
     this.container.stop();
     this.container.shutdown();
}

However, after the EndEvent, I find in my RabbitMQ admin console that all the channels are closed but there is still a lingering connection. 但是,在EndEvent之后,我在RabbitMQ管理控制台中发现所有通道均已关闭,但仍然存在挥之不去的连接。

Does that mean that shutdown() is not the right method to use to remove the lingering connection? 这是否意味着shutdown()不是删除持久连接的正确方法? If that's the case, what is the right method to use? 在这种情况下,正确的方法是什么?

Thanks. 谢谢。

You need to call resetConnection() on the CachingConnectionFactory to close the connection; 你需要调用resetConnection()CachingConnectionFactory关闭连接; as implied by the class name; 如类名所暗示; the connection is cached. 连接已缓存。

暂无
暂无

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

相关问题 Spring AMQP动态创建RabbitTemplate和SimpleMessageListenerContainer,错误RabbitTemplate未配置为MessageListener - Spring AMQP Dynamically create RabbitTemplate and SimpleMessageListenerContainer, error RabbitTemplate is not configured as MessageListener Spring AMQP中的SimpleMessageListenerContainer和DirectMessageListenerContainer有什么区别? - What's the difference between SimpleMessageListenerContainer and DirectMessageListenerContainer in Spring AMQP? 删除队列后,SimpleMessageListenerContainer上的Spring AMQP通知/事件 - Spring AMQP Notification/Events on SimpleMessageListenerContainer, when queue gets deleted 使用 Spring AMQP 在 AMQP 连接自动恢复期间出现 AlreadyClosedException - AlreadyClosedException during AMQP connections auto-recovery using Spring AMQP AMQP使用者(侦听器适配器+ SimpleMessageListenerContainer)持有消息 - AMQP consumer(listener adapter + SimpleMessageListenerContainer) holding message 如何使用spring-amqp关闭与Rabbit MQ代理的连接 - How to close connection with Rabbit MQ broker using spring-amqp 冬眠的春天并没有关闭 - hibernate spring doesn t close connections Spring JmsTemplate是否默认关闭连接? - Does Spring JmsTemplate close connections by default? 使用RabbitMQ的Spring Integration DSL中的SimpleMessageListenerContainer AbstractMethodError - SimpleMessageListenerContainer AbstractMethodError in Spring Integration DSL with RabbitMQ RabbitMQ的Spring SimpleMessageListenerContainer正在中止无效消息 - Spring SimpleMessageListenerContainer for RabbitMQ is aborting on invalid message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM