gpt4 book ai didi

java - 如何处理 Spring 集成流的事务(Java DSL)

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:08:41 26 4
gpt4 key购买 nike

在 Spring Integration (Java DSL) 中如何定义一个完整流程的事务?

通过 Spring 集成,我们可以定义一个示例流程:

@Bean
public IntegrationFlow myMessageFromMessageAmqpInboundFlow() {
return IntegrationFlows.from(myInboundChannel)
.transform(aMessageTransformer)
.transform(anotherMessageTransformer)
.channel(anOutputChannel)
.get();
}

我需要一个跨越整个流程的事务。目前,当我使用“aMessageTransformer”访问数据库时,事务将在处理完此消息转换器后关闭。但是我需要一个在处理“anotherMessageTransformer”时仍未提交的事务?

我预计我只需要添加一个“@Transactional”(或@Transactional(propagation = Propagation.REQUIRED,readOnly = true))

@Bean
@Transactional
public IntegrationFlow myMessageFromMessageAmqpInboundFlow() {
return IntegrationFlows.from(myInboundChannel)
.transform(aMessageTransformer)
.transform(anotherMessageTransformer)
.channel(anOutputChannel)
.get();
}

但这会导致“anotherMessageTransformer”中出现“无 session 异常”

最佳答案

你需要按照这个documentation因此将其添加到您的流程中:

.transform(aMessageTransformer, e -> e.transactional(true))

.transactional() 是关于:

/**
* Specify a {@link TransactionInterceptor} {@link Advice} with default
* {@code PlatformTransactionManager} and {@link DefaultTransactionAttribute} for the
* {@link MessageHandler}.
* @param handleMessageAdvice the flag to indicate the target {@link Advice} type:
* {@code false} - regular {@link TransactionInterceptor}; {@code true} -
* {@link org.springframework.integration.transaction.TransactionHandleMessageAdvice}
* extension.
* @return the spec.
*/
public S transactional(boolean handleMessageAdvice) {

TransactionHandleMessageAdvice 表示:

* When this {@link Advice} is used from the {@code request-handler-advice-chain}, it is applied
* to the {@link MessageHandler#handleMessage}
* (not to the
* {@link org.springframework.integration.handler.AbstractReplyProducingMessageHandler.RequestHandler#handleRequestMessage}),
* therefore the entire downstream process is wrapped to the transaction.

关于java - 如何处理 Spring 集成流的事务(Java DSL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49893900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com