Skip to main content
 首页 » 编程设计

transactions之将事务保留在 Spring Integration 流程中

2024年06月20日22linjiqin

入站网关:

<int-http:inbound-gateway   id="inbound.gateway" 
                            request-channel="transactional.channel.input" 
                            reply-channel="channel.output" 
                            error-channel="channel.error" 
                            request-payload-type="java.lang.String" 
</int-http:inbound-gateway> 

建议定义:

<tx:advice id="advice"> 
    <tx:attributes> 
        <tx:method name="send" propagation="REQUIRES_NEW" rollback-for="MyClassException"/> 
    </tx:attributes> 
</tx:advice> 

建议配置:

<aop:config> 
    <aop:advisor advice-ref="advice" pointcut="bean(transactional.channel.input)"/> 
</aop:config> 

需要交易的链:

<int:chain input-channel="transactional.channel.input" output-channel="non.transactional.channel.input> 
    <int:service-activator ref="v1.registerUser.service" method="registerUser"/> 
    <int:service-activator ref="v1.saveObject.service" method="saveObject"/> 
</int:chain> 

需要先执行交易才能获取最后交易链步骤中生成的对象ID的链:

<int:chain input-channel="non.transactional.channel.input" output-channel="channel.output"> 
    <int:service-activator  ref="v1.getObjectId.service" method="getObjectId"/> 
    <int:object-to-json-transformer/> 
</int:chain> 

有了这个简化的上下文,当我访问 getObjectId 服务中的 id 时,事务尚未执行。

因此事务似乎是在入站网关输出级别提交的。

请您参考如下方法:

无需编写任何 Java 代码,还有一个令人惊奇的技巧:

<channel id="input"/> 
 
<aop:config> 
    <aop:advisor advice-ref="txAdvice" pointcut="bean(input)"/> 
</aop:config> 
 
<tx:advice id="txAdvice"> 
    <tx:attributes> 
     <tx:method name="send"/> 
    </tx:attributes> 
</tx:advice> 

这样,所有直接单线程消息流都将被包装到发送到 channel 输入的消息的 TX