问题
Can anyone please guide on how to programatically register an ApplicationListener in DirectMessageListenerContainer(spring rabbitmq) to handle the ListenerContainerConsumerFailedEvent
Thanks In Advance.
回答1:
The listener is not registered to the container; it is simply added as a @Bean
to the Spring application context; the container publishes the event and Spring dispatches it to each registered listener.
EDIT
Don't put code in comments; they don't render well - always edit the question instead and add a comment that you have done so.
Creating containers like that means you will miss all the Spring goodness and injected dependencies; it's better to add a DirectRabbitListenerContainerFactory
@Bean
to the application context and use it to create containers - that way the event publisher will be injected.
You only need one listener; it will receive events from all containers.
If you are not using Spring at all (except spring-rabbit) then you need to satisfy all of the container's ...Aware
interfaces yourself and call afterPropertiesSet()
.
In this case you would call setApplicationEventPublisher()
with your custom publisher which will receive the event(s).
来源:https://stackoverflow.com/questions/58370414/how-to-implement-an-applicationlistener-in-directmessagelistenercontainer