在Spring Integration流程中保持事务

Ant*_*edo 5 transactions spring-integration

Inboud网关:

<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>
Run Code Online (Sandbox Code Playgroud)

建议定义:

<tx:advice id="advice">
    <tx:attributes>
        <tx:method name="send" propagation="REQUIRES_NEW" rollback-for="MyClassException"/>
    </tx:attributes>
</tx:advice>
Run Code Online (Sandbox Code Playgroud)

建议配置:

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

需要交易的链:

<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>
Run Code Online (Sandbox Code Playgroud)

需要事先执行的链以获取在最后一个转换链步骤中生成的对象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>
Run Code Online (Sandbox Code Playgroud)

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

因此,事务似乎在入站网关输出级别提交.

Art*_*lan 9

不编写任何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>
Run Code Online (Sandbox Code Playgroud)

有了这个,所有直接的单线程消息流将被包装到发送到通道输入的消息上的TX