spring batch處理數據模板(Reader-Processor-Writer模式)

步驟監聽器

@Component
public class StepListener implements StepExecutionListener {private StepExecution stepExecution;public StepExecution getStepExecution() {return this.stepExecution;}@Overridepublic void beforeStep(StepExecution stepExecution) {this.stepExecution = stepExecution;}@Overridepublic ExitStatus afterStep(StepExecution stepExecution) {return null;}
}

@Component
@StepScope
public class ProductItemReader implements ItemReader<Product> {private Iterator<Product> iterator;private final StepListener stepListener;private final XxxRepository xxxRepository;public ProductItemReader(StepListener stepListener,XxxRepository xxxRepository) {this.stepListener = stepListener;this.xxxRepository = xxxRepository;}@Overridepublic Product read() {// 利用repository讀取數據this.iterator = xxxRepository.xxx();// StepExecution 在 Step 執行時才可用,必須懶初始化StepExecution stepExecution = stepListener.getStepExecution();ExecutionContext executionContext = stepExecution.getExecutionContext();// 可設置STEP內上下文return iterator.hasNext() ? iterator.next() : null;}
}

處理

@Slf4j
@StepScope
@Component("productProcessor")
public class ProductProcessor implements ItemProcessor<Product, Product> {private final StepListener stepListener;public ProductProcessor(@Qualifier("stepListener") StepListener stepListener) {this.stepListener = stepListener;}@Overridepublic Product process(Product product) {// 從步驟域內讀取數據或設置數據StepExecution stepExecution = productStepListener.getStepExecution();ExecutionContext executionContext = stepExecution.getExecutionContext();return product;}}

@Slf4j
@Component("productWriter")
public class ProductWriter implements ItemWriter<Product> {private final XxxRepository repository;private final StepListener stepListener;public ProductWriter(@Qualifier(value = "productRepository") XxxRepository xxxRepository,@Qualifier(value = "stepListener") StepListener stepListener) {this.repository = xxxRepository;this.stepListener = stepListener;}@Overridepublic void write(List<? extends Product> items){StepExecution stepExecution = this.stepListener.getStepExecution();ExecutionContext executionContext = stepExecution.getExecutionContext();items.forEach( product ->  {...});}
}

定義步驟

@Bean
public Step printStep(){TaskletStep taskletStep = stepBuilderFactory.get("testStep").listener(stepListener).<Product, Product>chunk(10).reader(productItemReader).processor(productProcessor).writer(productWriter).build();return taskletStep;
}

使用步驟

@Bean
public Job testJob(){return jobBuilderFactory.get("productJob").start(printStep())....build();
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/94189.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/94189.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/94189.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【華為OD機試】從小桶里取球

題目描述 某部門開展Family Day開放日活動,其中有個從桶里取球的游戲,游戲規則如下: 有N個容量一樣的小桶等距排開,且每個小桶都默認裝了數不等的小球, 每個小桶裝的小球數量記錄在數組bucketBallNums中, 游戲開始時,要求所有桶的小球總數不能超過SUM, 如果小球總…

std::unordered_map 和 std::map的區別【C++】

std::unordered_map 和 std::map 是 C 標準庫中兩種不同的關聯容器&#xff0c;它們都用于存儲鍵值對&#xff0c;但在實現方式、性能特點和使用場景上存在顯著區別。以下是它們的主要區別&#xff1a; 1. 數據結構 std::map&#xff1a; 基于 紅黑樹&#xff08;一種自平衡二叉…

云原生環境里的顯示變革:Docker虛擬瀏覽器與cpolar穿透技術實戰

文章目錄前言【視頻教程】1. 關于neko2. 本地部署neko3. neko簡單使用4. 安裝內網穿透5. 配置neko公網地址6. 配置固定公網地址前言 現代遠程協作本該是無縫銜接的過程&#xff0c;卻被這些障礙不斷打斷&#xff1a;多設備屏幕同步存在延遲、跨平臺訪問需要復雜配置、公網IP申…

LVGL + ESP-Brookesia 在Windows下的編譯和運行

LVGL ESP-Brookesia 在Windows下的編譯和運行 1. 項目介紹 本項目是基于 LVGL&#xff08;輕量級多功能圖形庫&#xff09;和 ESP-Brookesia 的嵌入式模擬桌面應用開發框架&#xff0c;專為嵌入式設備構建豐富的圖形界面而設計。通過在Windows環境下模擬嵌入式設備的圖形界面…

【ip】IP地址能否直接填寫255?

IP地址數值限制? 最近有朋友后臺問我&#xff0c;IP地址里填255行不行&#xff1f;思索著有一陣子沒有分享基礎的知識&#xff0c;就在今天大致說一下&#xff0c;關于IP地址里填255行不行&#xff1f;答案當然是否定的。 IP地址由4個段組成&#xff0c;每個段的數值范圍其實限…

力扣熱題100----------141.環形鏈表

給你一個鏈表的頭節點 head &#xff0c;判斷鏈表中是否有環。 如果鏈表中有某個節點&#xff0c;可以通過連續跟蹤 next 指針再次到達&#xff0c;則鏈表中存在環。 為了表示給定鏈表中的環&#xff0c;評測系統內部使用整數 pos 來表示鏈表尾連接到鏈表中的位置&#xff08;索…

【Java開發日記】我們來說說 LockSupport 的 park 和 unpark

目錄 一、LockSupport 1.1、LockSupport函數列表 1.2、基本使用 先 park 再 unpark 先 unpark 再 park 1.3、特點 與 Object 的 wait & notify 相比 二、LockSupport park & unpark原理 2.1、情況一&#xff0c;先調用park&#xff0c;再調用unpark park 操作…

AGI|從“實驗室”到“生產線”:企業級AI Agent 如何突圍

在數字化轉型的深水區&#xff0c;企業級 AI Agent 正從技術概念走向產業實踐&#xff0c;成為驅動生產力變革的核心引擎。目錄 一、風口已至&#xff1a;AI Agent 的崛起邏輯與市場剛需 二、企業級AI Agent&#xff1a;核心能力與獨特價值定位 三、AI Agent 的未來目標 一、…

AtCoder Beginner Contest 417

文章目錄A A SubstringB Search and DeleteC Distance IndicatorsD Takahashis ExpectationE A Path in A DictionaryF Random GatheringG Binary CatAtCoder Beginner Contest 417A A Substring You are given an N-character string S consisting of lowercase English lett…

C++23 Concepts:用類型約束重構泛型編程的終極方案

一、開篇:模板元編程的"類型檢查困局" 某金融量化團隊曾遇到詭異bug: template<typename T> void process(T data) {static_assert(std::is_arithmetic<T>::value, "需要數值類型");// 業務邏輯... } 當調用process("hello")時…

【RK3568 看門狗驅動開發詳解】

RK3568 看門狗驅動開發詳解一、Linux 看門狗子系統架構?二、設備樹配置?三、 看門狗驅動實現四、驗證看門狗定時器&#xff08;Watchdog Timer&#xff09;是保障嵌入式系統可靠性的關鍵硬件&#xff0c;它通過定期接收 “喂狗” 信號監控系統運行狀態&#xff0c;當系統故障…

探索 Vue 3.6 新特性:Vapor Mode 與高性能 Web 應用開發

Vue 3.6 簡介 Vue.js 是一個廣受歡迎的漸進式 JavaScript 框架&#xff0c;以其簡潔的 API、靈活的組件系統和高性能著稱。Vue 3.6 是 Vue 3 系列的一個重要版本&#xff0c;引入了多項性能優化和新特性&#xff0c;尤其是備受關注的 Vapor Mode&#xff0c;這是一個無需虛擬 D…

初識prometheus

Prometheus&#xff1a;云原生時代的監控利器 在當今快速發展的云原生和微服務架構時代&#xff0c;傳統的監控系統面臨著巨大的挑戰&#xff1a;如何高效地收集海量、動態變化的指標&#xff1f;如何實時告警并快速定位問題&#xff1f;如何實現靈活的可視化和強大的數據查詢…

從源碼角度分析導致 JVM 內存泄露的 ThreadLocal

文章目錄1. 為什么需要ThreadLocal2. ThreadLocal的實現解析1.1 實現分析1.2 具體實現1.3 ThreadLocalMap中Hash沖突的解決1.3.1 Hash沖突解決的幾種方法1.3.1.1 開放定值法1.3.1.2 鏈地址法1.3.1.3再哈希法&#xff1a;1.3.1.4 建立公共溢出區1.3.2 ThreadLocal解決Hash沖突的…

React組件化的封裝

1. 組件化封裝的結構 1.1. 定義一個類(組件名必須是大寫&#xff0c;小寫會被認為是html元素), 繼續自React.Component1.2. 實現當前組件的render函數 render當中返回的jsx內容&#xff0c;就是之后React會幫助我們渲染的內容 1.3. 結構圖如下&#xff1a; data 方法render()…

嵌入式仿真教學的革新力量:深圳航天科技創新研究院引領高效學習新時代

嵌入式系統作為現代信息技術的核心基石&#xff0c;已深度融入工業控制、物聯網、智能終端等關鍵領域。高校肩負著培養嵌入式技術人才的重任&#xff0c;但傳統教學方式正面臨嚴峻挑戰&#xff1a;硬件實驗設備投入巨大、更新滯后、維護繁瑣、時空限制嚴格&#xff0c;難以滿足…

六、Linux核心服務與包管理

作者&#xff1a;IvanCodes 日期&#xff1a;2025年8月3日 專欄&#xff1a;Linux教程 要保證一個Linux系統穩定、安全、功能完備&#xff0c;有效管理其后臺服務和軟件包是至關重要的。本文將深入介紹現代Linux系統中四個核心的管理工具&#xff1a;systemctl (服務管理)&…

【數據結構】哈希表實現

目錄 1. 哈希概念 2 哈希沖突和哈希函數 3. 負載因子 4. 將關鍵字轉為整數 5. 哈希函數 5.1直接定址法 5.2 除法散列法/除留余數法 5.3 乘法散列法&#xff08;了解&#xff09; 5.4 全域散列法&#xff08;了解&#xff09; 5.5 其他方法&#xff08;了解&#xff09…

PostgreSQL面試題及詳細答案120道(21-40)

《前后端面試題》專欄集合了前后端各個知識模塊的面試題&#xff0c;包括html&#xff0c;javascript&#xff0c;css&#xff0c;vue&#xff0c;react&#xff0c;java&#xff0c;Openlayers&#xff0c;leaflet&#xff0c;cesium&#xff0c;mapboxGL&#xff0c;threejs&…

數據建模及基本數據分析

目錄 &#xff08;一&#xff09;數據建模 1.以數據預測為核心的建模 2.以數據聚類為核心的建模 &#xff08;二&#xff09;基本數據分析 1.Numpy 2. Pandas 3.實例 4.Matplotlib 資料自取&#xff1a; 鏈接: https://pan.baidu.com/s/1PROmz-2hR3VCTd6Eei6lFQ?pwdy8…