首页 > 其他分享 >Spring支撑ApplicationEvent

Spring支撑ApplicationEvent

时间:2022-12-08 12:05:55浏览次数:42  
标签:ApplicationEventListeningMessageProducer producer Spring ApplicationEvent Applic

Spring支撑ApplicationEvent_Java

Spring 集成提供了对入站和出站的支持,由底层 Spring 框架定义。 有关 Spring 对事件和侦听器的支持的更多信息,请参阅 Spring 参考手册​。​​ApplicationEvents​

您需要将此依赖项包含在项目中:

<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-event</artifactId>
<version>6.0.0</version>
</dependency>

接收春季应用程序事件

要接收事件并将其发送到通道,您可以定义 Spring 集成的实例。 这个类是 Spring 接口的实现。 默认情况下,它将所有收到的事件作为 Spring 集成消息传递。 若要根据事件类型进行限制,可以使用“事件类型”属性来配置要接收的事件类型列表。 如果收到的事件将实例作为其“源”,则按原样传递。 否则,如果提供了基于 SpEL 的实例,则会针对实例进行评估。 如果事件的源不是实例并且未提供,则自身将作为有效负载传递。​​ApplicationEventListeningMessageProducer​​​​ApplicationListener​​​​Message​​​​Message​​​​payloadExpression​​​​ApplicationEvent​​​​Message​​​​payloadExpression​​​​ApplicationEvent​

从版本 4.2 开始,实现并且可以配置为不仅接受类型,而且接受任何类型来处理有效负载事件(从 Spring Framework 4.2 开始也支持)。 当接受的事件是 的实例时,它将用于要发送的消息。​​ApplicationEventListeningMessageProducer​​​​GenericApplicationListener​​​​ApplicationEvent​​​​PayloadApplicationEvent​​​​payload​

为方便起见,提供了命名空间支持来配置 with 元素,如以下示例所示:​​ApplicationEventListeningMessageProducer​​​​inbound-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"/>

在前面的示例中,与“事件类型”(可选)属性指定的类型之一匹配的所有应用程序上下文事件都将作为 Spring 集成消息传递到名为“eventChannel”的消息通道。 如果下游组件引发异常,则包含失败消息和异常的 将发送到名为“eventErrorChannel”的通道。 如果指定了 no 并且下游通道是同步的,则异常将传播到调用方。​​MessagingException​​​​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 应用程序事件

要发送 Spring ,请创建 的实例并将其注册到端点中。 该接口的实现也实现了 Spring 的接口,因此充当 Spring 集成消息和 .​​ApplicationEvents​​​​ApplicationEventPublishingMessageHandler​​​​MessageHandler​​​​ApplicationEventPublisherAware​​​​ApplicationEvents​

为方便起见,提供了命名空间支持来配置 with 元素,如以下示例所示:​​ApplicationEventPublishingMessageHandler​​​​outbound-channel-adapter​

<int:channel id="eventChannel"/>

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

如果使用 (如 ),还可以提供元素的子元素。 您还可以选择为该轮询器提供参考。 以下示例演示了两者:​​PollableChannel​​​​QueueChannel​​​​poller​​​​outbound-channel-adapter​​​​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”通道的所有消息都作为实例发布到在同一 Spring 中注册的任何相关实例。 如果消息的有效负载为 ,则按原样传递。 否则,消息本身将包装在实例中。​​ApplicationEvent​​​​ApplicationListener​​​​ApplicationContext​​​​ApplicationEvent​​​​MessagingEvent​

从版本 4.2 开始,您可以使用布尔属性配置 () 以按原样发布到应用程序上下文,而不是将其包装到实例。​​ApplicationEventPublishingMessageHandler​​​​<int-event:outbound-channel-adapter>​​​​publish-payload​​​​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);
}

标签:ApplicationEventListeningMessageProducer,producer,Spring,ApplicationEvent,Applic
From: https://blog.51cto.com/u_15326439/5920847

相关文章