[英]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.