英文:
How do I hook into Channel closed event? Once per channel opened
问题
代码部分不进行翻译,以下是翻译好的内容:
I have an annotation-based Rabbit consumer represented by a @RabbitListener
-annotated method.
I also have a configuration class to configure my Factory e.g.
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
ConnectionFactory connectionFactory) {
var factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
...
return factory;
}
Sometimes my listener's code hits a PRECONDITION_FAILED error because it passes the broker's delivery acknowledgement timeout. I want to run some handling code whenever this happens.
I know that channel.addShutdownListener()
exists but I don't know where to put it. I can't use the listener's method channel reference because that would add a listener for every message I process, which is not ideal.
I also understand that when a channel is closed, the listener will internally keep using the same ListenerContainer but with a new channel, so I will need to register the shutdown listener with the new channel in that case.
Basically, I need to add my handling code ONCE to each channel that is opened for my message listener.
英文:
I have an annotation-based Rabbit consumer represented by a @RabbitListener
-annotated method.
I also have a configuration class to configure my Factory e.g.
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(
ConnectionFactory connectionFactory) {
var factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
...
return factory;
}
Sometimes my listener's code hits a PRECONDITION_FAILED error because it passes the broker's delivery acknowledgement timeout. I want to run some handling code whenever this happens.
I know that channel.addShutdownListener()
exists but I don't know where to put it. I can't use the listener's method channel reference because that would add a listener for every message I process, which is not ideal.
I also understand that when a channel is closed, the listener will internally keep using the same ListenerContainer but with a new channel, so I will need to register the shutdown listener with the new channel in that case.
Basically, I need to add my handling code ONCE to each channel that is opened for my message listener.
答案1
得分: 1
查看 AbstractConnectionFactory.addChannelListener(ChannelListener)
配置。
https://docs.spring.io/spring-amqp/docs/current/reference/html/#connection-channel-listeners
英文:
See AbstractConnectionFactory.addChannelListener(ChannelListener)
configuration.
https://docs.spring.io/spring-amqp/docs/current/reference/html/#connection-channel-listeners
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论