如何在spring集成中动态调用.handle()中的两个参数重载方法?



我想让我的spring集成流通用的不同类型的请求,通过网关,我想有重载的方法,并希望调用特定的重载方法,通过网关的特定消息。

flow.handle(validatorService, "validateRequest")

类似于上面的代码,在validatorService中,我有几个重载的方法来满足进入该流的不同请求。但是如果我想要发送另一个参数给一个方法。那我该怎么做呢?

@Configuration
public class IntegrationConfiguration {
@Autowired LionsServiceImpl lionsService;

long dbId = new SequenceGenerator().nextId();
//   Main flow
@Bean
public IntegrationFlow flow() {
return flow ->
flow.handle(validatorService, "validateRequest")
.split()
.channel(c -> c.executor(Executors.newCachedThreadPool()))
.scatterGather(
scatterer ->
scatterer
.applySequence(true)
.recipientFlow(flow1())
.recipientFlow(flow2())
.recipientFlow(flow3()),
gatherer ->
gatherer
.releaseLockBeforeSend(true)
.releaseStrategy(group -> group.size() == 2))
.aggregate(prepareSomeRequest())
.to(getDec());
}

//网关

@Gateway(requestChannel = "flow.input")
void processLionRequest(
@Payload Message lionRequest, @Header("sourceSystem") SourceSystem sourceSystem);

所以如果你看到在flow.handle(validatorService, "validateRequest")我们有一个validatorService类接受LionRequest, CatRequest和动态调用重载的方法。同样的,我有一个重载方法,它接受2个参数,一个是LionRequest或CatRequest,另一个值来自header或其他变量。

我的重载方法如下-

public LionRequest save(LionRequest lionreq, String dbID){}
public CatRequest save(CatRequest catReq, String dbID){}..

所以网关我可以传递LionRequest或CatRequest,我只需要从类中调用save方法,动态地调用LionRequest/CatRequest重载方法。

请建议。一个示例代码可以帮助

我们需要理解"另一个参数"是什么,但如果你只谈论Message上下文,那么它可能像这样:

Foo validateRequest(Foo payload, @Header("fooHeader") String fooHeader)
Bar validateRequest(Bar payload, @Header("barHeader") String barHeader)

更多信息见文档:https://docs.spring.io/spring-integration/docs/current/reference/html/configuration.html#annotations

流定义中handle()的引用与您现在在问题中所拥有的完全相同。

最新更新