简体   繁体   English

Spring Integration DSL 添加中流事务

[英]Spring Integration DSL adding mid flow transaction

I want to make specific part of flow as transactional.我想将流程的特定部分设为事务性。 For instance, I want to make the first two transform operation in one transactional block.例如,我想在一个事务块中进行前两个转换操作。 Here is the flow code that I use:这是我使用的流程代码:

@Bean
public IntegrationFlow createNumberRange() {

    return IntegrationFlows.from("npEventPubSubChannel")

            .transform(...) 
            .transform(...)// should be transactional with above transform together
            .transform(...) // non transactional
            .handle((payload, headers) -> numbRepository.saveAll(payload))
            .get();

}

I found a workaround as adding another handle and directing flow to transactional gateway like this one:我找到了一种解决方法,即添加另一个句柄并将流定向到这样的事务网关:

.handle("transactionalBean", "transactionalMetod") //Then implemented messagingGateway which consists of transactional method.

I also found mid flow transactional support but couldn't find an example to work on.我还找到了中流事务支持,但找不到可以处理的示例。

Is there an elegant solution rather than directing to another gateway in the middle of the flow?是否有一个优雅的解决方案,而不是在流程中间指向另一个网关?

If you want to wrap two transformers into the transaction, you don't have choice unless hide that call behind transactional gateway.如果您想将两个转换器包装到事务中,除非将该调用隐藏在事务网关之后,否则您别无选择。 That is fully similar when you do raw Java:当您执行原始 Java 时,这完全相似:

@Transactional
void myTransactionalMethod() {
    transform1();
    transform2();
}

I'm sure you are agree with me that we always have to do this way to have them both in the same transaction.我相信你同意我的看法,我们总是必须这样做才能让他们在同一笔交易中。

With Spring Integration Java DSL you can do this though:使用 Spring Integration Java DSL,您可以做到这一点:

.gateway(f -> f
            .transform(...)
            .transform(...),
        e -> e.transactional())

Do you agree it is similar to what we have with the raw Java and not so bad from the elegance perspective?您是否同意它与我们在原始 Java 中所拥有的相似,并且从优雅的角度来看并没有那么糟糕?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM