对于最新的稳定版本,请使用 Spring Integration 6.4.0! |
路由器实现
由于基于内容的路由通常需要一些特定于域的逻辑,因此大多数用例都需要 Spring 集成的选项,通过使用 XML 名称空间支持或注释来委托给 POJO。 这两者都将在后面讨论。 但是,我们首先介绍几个满足常见要求的实现。
PayloadTypeRouter
一个PayloadTypeRouter
将消息发送到由 payload-type 映射定义的通道,如下例所示:
<bean id="payloadTypeRouter"
class="org.springframework.integration.router.PayloadTypeRouter">
<property name="channelMapping">
<map>
<entry key="java.lang.String" value-ref="stringChannel"/>
<entry key="java.lang.Integer" value-ref="integerChannel"/>
</map>
</property>
</bean>
的配置PayloadTypeRouter
也受 Spring 集成提供的名称空间的支持(参见Namespace Support
),它通过结合<router/>
configuration 及其相应的实现(通过使用<bean/>
元素)转换为单个更简洁的配置元素。
以下示例显示了PayloadTypeRouter
等同于上述配置,但使用命名空间支持:
<int:payload-type-router input-channel="routingChannel">
<int:mapping type="java.lang.String" channel="stringChannel" />
<int:mapping type="java.lang.Integer" channel="integerChannel" />
</int:payload-type-router>
以下示例显示了在 Java 中配置的等效路由器:
@ServiceActivator(inputChannel = "routingChannel")
@Bean
public PayloadTypeRouter router() {
PayloadTypeRouter router = new PayloadTypeRouter();
router.setChannelMapping(String.class.getName(), "stringChannel");
router.setChannelMapping(Integer.class.getName(), "integerChannel");
return router;
}
使用 Java DSL 时,有两个选项。
首先,您可以定义 router 对象,如前面的示例所示:
@Bean
public IntegrationFlow routerFlow1() {
return IntegrationFlow.from("routingChannel")
.route(router())
.get();
}
public PayloadTypeRouter router() {
PayloadTypeRouter router = new PayloadTypeRouter();
router.setChannelMapping(String.class.getName(), "stringChannel");
router.setChannelMapping(Integer.class.getName(), "integerChannel");
return router;
}
请注意,路由器可以是(但并非必须)是@Bean
.
如果它不是@Bean
.
其次,您可以在 DSL 流本身中定义路由函数,如下例所示:
@Bean
public IntegrationFlow routerFlow2() {
return IntegrationFlow.from("routingChannel")
.<Object, Class<?>>route(Object::getClass, m -> m
.channelMapping(String.class, "stringChannel")
.channelMapping(Integer.class, "integerChannel"))
.get();
}
HeaderValueRouter
一个HeaderValueRouter
根据各个标头值映射将 Messages 发送到通道。
当HeaderValueRouter
创建,则使用要评估的标头的名称对其进行初始化。
标头的值可以是以下两项之一:
-
任意值
-
频道名称
如果它是任意值,则需要将这些 Headers 值附加到通道名称的其他映射。 否则,无需其他配置。
Spring 集成提供了一个简单的基于名称空间的 XML 配置,用于配置一个HeaderValueRouter
.
以下示例演示了HeaderValueRouter
当需要将 Headers 值映射到 channels 时:
<int:header-value-router input-channel="routingChannel" header-name="testHeader">
<int:mapping value="someHeaderValue" channel="channelA" />
<int:mapping value="someOtherHeaderValue" channel="channelB" />
</int:header-value-router>
在解析过程中,上述示例中定义的路由器可能会遇到通道解析失败,从而导致异常。
如果要禁止此类异常并将未解析的消息发送到默认输出通道(以default-output-channel
属性)设置resolution-required
自false
.
通常,标头值未显式映射到通道的消息将发送到default-output-channel
.
但是,当 header 值映射到通道名称但无法解析通道时,将resolution-required
属性设置为false
导致将此类消息路由到default-output-channel
.
以下示例显示了在 Java 中配置的等效路由器:
@ServiceActivator(inputChannel = "routingChannel")
@Bean
public HeaderValueRouter router() {
HeaderValueRouter router = new HeaderValueRouter("testHeader");
router.setChannelMapping("someHeaderValue", "channelA");
router.setChannelMapping("someOtherHeaderValue", "channelB");
return router;
}
使用 Java DSL 时,有两个选项。 首先,您可以定义 router 对象,如前面的示例所示:
@Bean
public IntegrationFlow routerFlow1() {
return IntegrationFlow.from("routingChannel")
.route(router())
.get();
}
public HeaderValueRouter router() {
HeaderValueRouter router = new HeaderValueRouter("testHeader");
router.setChannelMapping("someHeaderValue", "channelA");
router.setChannelMapping("someOtherHeaderValue", "channelB");
return router;
}
请注意,路由器可以是(但并非必须)是@Bean
.
如果它不是@Bean
.
其次,您可以在 DSL 流本身中定义路由函数,如下例所示:
@Bean
public IntegrationFlow routerFlow2() {
return IntegrationFlow.from("routingChannel")
.route(Message.class, m -> m.getHeaders().get("testHeader", String.class),
m -> m
.channelMapping("someHeaderValue", "channelA")
.channelMapping("someOtherHeaderValue", "channelB"),
e -> e.id("headerValueRouter"))
.get();
}
不需要将 Headers 值映射到通道名称的配置,因为 Headers 值本身表示通道名称。 以下示例显示了一个不需要将 Headers 值映射到通道名称的路由器:
<int:header-value-router input-channel="routingChannel" header-name="testHeader"/>
从 Spring Integration 2.1 开始,解析通道的行为更加明确。
例如,如果省略 基本上,默认情况下,路由器必须能够成功地将消息路由到至少一个通道。
如果你真的想丢弃消息,你还必须有 |
RecipientListRouter
一个RecipientListRouter
将收到的每条消息发送到静态定义的消息通道列表。
以下示例创建一个RecipientListRouter
:
<bean id="recipientListRouter"
class="org.springframework.integration.router.RecipientListRouter">
<property name="channels">
<list>
<ref bean="channel1"/>
<ref bean="channel2"/>
<ref bean="channel3"/>
</list>
</property>
</bean>
Spring 集成还为RecipientListRouter
配置(请参阅命名空间支持),如下例所示:
<int:recipient-list-router id="customRouter" input-channel="routingChannel"
timeout="1234"
ignore-send-failures="true"
apply-sequence="true">
<int:recipient channel="channel1"/>
<int:recipient channel="channel2"/>
</int:recipient-list-router>
以下示例显示了在 Java 中配置的等效路由器:
@ServiceActivator(inputChannel = "routingChannel")
@Bean
public RecipientListRouter router() {
RecipientListRouter router = new RecipientListRouter();
router.setSendTimeout(1_234L);
router.setIgnoreSendFailures(true);
router.setApplySequence(true);
router.addRecipient("channel1");
router.addRecipient("channel2");
router.addRecipient("channel3");
return router;
}
以下示例显示了使用 Java DSL 配置的等效路由器:
@Bean
public IntegrationFlow routerFlow() {
return IntegrationFlow.from("routingChannel")
.routeToRecipients(r -> r
.applySequence(true)
.ignoreSendFailures(true)
.recipient("channel1")
.recipient("channel2")
.recipient("channel3")
.sendTimeout(1_234L))
.get();
}
这里的 'apply-sequence' 标志与它对 publish-subscribe-channel 的作用相同,并且与 publish-subscribe-channel 一样,它在recipient-list-router .
看PublishSubscribeChannel 配置了解更多信息。 |
配置RecipientListRouter
是使用 Spring 表达式语言 (SpEL) 支持作为单个收件人通道的选择器。
这样做类似于在 'chain' 的开头使用 filter 来充当 “selective consumer”。
但是,在这种情况下,它全部相当简洁地组合到路由器的配置中,如下例所示:
<int:recipient-list-router id="customRouter" input-channel="routingChannel">
<int:recipient channel="channel1" selector-expression="payload.equals('foo')"/>
<int:recipient channel="channel2" selector-expression="headers.containsKey('bar')"/>
</int:recipient-list-router>
在前面的配置中,由selector-expression
属性以确定此收件人是否应包含在给定输入消息的收件人列表中。
表达式的求值结果必须为boolean
.
如果未定义此属性,则渠道始终位于收件人列表中。
RecipientListRouterManagement
从版本 4.1 开始,RecipientListRouter
提供了多个作,用于在运行时动态作收件人。
这些管理作由RecipientListRouterManagement
通过@ManagedResource
注解。
它们可以通过使用 Control Bus 和 JMX 来使用,如下例所示:
<control-bus input-channel="controlBus"/>
<recipient-list-router id="simpleRouter" input-channel="routingChannelA">
<recipient channel="channel1"/>
</recipient-list-router>
<channel id="channel2"/>
messagingTemplate.convertAndSend(controlBus, "@'simpleRouter.handler'.addRecipient('channel2')");
从应用程序启动simpleRouter
,只有一个channel1
收件人。
但是在addRecipient
命令channel2
收件人。
这是一个 “注册对消息中的一部分的兴趣” 用例,当我们可能在某个时间段内对来自路由器的消息感兴趣时,因此我们订阅了recipient-list-router
并在某个时候决定取消订阅。
由于<recipient-list-router>
,它可以在没有任何<recipient>
从一开始。
在这种情况下,RecipientListRouter
当邮件没有一个匹配的收件人时,该规则相同。
如果defaultOutputChannel
配置后,消息将发送到该处。
否则,MessageDeliveryException
被抛出。
XPath 路由器
XPath Router 是 XML Module 的一部分。 请参见使用 XPath 路由 XML 消息。
路由和错误处理
Spring 集成还提供了一个特殊的基于类型的路由器,称为ErrorMessageExceptionTypeRouter
用于路由错误消息(定义为payload
是一个Throwable
实例)。ErrorMessageExceptionTypeRouter
类似于PayloadTypeRouter
.
事实上,它们几乎相同。
唯一的区别是,虽然PayloadTypeRouter
导航有效负载实例的实例层次结构(例如payload.getClass().getSuperclass()
) 查找最具体的类型和通道映射,则ErrorMessageExceptionTypeRouter
导航“异常原因”的层次结构(例如,payload.getCause()
) 查找最具体的Throwable
类型或通道映射和用途mappingClass.isInstance(cause)
以匹配cause
添加到类或任何超类。
在这种情况下,通道映射顺序很重要。
因此,如果需要获取IllegalArgumentException ,但不是RuntimeException ,则必须先在 router 上配置最后一个选项。 |
从 4.3 版本开始,ErrorMessageExceptionTypeRouter 在初始化阶段加载所有映射类,以快速失败ClassNotFoundException . |
以下示例显示了ErrorMessageExceptionTypeRouter
:
-
Java DSL
-
Kotlin DSL
-
Groovy DSL
-
XML DSL
@Bean
public IntegrationFlow someFlow() {
return f -> f
.routeByException(r -> r
.channelMapping(IllegalArgumentException.class, "illegalChannel")
.channelMapping(NullPointerException.class, "npeChannel")
.defaultOutputChannel("defaultChannel"));
}
@Bean
fun someFlow() =
integrationFlow {
routeByException {
channelMapping(IllegalArgumentException::class.java, "illegalChannel")
channelMapping(NullPointerException::class.java, "npeChannel")
defaultOutputChannel("defaultChannel")
}
}
@Bean
someFlow() {
integrationFlow {
routeByException {
channelMapping IllegalArgumentException, 'illegalChannel'
channelMapping NullPointerException, 'npeChannel'
defaultOutputChannel 'defaultChannel'
}
}
}
<int:exception-type-router input-channel="inputChannel"
default-output-channel="defaultChannel">
<int:mapping exception-type="java.lang.IllegalArgumentException"
channel="illegalChannel"/>
<int:mapping exception-type="java.lang.NullPointerException"
channel="npeChannel"/>
</int:exception-type-router>
<int:channel id="illegalChannel" />
<int:channel id="npeChannel" />