Spring Integration JPA inbound channel adapter

旧时模样 提交于 2019-12-24 12:34:25

问题


I have a spring-integration message channel which read from a database using a 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>

This always reads only the first row of the table transactionlog. So I want to update the status of each database entry just after read. Any body know how to do that?


回答1:


If max-results="1" is OK for you and receive only one entity per 5 second is appropiate for your use-case, let it be.

Now how to update that entity to skip it on the next poll.

The <int-jpa:inbound-channel-adapter> has delete-after-poll="true" option, which allows to perform entityManager.remove(entity) after an entity retrival.

Right, it is the real removal from DB. To convert it to the UPDATE, you can mark your entity with:

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

Or something similar, that is appropiate for you.

Another feature is Transaction Synchronization, when you mark your <poller> with some before-commit factory and do UPDATE there. Something like:

<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/>

Something like that.



来源:https://stackoverflow.com/questions/23602591/spring-integration-jpa-inbound-channel-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!