inbound-channel-adapter - 如何在失败时更新行字段?

时间:2014-05-15 10:21:43

标签: spring-integration spring-jdbc

我有一个以标准数据库查询开头的集成,它更新数据库中的状态以指示集成工作正常。有用。

但是如果无法处理数据并引发异常,则状态不会按预期更新,但我想以“KO”状态更新我的数据库行,因此同一行不会一遍又一遍地失败。

有没有办法在集成失败时提供第二个查询来执行?

在我看来,这是非常标准的做事方式,但我找不到一种简单的方法。我可以在集成的每个步骤中捕获异常并更新数据库,但它会创建耦合,因此应该有另一个解决方案。

我尝试了很多谷歌搜索,但我找不到任何东西,但我很确定答案就在那里。

以防万一,有我的xml配置来进行数据库查询(没什么特别的):

<int-jdbc:inbound-channel-adapter auto-startup="true" data-source="datasource"
            query="select * FROM MyTable  where STATE='ToProcess')"
            channel="stuffTransformerChannel"
            update="UPDATE MyTable SET STATE='OK' where id in (:id)"
            row-mapper="myRowMapper" max-rows-per-poll="1">
            <int:poller fixed-rate="1000">
                <int:transactional />
            </int:poller>
</int-jdbc:inbound-channel-adapter>

我正在使用spring-integration版本4.0.0.RELEASE

1 个答案:

答案 0 :(得分:2)

由于你在交易中,这是正常的行为,导致rallback,并且你的数据库返回到清除状态。

在这种情况下,处理应用程序目的数据是经典模式,而不是来自某些内置工具。这就是为什么我们不提供任何on-error-update的原因,因为它不能成为evrything的用例。

无论如何,只要您要更新行,您应该在onRallback事件上执行某些操作,并在新事务中执行此操作。但是它应该在同一个Thread中,以防止从第二个轮询任务中获取同一行。

为此,我们提供transaction-synchronization-factory功能:

<int-jdbc:inbound-channel-adapter max-rows-per-poll="1">
      <int:poller fixed-rate="1000" max-messages-per-poll="1">
           <int:transactional synchronization-factory="syncFactory"/>
      </int:poller>
</int-jdbc:inbound-channel-adapter>

<int:transaction-synchronization-factory id="syncFactory">
     <int:after-rollback channel="stuffErrorChannel"/>
</int:transaction-synchronization-factory>

<int-jdbc:outbound-channel-adapter 
        query="UPDATE MyTable SET STATE='KO' where id in (:payload[id])"
        channel="stuffErrorChannel">
    <int-jdbc:request-handler-advice-chain>
         <tx:advice id="requiresNewTx">
             <tx:attributes>
                 <tx:method name="handle*Message" propagation="REQUIRES_NEW"/>
             </tx:attributes>
         </tx:advice>           
    </int-jdbc:request-handler-advice-chain>
</int-jdbc:outbound-channel-adapter>

希望我很清楚