Spring 集成 DSL:如何重构为子流?
Spring Integration DSL: How to refactor into Subflows?
为了使我的 Spring 集成 DSL 代码更具可读性和模块化,我想将像 .scatterGather()
这样的复杂操作提取到子流中。
以此为例,代码重构前的样子:
@Bean
IntegrationFlow testFlow() {
return IntegrationFlows
.from(Http.inboundChannelAdapter("test").get())
.scatterGather(
s -> s
.applySequence(true)
.recipientFlow(getSubFlow2()),
g -> g.outputProcessor(MessageGroup::getOne))
.log(INFO, m -> m)
.routeToRecipients(route -> route
.recipientFlow(IntegrationFlowDefinition::nullChannel)
.get())
.get();
}
private IntegrationFlow getSubFlow2() {
return f -> f.handle((m, h) -> 42);
}
这是我想提取到它自己的子流程和方法中的部分:
.scatterGather(
s -> s
.applySequence(true)
.recipientFlow(getSubFlow2()),
g -> g.outputProcessor(MessageGroup::getOne))
.log(INFO, m -> m)
我想象的结果是这样的:
@Bean
IntegrationFlow testFlow() {
return IntegrationFlows
.from(Http.inboundChannelAdapter("test").get())
.someMethod(getSubFlow1())
.routeToRecipients(route -> route
.recipientFlow(IntegrationFlowDefinition::nullChannel)
.get())
.get();
}
private IntegrationFlow getSubFlow1() {
return f -> f
.scatterGather(
s -> s
.applySequence(true)
.recipientFlow(getSubFlow2()),
g -> g.outputProcessor(MessageGroup::getOne))
.logAndReply(INFO, m -> m)
}
private IntegrationFlow getSubFlow2() {
return f -> f.handle((m, h) -> 42);
}
这可以通过 Spring Integration DSL 以某种方式完成吗?怎么样?
参见gateway(IntegrationFlow)
流定义方法:
/**
* Populate the "artificial"
* {@link org.springframework.integration.gateway.GatewayMessageHandler} for the
* provided {@code subflow}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
* .gateway(f -> f.transform("From Gateway SubFlow: "::concat))
* }
* </pre>
* @param flow the {@link IntegrationFlow} to to send a request message and wait for reply.
* @return the current {@link BaseIntegrationFlowDefinition}.
*/
public B gateway(IntegrationFlow flow) {
为了使我的 Spring 集成 DSL 代码更具可读性和模块化,我想将像 .scatterGather()
这样的复杂操作提取到子流中。
以此为例,代码重构前的样子:
@Bean
IntegrationFlow testFlow() {
return IntegrationFlows
.from(Http.inboundChannelAdapter("test").get())
.scatterGather(
s -> s
.applySequence(true)
.recipientFlow(getSubFlow2()),
g -> g.outputProcessor(MessageGroup::getOne))
.log(INFO, m -> m)
.routeToRecipients(route -> route
.recipientFlow(IntegrationFlowDefinition::nullChannel)
.get())
.get();
}
private IntegrationFlow getSubFlow2() {
return f -> f.handle((m, h) -> 42);
}
这是我想提取到它自己的子流程和方法中的部分:
.scatterGather(
s -> s
.applySequence(true)
.recipientFlow(getSubFlow2()),
g -> g.outputProcessor(MessageGroup::getOne))
.log(INFO, m -> m)
我想象的结果是这样的:
@Bean
IntegrationFlow testFlow() {
return IntegrationFlows
.from(Http.inboundChannelAdapter("test").get())
.someMethod(getSubFlow1())
.routeToRecipients(route -> route
.recipientFlow(IntegrationFlowDefinition::nullChannel)
.get())
.get();
}
private IntegrationFlow getSubFlow1() {
return f -> f
.scatterGather(
s -> s
.applySequence(true)
.recipientFlow(getSubFlow2()),
g -> g.outputProcessor(MessageGroup::getOne))
.logAndReply(INFO, m -> m)
}
private IntegrationFlow getSubFlow2() {
return f -> f.handle((m, h) -> 42);
}
这可以通过 Spring Integration DSL 以某种方式完成吗?怎么样?
参见gateway(IntegrationFlow)
流定义方法:
/**
* Populate the "artificial"
* {@link org.springframework.integration.gateway.GatewayMessageHandler} for the
* provided {@code subflow}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
* .gateway(f -> f.transform("From Gateway SubFlow: "::concat))
* }
* </pre>
* @param flow the {@link IntegrationFlow} to to send a request message and wait for reply.
* @return the current {@link BaseIntegrationFlowDefinition}.
*/
public B gateway(IntegrationFlow flow) {