| 此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.3.1! | 
| 此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.3.1! | 
Spring Integration 提供对入站和出站的支持,由底层 Spring Framework 定义。
有关 Spring 对事件和侦听器的支持的更多信息,请参阅 Spring 参考手册。ApplicationEvents
您需要将此依赖项包含在项目中:
- 
Maven 
- 
Gradle 
<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-event</artifactId>
    <version>6.3.2-SNAPSHOT</version>
</dependency>compile "org.springframework.integration:spring-integration-event:6.3.2-SNAPSHOT"接收 Spring 应用程序事件
要接收事件并将其发送到通道,您可以定义 Spring Integration 的 .
这个类是 Spring 接口的实现。
默认情况下,它将所有收到的事件作为 Spring Integration 消息传递。
若要根据事件类型进行限制,可以使用“eventTypes”属性配置要接收的事件类型列表。
如果接收到的事件具有实例作为其“源”,则按原样传递。
否则,如果提供了基于 SpEL 的,则会根据实例进行评估。
如果事件的源不是实例,并且未提供任何实例,则事件本身将作为有效负载传递。ApplicationEventListeningMessageProducerApplicationListenerMessageMessagepayloadExpressionApplicationEventMessagepayloadExpressionApplicationEvent
从版本 4.2 开始,实现并可以配置为不仅接受类型,而且接受任何类型来处理有效负载事件(自 Spring Framework 4.2 以来也支持这些事件)。
当接受的事件是 的实例时,它用于发送消息。ApplicationEventListeningMessageProducerGenericApplicationListenerApplicationEventPayloadApplicationEventpayload
为方便起见,提供了命名空间支持来配置 with the element,如以下示例所示:ApplicationEventListeningMessageProducerinbound-channel-adapter
<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”的消息通道。
如果下游组件引发异常,则包含失败消息和异常的组件将发送到名为“eventErrorChannel”的通道。
如果指定了 no 并且下行通道是同步的,则异常将传播到调用方。MessagingExceptionerror-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 应用程序事件
要发送 Spring ,请创建一个实例并在端点中注册它。
该接口的实现也实现了 Spring 的接口,因此充当了 Spring Integration 消息和 之间的桥梁。ApplicationEventsApplicationEventPublishingMessageHandlerMessageHandlerApplicationEventPublisherAwareApplicationEvents
为方便起见,提供了命名空间支持来配置 with the element,如以下示例所示:ApplicationEventPublishingMessageHandleroutbound-channel-adapter
<int:channel id="eventChannel"/>
<int-event:outbound-channel-adapter channel="eventChannel"/>如果使用 (如 ),还可以提供该元素的子元素。
您还可以选择为该轮询器提供参考。
以下示例演示了这两者:PollableChannelQueueChannelpolleroutbound-channel-adaptertask-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”通道的所有消息都将作为实例发布到在同一 Spring 中注册的任何相关实例。
如果消息的有效负载是 ,则按原样传递。
否则,消息本身将包装在实例中。ApplicationEventApplicationListenerApplicationContextApplicationEventMessagingEvent
从版本 4.2 开始,您可以使用 boolean 属性配置 () 以按原样发布到应用程序上下文,而不是将其包装到实例。ApplicationEventPublishingMessageHandler<int-event:outbound-channel-adapter>publish-payloadpayloadMessagingEvent
要使用 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();
     }
}在这种情况下,事件侦听器方法的返回值用作要发布到该通道的有效负载。
有关注释驱动配置部分的详细信息。MessageeventFromPublisher@Publisher