メッセージルーター
Spring Integration は、次のような特殊なルーター型をネイティブで提供します。
HeaderValueRouterPayloadTypeRouterExceptionTypeRouterRecipientListRouterXPathRouter
他の多くの DSL IntegrationFlowBuilder EIP メソッドと同様に、route() メソッドは、任意の AbstractMessageRouter 実装、または便宜上、SpEL 式としての String、ref-method ペアを適用できます。さらに、ラムダを使用して route() を構成し、Consumer<RouterSpec<MethodInvokingRouter>> にラムダを使用することができます。次の例に示すように、Fluent API は、channelMapping(String key, String channelName) ペアなどの AbstractMappingMessageRouter オプションも提供します。
@Bean
public IntegrationFlow routeFlowByLambda() {
return IntegrationFlow.from("routerInput")
.<Integer, Boolean>route(p -> p % 2 == 0,
m -> m.suffix("Channel")
.channelMapping(true, "even")
.channelMapping(false, "odd")
)
.get();
}次の例は、単純な式ベースのルーターを示しています。
@Bean
public IntegrationFlow routeFlowByExpression() {
return IntegrationFlow.from("routerInput")
.route("headers['destChannel']")
.get();
} 次の例に示すように、routeToRecipients() メソッドは Consumer<RecipientListRouterSpec> を取ります。
@Bean
public IntegrationFlow recipientListFlow() {
return IntegrationFlow.from("recipientListInput")
.<String, String>transform(p -> p.replaceFirst("Payload", ""))
.routeToRecipients(r -> r
.recipient("thing1-channel", "'thing1' == payload")
.recipientMessageSelector("thing2-channel", m ->
m.getHeaders().containsKey("recipient")
&& (boolean) m.getHeaders().get("recipient"))
.recipientFlow("'thing1' == payload or 'thing2' == payload or 'thing3' == payload",
f -> f.<String, String>transform(String::toUpperCase)
.channel(c -> c.queue("recipientListSubFlow1Result")))
.recipientFlow((String p) -> p.startsWith("thing3"),
f -> f.transform("Hello "::concat)
.channel(c -> c.queue("recipientListSubFlow2Result")))
.recipientFlow(new FunctionExpression<Message<?>>(m ->
"thing3".equals(m.getPayload())),
f -> f.channel(c -> c.queue("recipientListSubFlow3Result")))
.defaultOutputToParentFlow())
.get();
}.routeToRecipients() 定義の .defaultOutputToParentFlow() を使用すると、ルーターの defaultOutput をゲートウェイとして設定して、メインフロー内の一致しないメッセージのプロセスを続行できます。
ラムダと Message<?> 引数も参照してください。