天天看點

spring batch 1:簡介

最近因項目需要開始使用spring batch作為批處理方案,結合Quartz開發定時批處理任務。此次的子產品開發中使用了spring、mybatis。
另外需要說明:官方文檔中是結合spring boot使用,如果項目中使用了spring boot的同學不需要像本文中一樣還是使用xml配置定義job、step等。
一、spring batch
spring batch:建立在Spring架構的基礎之上,它遵循Spring的慣例.spring batch包括日志/跟蹤,事務管理,作業處理統計,作業重新啟動,跳過和資源管理。
二、spring batch 适用環境
    1. 比如商城的提貨通知,當某一批次物流到達後需要通知使用者提貨。
    2. 繳費扣款、續訂等業務:目前業務觸發則進行查詢、讀取适用資訊,再根據業務邏輯進行扣款、或者通知業務,完成後批量寫入庫。
    3. 優惠券等:優惠券到期、發放等。
    4. 等等等等。
三、spring batch 基本組成:Job、setp、ItemReader、ItemProcessor、ItemWriter、Partitioner
     1. org.springframework.batch.core.Job:batch Job,同時也是執行Job的入口;
     2. org.springframework.batch.core.Step:job中的step,提供了執行Step的能力;
     3. org.springframework.batch.item.ItemReader<T>:讀資料(包括檔案、資料庫等)提供了讀取資料的能力;
     4. org.springframework.batch.item.ItemProcessor<T>:業務處理(自定義業務)我們可以通過它應用業務邏輯到每一條要處理的資料;
     5. org.springframework.batch.item.ItemWriter<T>:寫資料(包括檔案、資料庫等)提供了寫資料的能力;
     6. org.springframework.batch.core.partition.support.Partitioner:參數處理、傳遞,包括任務中傳遞的參數,根據需求傳遞的參數等


附錄
1. [ IBM developerworks 詳細介紹了spring batch的使用 ](http://www.ibm.com/developerworks/cn/java/j-lo-springbatch1/)
2. [spring.io](http://projects.spring.io/spring-batch/#quick-start)
3. [Spring Batch中文文檔](https://kimmking.gitbooks.io/springbatchreference/content/01_introduction/11.html)
           

如有疑問請加公衆号(K171),如果覺得對您有幫助請 github start

spring batch 1:簡介