Spring Integration JPA入站通道适配器
我有一个spring-integration
message channel
,它使用jpa inbound-channel-adapter
从数据库中读取.
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>
这总是只读取表transactionlog
的第一行.因此,我想在读取后立即更新每个数据库条目的status
.任何人都知道该怎么做吗?
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?
如果max-results="1"
对您来说还行,并且每5秒仅收到一个实体适合您的用例,就顺其自然.
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.
<int-jpa:inbound-channel-adapter>
具有delete-after-poll="true"
选项,该选项允许在检索实体后执行entityManager.remove(entity)
.
The <int-jpa:inbound-channel-adapter>
has delete-after-poll="true"
option, which allows to perform entityManager.remove(entity)
after an entity retrival.
对,这是从数据库中真正删除的内容.要将其转换为UPDATE,您可以使用以下方式标记您的实体:
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.
另一个功能是交易同步,当您用某些before-commit
工厂标记<poller>
并在那里进行UPDATE时.像这样:
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/>
类似的东西.