【问题标题】:How to handle transactions for Spring integration flows (Java DSL)如何处理 Spring 集成流的事务(Java DSL)
【发布时间】:2018-09-28 08:28:18
【问题描述】:

如何在 Spring 集成 (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”中出现“无会话异常”

【问题讨论】:

  • 首先,你的消息流是否发生在同一个线程中?

标签: java spring spring-integration spring-integration-dsl


【解决方案1】:

您需要关注此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.

【讨论】:

    猜你喜欢
    • 2018-07-01
    • 2021-01-14
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    相关资源
    最近更新 更多