Spring Integration JPA入站通道适配器

时间:2014-05-12 06:43:52

标签: java spring jpa spring-integration

我有一个spring-integration message channel,使用jpa inbound-channel-adapter从数据库中读取。

<int:channel id="logChannel">
    <int:priority-queue capacity="20" />
</int:channel>

<int-jpa:inbound-channel-adapter
    channel="logChannel" entity-class="com.objects.Transactionlog"
    entity-manager-factory="entityManagerFactory" auto-startup="true"
    jpa-query="SELECT x FROM Transactionlog AS x WHERE x.status LIKE '1'" max-results="1">
    <int:poller fixed-rate="5000">
        <int:transactional propagation="REQUIRED"
            transaction-manager="transactionManager" />
    </int:poller>
</int-jpa:inbound-channel-adapter>

这始终只读取表transactionlog的第一行。所以我想在阅读后立即更新每个数据库条目的status。任何人都知道如何做到这一点?

1 个答案:

答案 0 :(得分:4)

如果max-results="1"对您而言是合适的,并且每5秒只收到一个实体,则适合您的用例,请接受。

现在如何在下次投票时更新该实体以跳过它。

<int-jpa:inbound-channel-adapter>delete-after-poll="true"选项,允许在实体回溯后执行entityManager.remove(entity)

是的,这是从DB真正的删除。要将其转换为UPDATE,您可以使用以下标记实体:

@SQLDelete(sql = "UPDATE Transactionlog SET status = false WHERE id = ?")

或类似的东西,这对你来说是合适的。

另一个功能是Transaction Synchronization,当您使用<poller>工厂标记before-commit并在那里进行更新时。类似的东西:

<int-jpa:inbound-channel-adapter ...>
    <int:poller fixed-rate="5000">
      <int:transactional propagation="REQUIRED"
           transaction-manager="transactionManager"
           synchronization-factory="txSyncFactory" />
    </int:poller>
<int-jpa:inbound-channel-adapter>

<int:transaction-synchronization-factory id="txSyncFactory">
    <int:before-commit channel="updateEntityChannel" />
</int:transaction-synchronization-factory>

<int:chain input-channel="updateEntityChannel">
   <int:enricher>
       <int:property name="status" value="true"/>
   </int:enricher>
   <int-jpa:outbound-channel-adapter entity-manager="entityManager"/>
</int:chain/>

类似的东西。