如何监听通道关闭事件?每当通道打开一次。

huangapple go评论70阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年3月31日 18:29:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定