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

ABX:

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

Artem Bilan:

您需要遵循以下文档,因此将其添加到您的流程中:

.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.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章