问题
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