此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Integration 6.5.1spring-doc.cadn.net.cn

SpringApplicationEvent支持

Spring Integration 提供对入站和出站的支持ApplicationEvents,由底层 Spring Framework 定义。 有关 Spring 对事件和侦听器的支持的更多信息,请参阅 Spring 参考手册spring-doc.cadn.net.cn

项目需要此依赖项:spring-doc.cadn.net.cn

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

接收 Spring 应用程序事件

要接收事件并将其发送到通道,您可以定义 Spring Integration 的ApplicationEventListeningMessageProducer. 这个类是 Spring 的ApplicationListener接口。 默认情况下,它将所有收到的事件作为 Spring Integration 消息传递。 要根据事件类型进行限制,您可以使用“eventTypes”属性来配置要接收的事件类型列表。 如果收到的事件具有Message实例作为其“源”,即Message按原样传递。 否则,如果基于 SpELpayloadExpression已提供,则根据ApplicationEvent实例。 如果事件的来源不是Message实例,没有payloadExpression已提供,则ApplicationEvent本身作为有效负载传递。spring-doc.cadn.net.cn

从 4.2 版开始,ApplicationEventListeningMessageProducer实现GenericApplicationListener并且可以配置为不仅接受ApplicationEvent类型,但用于处理有效负载事件的任何类型(从 Spring Framework 4.2 开始也支持)。 当接受的事件是PayloadApplicationEventpayload用于发送消息。spring-doc.cadn.net.cn

为方便起见,提供了命名空间支持来配置ApplicationEventListeningMessageProducer使用inbound-channel-adapter元素,如以下示例所示:spring-doc.cadn.net.cn

<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指定了下游通道,并且下游通道是同步的,则异常将传播给调用方。spring-doc.cadn.net.cn

使用 Java 配置相同的适配器:spring-doc.cadn.net.cn

@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:spring-doc.cadn.net.cn

@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 应用程序事件

发送 SpringApplicationEvents,创建ApplicationEventPublishingMessageHandler并在终结点中注册它。 这种实现MessageHandler接口还实现了 Spring 的ApplicationEventPublisherAware接口,因此充当 Spring Integration 消息和ApplicationEvents.spring-doc.cadn.net.cn

为方便起见,提供了命名空间支持来配置ApplicationEventPublishingMessageHandler使用outbound-channel-adapter元素,如以下示例所示:spring-doc.cadn.net.cn

<int:channel id="eventChannel"/>

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

如果您使用PollableChannel(例如QueueChannel),您还可以提供pollerchild 元素的outbound-channel-adapter元素。 您还可以选择提供task-executor该轮询器的参考。 以下示例演示了两者:spring-doc.cadn.net.cn

<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实例添加到任何相关ApplicationListener在同一 Spring 中注册的实例ApplicationContext. 如果消息的有效负载是ApplicationEvent,则按原样传递。 否则,消息本身将包装在MessagingEvent实例。spring-doc.cadn.net.cn

从 4.2 版开始,您可以配置ApplicationEventPublishingMessageHandler (<int-event:outbound-channel-adapter>) 替换为publish-payload要发布到应用程序上下文的布尔属性payload按原样,而不是将其包装为MessagingEvent实例。spring-doc.cadn.net.cn

要使用 Java 配置配置适配器,请执行以下作:spring-doc.cadn.net.cn

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

使用 Java DSL:spring-doc.cadn.net.cn

@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:spring-doc.cadn.net.cn

@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();
     }

}

在这种情况下,事件侦听器方法的返回值用作Message将发布给该eventFromPublisher渠道。 查看有关@Publisher在“注释驱动配置”(Annotation-driven Configuration) 部分中。spring-doc.cadn.net.cn