如何在Spring Batch的ItemWriter提交后更新源数据的状态

huangapple go评论75阅读模式
英文:

How to update status of source data after commit on ItemWriter in Spring Batch

问题

我正在使用带分页的ItemReader从源表中获取数据。在ItemProcessor中,我将逐一使用项目的ID向服务发送rest请求,并将它们的响应传递给ItemWriter,以存储在另一个表中。

在第一次读取(第1页)时,每个块上的项目都具有UNPROCESSED状态,然后在处理并将其响应保存到另一个表后,我希望将当前块中所有项目的状态更新为PROCESSED。

在ItemReader中,我正在选择所有具有UNPROCESSED状态的项目。
select * from table where status = unprocessed

我的问题是,在保存响应后如何更新源表中已处理的每个项目的状态。因为如果我更新状态,分页将不再正确,会因为更新而导致第2页跳过一些项目。

英文:

I'm using ItemReader with paging to get data from the source table. On ItemProcessor one by one I will send a rest request to a service using the id of the item and pass their responses to the ItemWriter to store in another table.

At first read (page 1), each item on the chunk have a status of UNPROCESSED, then after processing and saving their responses to another table, I want to update the the status of all items in the current chunk to PROCESSED.

On ItemReader I'm selecting all items that have UNPROCESSED status.
select * from table where status = unprocessed

My question is how can I update the status of each item in my source table that have been processed after saving the response. Because if I update the status, the paging is not correct anymore because of the update. Page 2 will skip some items.

答案1

得分: 0

你的ItemWriter负责写入远程服务的响应,还可以写回源表。因此,item writer 应执行以下操作:

  1. 写入 Web 服务的响应。
  2. 使用状态 = 已处理更新特定的项目 ID。
英文:

Your ItemWriter which is writing the response of your remote service can additionally write back to the source table as well. Thus, the item writer should do following

  1. write response of your webservice
  2. update the particular item id with status = processed

huangapple
  • 本文由 发表于 2020年9月11日 14:14:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定