|
对于最新稳定版本,请使用 Spring Integration 7.0.0! |
SpringApplicationEvent支持
Spring Integration 支持入站和出站应用事件,由底层的Spring框架定义。
有关春季对活动和听众的支持的更多信息,请参阅春季参考手册。
你需要把这种依赖性纳入你的项目中:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-event</artifactId>
<version>6.2.11</version>
</dependency>
compile "org.springframework.integration:spring-integration-event:6.2.11"
接收春季申请活动
要接收事件并将其发送到通道,你可以定义 Spring Integration 的实例应用程序事件监听消息制作器.
该类是 Spring 的实现ApplicationListener接口。
默认情况下,它会将所有收到的事件作为 Spring 集成消息传递。
为了根据事件类型进行限制,你可以使用 'eventTypes' 属性来配置你想接收的事件类型列表。
如果接收事件具有消息实例作为其“来源”,即消息按现状通过。
否则,如果是基于特殊语言的payloadExpression已被提供,且该参数会根据ApplicationEvent实例。
如果事件的源头不是消息实例和否payloadExpression已被提供,ApplicationEvent它本身作为有效载荷传递。
从4.2版本开始,应用程序事件监听消息制作器实现GenericApplicationListener并且可以配置为接受ApplicationEvent类型但任何用于处理有效载荷事件的类型(自 Spring Framework 4.2 起也支持有效载荷事件)。
当接受事件是PayloadApplicationEvent其有效载荷用于发送消息。
为方便起见,提供了命名空间支持以配置应用程序事件监听消息制作器其中入站信道适配器元素,如下例所示:
<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”的通道。
如果没有错误信道是指定且下游信道是同步的,例外会传播给呼叫者。
使用 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 集成消息之间的桥梁应用事件.
为方便起见,提供了命名空间支持以配置应用程序事件发布消息处理程序其中出站通道适配器元素,如下例所示:
<int:channel id="eventChannel"/>
<int-event:outbound-channel-adapter channel="eventChannel"/>
如果你使用Pollable频道(例如队列通道你也可以提供一个轮询者子元素出站通道适配器元素。
你也可以选择性地提供任务执行者那个民调器的参考资料。
以下示例展示了这两种情况:
<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在同一个春季内注册的实例应用上下文.
如果消息的有效载荷是ApplicationEvent,它按原样传递。
否则,消息本身会被包裹在消息事件实例。
从4.2版本开始,你可以配置应用程序事件发布消息处理程序 (<int-event:出站-通道-适配器>)发布有效载荷布尔属性以发布到应用上下文有效载荷原样,而不是将其包裹到消息事件实例。
使用 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();
}
}
在这种情况下,事件监听器方法的返回值被用作消息将向该机构发布出版社发布渠道。
查看更多关于@Publisher在注释驱动配置部分。