Broker Event Listener

如果启用了 Event Exchange Plugin,如果在应用程序上下文中添加了类型为 BrokerEventListener 的 bean,则它会将选定的代理事件发布为 BrokerEvent 实例,这些实例可以使用普通的 Spring ApplicationListener@EventListener 方法使用。事件由代理发布到主题交换 amq.rabbitmq.event,其中每个事件类型都有不同的路由键。侦听器使用事件键,用于将 AnonymousQueue 绑定到交换,以便侦听器仅接收选定的事件。由于它是主题交换,因此可以使用通配符(以及明确请求特定事件),如下例所示:

When the Event Exchange Plugin is enabled, if you add a bean of type BrokerEventListener to the application context, it publishes selected broker events as BrokerEvent instances, which can be consumed with a normal Spring ApplicationListener or @EventListener method. Events are published by the broker to a topic exchange amq.rabbitmq.event with a different routing key for each event type. The listener uses event keys, which are used to bind an AnonymousQueue to the exchange so the listener receives only selected events. Since it is a topic exchange, wildcards can be used (as well as explicitly requesting specific events), as the following example shows:

@Bean
public BrokerEventListener eventListener() {
    return new BrokerEventListener(connectionFactory(), "user.deleted", "channel.#", "queue.#");
}

你可以通过使用正常的 Spring 技术,进一步缩小在单个事件监听器中接收的事件,如下例所示:

You can further narrow the received events in individual event listeners, by using normal Spring techniques, as the following example shows:

@EventListener(condition = "event.eventType == 'queue.created'")
public void listener(BrokerEvent event) {
    ...
}