Spring ApplicationEvent 支援

Spring Integration 支援入站和出站 ApplicationEvents,這由底層的 Spring Framework 定義。有關 Spring 對事件和監聽器的支援的更多資訊,請參閱 Spring 參考手冊

你需要將此依賴新增到你的專案中

  • Maven

  • Gradle

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-event</artifactId>
    <version>6.4.4</version>
</dependency>
compile "org.springframework.integration:spring-integration-event:6.4.4"

接收 Spring Application Events

要接收事件並將其傳送到通道,你可以定義 Spring Integration 的 ApplicationEventListeningMessageProducer 例項。此類是 Spring 的 ApplicationListener 介面的實現。預設情況下,它將所有接收到的事件作為 Spring Integration 訊息傳遞。要根據事件型別進行限制,可以使用 'eventTypes' 屬性配置要接收的事件型別列表。如果接收到的事件的 'source' 是 Message 例項,則該 Message 將按原樣傳遞。否則,如果提供了基於 SpEL 的 payloadExpression,則會針對 ApplicationEvent 例項對其進行評估。如果事件的源不是 Message 例項且未提供 payloadExpression,則 ApplicationEvent 本身將作為載荷傳遞。

從 4.2 版本開始,ApplicationEventListeningMessageProducer 實現了 GenericApplicationListener,並且可以配置為不僅接受 ApplicationEvent 型別,還可以接受用於處理載荷事件的任何型別(Spring Framework 4.2 也支援此功能)。當接受的事件是 PayloadApplicationEvent 的例項時,其 payload 將用於傳送訊息。

為了方便起見,提供了 namespace 支援,可以使用 inbound-channel-adapter 元素配置 ApplicationEventListeningMessageProducer,如下例所示

<int-event:inbound-channel-adapter channel="eventChannel"
                                   error-channel="eventErrorChannel"
                                   event-types="example.FooEvent, example.BarEvent, java.util.Date"/>

<int:publish-subscribe-channel id="eventChannel"/>

在前面的示例中,所有與 'event-types'(可選)屬性指定的型別之一匹配的應用程式上下文事件都作為 Spring Integration 訊息傳送到名為 'eventChannel' 的訊息通道。如果下游元件丟擲異常,則包含失敗訊息和異常的 MessagingException 會發送到名為 'eventErrorChannel' 的通道。如果未指定 error-channel 且下游通道是同步的,則異常將傳播給呼叫者。

使用 Java 配置相同的介面卡

@Bean
public ApplicationEventListeningMessageProducer eventsAdapter(
            MessageChannel eventChannel, MessageChannel eventErrorChannel) {

    ApplicationEventListeningMessageProducer producer =
        new ApplicationEventListeningMessageProducer();
    producer.setEventTypes(example.FooEvent.class, example.BarEvent.class, java.util.Date.class);
    producer.setOutputChannel(eventChannel);
    producer.setErrorChannel(eventErrorChannel);
    return producer;
}

使用 Java DSL

@Bean
public ApplicationEventListeningMessageProducer eventsAdapter() {

    ApplicationEventListeningMessageProducer producer =
        new ApplicationEventListeningMessageProducer();
    producer.setEventTypes(example.FooEvent.class, example.BarEvent.class, java.util.Date.class);
    return producer;
}

@Bean
public IntegrationFlow eventFlow(ApplicationEventListeningMessageProducer eventsAdapter,
        MessageChannel eventErrorChannel) {

    return IntegrationFlow.from(eventsAdapter, e -> e.errorChannel(eventErrorChannel))
        .handle(...)
        ...
        .get();
}

傳送 Spring Application Events

要傳送 Spring ApplicationEvents,請建立 ApplicationEventPublishingMessageHandler 的例項並在端點內註冊它。這個 MessageHandler 介面的實現也實現了 Spring 的 ApplicationEventPublisherAware 介面,因此充當了 Spring Integration 訊息和 ApplicationEvents 之間的橋樑。

為了方便起見,提供了 namespace 支援,可以使用 outbound-channel-adapter 元素配置 ApplicationEventPublishingMessageHandler,如下例所示

<int:channel id="eventChannel"/>

<int-event:outbound-channel-adapter channel="eventChannel"/>

如果你使用 PollableChannel(例如 QueueChannel),你還可以提供 outbound-channel-adapter 元素的子元素 poller。你還可以選擇為該輪詢器提供 task-executor 引用。以下示例展示了這兩種情況

<int:channel id="eventChannel">
  <int:queue/>
</int:channel>

<int-event:outbound-channel-adapter channel="eventChannel">
  <int:poller max-messages-per-poll="1" task-executor="executor" fixed-rate="100"/>
</int-event:outbound-channel-adapter>

<task:executor id="executor" pool-size="5"/>

在前面的示例中,所有傳送到 'eventChannel' 通道的 訊息 都作為 ApplicationEvent 例項釋出給在同一 Spring ApplicationContext 中註冊的任何相關 ApplicationListener 例項。如果 訊息 的載荷是 ApplicationEvent,則按原樣傳遞。否則,訊息 本身將被包裝在 MessagingEvent 例項中。

從 4.2 版本開始,你可以使用 publish-payload 布林屬性配置 ApplicationEventPublishingMessageHandler (<int-event:outbound-channel-adapter>),將 payload 按原樣釋出到應用程式上下文,而不是將其包裝到 MessagingEvent 例項中。

使用 Java 配置介面卡

@Bean
@ServiceActivator(inputChannel = "eventChannel")
public ApplicationEventPublishingMessageHandler eventHandler() {
    ApplicationEventPublishingMessageHandler handler =
            new ApplicationEventPublishingMessageHandler();
    handler.setPublishPayload(true);
    return handler;
}

使用 Java DSL

@Bean
public ApplicationEventPublishingMessageHandler eventHandler() {
    ApplicationEventPublishingMessageHandler handler =
            new ApplicationEventPublishingMessageHandler();
    handler.setPublishPayload(true);
    return handler;
}

@Bean
// MessageChannel is "eventsFlow.input"
public IntegrationFlow eventsOutFlow(ApplicationEventPublishingMessageHandler eventHandler) {
    return f -> f.handle(eventHandler);
}

@Publisher 註解也可以與 @EventListener 結合使用

@Configuration
@EnableIntegration
@EnablePublisher
public static class ContextConfiguration {

     @Bean
     QueueChannel eventFromPublisher() {
         return new QueueChannel();
     }

     @EventListener
     @Publisher("eventFromPublisher")
     public String publishEventToChannel(TestApplicationEvent3 testApplicationEvent3) {
         return testApplicationEvent3.getSource().toString();
     }

}

在這種情況下,事件監聽器方法的返回值將用作要釋出到 eventFromPublisher 通道的 Message 的載荷。有關 @Publisher 的更多資訊,請參閱註解驅動的配置部分。