Spring Batch : 在不同steps间传递数据
参考文档:
How can we share data between the different steps of a Job in Spring Batch?
有三种方式:
- Use
StepContext
and promote it toJobContext
and you have access to it from each step, you must as noted obey limit in size - Create
@JobScope
bean and add data to that bean,@Autowire
it where needed and use it (drawback is that it is in-memory structure and if job fails data is lost, migh cause problems with restartability) - We had larger datasets needed to be processed across steps (read each line in csv and write to DB, read from DB, aggregate and send to API) so we decided to model data in new table in same DB as spring batch meta tables, keep
ids
inJobContext
and access when needed and delete that temporary table when job finishes successfully.
1|0使用 JobContext
通过step_execution 或者 job_execution来在不同step中传递数据。
但是如果数据量大的话,这将不是一种好的方式.因为spring batch默认会通过job repository将 step_execution
和job_execution
进行持久化.
Use ExecutionContextPromotionListener
:
首先在第一个Step中放到StepExecution
中
然后使用promotionListener
在监听器中将其升级到JobExecution
中:
在第二个Step中使用ExecutionContext
获取:
如果你使用的是tasklets的话,可以这样获取:
2|0用自己定义的bean传递数据
创建一个data holder
在step 1中设置数据:
在step2中取数据:
job配置
- define.xml
- Job Config
__EOF__

本文作者:咫尺是梦
本文链接:https://www.cnblogs.com/satire/p/15471611.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/satire/p/15471611.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!