简体   繁体   English

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

[英]How to handle transactions for Spring integration flows (Java DSL)

How is it possible to define a transaction for a complete flow in spring integration (Java DSL)? 如何在Spring集成(Java DSL)中为完整流定义事务?

With Spring integration we can define an example flow with: 通过Spring集成,我们可以定义一个示例流程:

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

I need a transaction which overspans the complete flow. 我需要一个超过完整流程的交易。 Currently, when I access a database with 'aMessageTransformer', the transaction will be closed after this message transformer has been processed. 目前,当我使用'aMessageTransformer'访问数据库时,在处理完此消息转换后,事务将被关闭。 But I need a transaction which is still uncommitted when processing 'anotherMessageTransformer'? 但是我需要一个在处理'anotherMessageTransformer'时仍未提交的事务?

I expected that I just have to add a '@Transactional' (or @Transactional(propagation = Propagation.REQUIRED, readOnly = true)) 我希望我只需添加'@Transactional'(或@Transactional(propagation = Propagation.REQUIRED,readOnly = true))

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

but this leads to a 'no session exception' in 'anotherMessageTransformer' 但这导致'anotherMessageTransformer'中出现'无会话异常'

You need to follow this documentation and therefore add this to your flow: 您需要遵循此文档 ,因此将其添加到您的流程中:

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

where that .transactional() is about: 那里.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) {

The TransactionHandleMessageAdvice means: 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.

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

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