我有一个 spring-integration
消息 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
。任何人都知道如何做到这一点?
最佳答案
如果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/>
类似的东西。
关于java - Spring Integration JPA 入站 channel 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23602591/