批處理框架 Spring Batch 這么強,你會用嗎


https://mp.weixin.qq.com/s/yid8mKnwFQ8BhBgAldrWUQ
批處理框架 Spring Batch 這么強,你會用嗎

spring batch 簡介
spring batch 是 spring 提供的一個數據處理框架。企業域中的許多應用程序需要批量處理才能在關鍵任務環境中執行業務操作。這些業務運營包括:

無需用戶交互即可最有效地處理大量信息的自動化,復雜處理。這些操作通常包括基於時間的事件(例如月末計算,通知或通信)。

在非常大的數據集中重復處理復雜業務規則的定期應用(例如,保險利益確定或費率調整)。

集成從內部和外部系統接收的信息,這些信息通常需要以事務方式格式化,驗證和處理到記錄系統中。批處理用於每天為企業處理數十億的交易。

Spring Batch 是一個輕量級,全面的批處理框架,旨在開發對企業系統日常運營至關重要的強大批處理應用程序。Spring Batch 構建了人們期望的 Spring Framework 特性(生產力,基於 POJO 的開發方法和一般易用性),同時使開發人員可以在必要時輕松訪問和利用更高級的企業服務。Spring Batch 不是一個 schuedling 的框架。

Spring Batch 提供了可重用的功能,這些功能對於處理大量的數據至關重要,包括記錄 / 跟蹤,事務管理,作業處理統計,作業重啟,跳過和資源管理。它還提供更高級的技術服務和功能,通過優化和分區技術實現極高容量和高性能的批處理作業。

Spring Batch 可用於兩種簡單的用例(例如將文件讀入數據庫或運行存儲過程)以及復雜的大量用例(例如在數據庫之間移動大量數據,轉換它等等) 上)。大批量批處理作業可以高度可擴展的方式利用該框架來處理大量信息。

Spring Batch 架構介紹
一個典型的批處理應用程序大致如下:

從數據庫,文件或隊列中讀取大量記錄。

以某種方式處理數據。

以修改之后的形式寫回數據。

其對應的示意圖如下:

                <h2 style="box-sizing: border-box;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 20px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">spring batch 簡介</h2><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">spring batch 是 spring 提供的一個數據處理框架。企業域中的許多應用程序需要批量處理才能在關鍵任務環境中執行業務操作。這些業務運營包括:</p><ul style="padding-left: 20px;list-style-position: initial;list-style-image: initial;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;margin-top: 2px !important;list-style-type: square !important;" class="list-paddingleft-2"><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">無需用戶交互即可最有效地處理大量信息的自動化,復雜處理。這些操作通常包括基於時間的事件(例如月末計算,通知或通信)。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">在非常大的數據集中重復處理復雜業務規則的定期應用(例如,保險利益確定或費率調整)。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">集成從內部和外部系統接收的信息,這些信息通常需要以事務方式格式化,驗證和處理到記錄系統中。批處理用於每天為企業處理數十億的交易。</p></li></ul><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">Spring Batch 是一個輕量級,全面的批處理框架,旨在開發對企業系統日常運營至關重要的強大批處理應用程序。Spring Batch 構建了人們期望的 Spring Framework 特性(生產力,基於 POJO 的開發方法和一般易用性),同時使開發人員可以在必要時輕松訪問和利用更高級的企業服務。Spring Batch 不是一個 schuedling 的框架。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">Spring Batch 提供了可重用的功能,這些功能對於處理大量的數據至關重要,包括記錄 / 跟蹤,事務管理,作業處理統計,作業重啟,跳過和資源管理。它還提供更高級的技術服務和功能,通過優化和分區技術實現極高容量和高性能的批處理作業。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">Spring Batch 可用於兩種簡單的用例(例如將文件讀入數據庫或運行存儲過程)以及復雜的大量用例(例如在數據庫之間移動大量數據,轉換它等等) 上)。大批量批處理作業可以高度可擴展的方式利用該框架來處理大量信息。</p><h2 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 20px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">Spring Batch 架構介紹</h2><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">一個典型的批處理應用程序大致如下:</p><ul style="padding-left: 20px;list-style-position: initial;list-style-image: initial;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;margin-top: 2px !important;list-style-type: square !important;" class="list-paddingleft-2"><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">從數據庫,文件或隊列中讀取大量記錄。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">以某種方式處理數據。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">以修改之后的形式寫回數據。</p></li></ul><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">其對應的示意圖如下:</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><img data-ratio="0.19736842105263158" data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOQwLL1iae4yZQ3exf5xXWLyF9BQHnpmCgMVa2pvleQgPf1eRh1Q6l6HQ/640?wx_fmt=png" data-type="png" data-w="912" style="box-sizing: border-box; border-width: 2px; border-style: solid; border-color: rgb(238, 238, 238); border-radius: 6px; width: 912px !important; height: auto !important; visibility: visible !important;" _width="912px" class="" src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOQwLL1iae4yZQ3exf5xXWLyF9BQHnpmCgMVa2pvleQgPf1eRh1Q6l6HQ/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" crossorigin="anonymous" alt="圖片" data-fail="0"></p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">spring batch 的一個總體的架構如下:</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><img data-ratio="0.3983739837398374" data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOCm1Sz30ZfLPoo10nHp3XPicbsMVLzhsYWwYP1LdQcSQibUxlLyCjcSSg/640?wx_fmt=png" data-type="png" data-w="738" style="box-sizing: border-box; border-width: 2px; border-style: solid; border-color: rgb(238, 238, 238); border-radius: 6px; width: 738px !important; height: auto !important; visibility: visible !important;" _width="738px" class="" src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOCm1Sz30ZfLPoo10nHp3XPicbsMVLzhsYWwYP1LdQcSQibUxlLyCjcSSg/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" crossorigin="anonymous" alt="圖片" data-fail="0"></p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">在 spring batch 中一個 job 可以定義很多的步驟 step,在每一個 step 里面可以定義其專屬的 ItemReader 用於讀取數據,ItemProcesseor 用於處理數據,ItemWriter 用於寫數據,而每一個定義的 job 則都在 JobRepository 里面,我們可以通過 JobLauncher 來啟動某一個 job。</p><h2 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 20px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">Spring Batch 核心概念介紹</h2><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">下面是一些概念是 Spring batch 框架中的核心概念。</p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 Job</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">Job 和 Step 是 spring batch 執行批處理任務最為核心的兩個概念。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">其中 Job 是一個封裝整個批處理過程的一個概念。Job 在 spring batch 的體系當中只是一個最頂層的一個抽象概念,體現在代碼當中則它只是一個最上層的接口,其代碼如下:</p><pre style="box-sizing: border-box;padding: 0.5em;font-family: Courier, &quot;Courier New&quot;, monospace;background: rgb(45, 45, 45);border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);overflow-x: auto;color: rgb(204, 204, 204);font-size: 15px;text-align: start;"><code style="box-sizing: border-box;font-family: Courier, &quot;Courier New&quot;, monospace;display: inline;overflow: initial;line-height: inherit;overflow-wrap: normal;border-width: 0px;border-style: initial;border-color: initial;font-size: 13.5px;"><span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;*&nbsp;Batch&nbsp;domain&nbsp;object&nbsp;representing&nbsp;a&nbsp;job.&nbsp;Job&nbsp;is&nbsp;an&nbsp;explicit&nbsp;abstraction<br>&nbsp;*&nbsp;representing&nbsp;the&nbsp;configuration&nbsp;of&nbsp;a&nbsp;job&nbsp;specified&nbsp;by&nbsp;a&nbsp;developer.&nbsp;It&nbsp;should<br>&nbsp;*&nbsp;be&nbsp;noted&nbsp;that&nbsp;restart&nbsp;policy&nbsp;is&nbsp;applied&nbsp;to&nbsp;the&nbsp;job&nbsp;as&nbsp;a&nbsp;whole&nbsp;and&nbsp;not&nbsp;to&nbsp;a<br>&nbsp;*&nbsp;step.<br>&nbsp;*/</span><br><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">interface</span>&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">Job</span>&nbsp;</span>{<br>&nbsp;<br>&nbsp;<span style="box-sizing: border-box;">String&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getName</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<br>&nbsp;<br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">boolean</span>&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">isRestartable</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<br>&nbsp;<br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">void</span>&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">execute</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">(JobExecution&nbsp;execution)</span></span>;<br>&nbsp;<br>&nbsp;<br>&nbsp;<span style="box-sizing: border-box;">JobParametersIncrementer&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getJobParametersIncrementer</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<br>&nbsp;<br>&nbsp;<span style="box-sizing: border-box;">JobParametersValidator&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getJobParametersValidator</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<br>}<br><br><br></code></pre><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">在 Job 這個接口當中定義了五個方法,它的實現類主要有兩種類型的 job,一個是 simplejob,另一個是 flowjob。在 spring batch 當中,job 是最頂層的抽象,除 job 之外我們還有 JobInstance 以及 JobExecution 這兩個更加底層的抽象。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">一個 job 是我們運行的基本單位,它內部由 step 組成。job 本質上可以看成 step 的一個容器。一個 job 可以按照指定的邏輯順序組合 step,並提供了我們給所有 step 設置相同屬性的方法,例如一些事件監聽,跳過策略。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">Spring Batch 以 SimpleJob 類的形式提供了 Job 接口的默認簡單實現,它在 Job 之上創建了一些標准功能。一個使用 java config 的例子代碼如下:</p><pre style="box-sizing: border-box;padding: 0.5em;font-family: Courier, &quot;Courier New&quot;, monospace;background: rgb(45, 45, 45);border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);overflow-x: auto;color: rgb(204, 204, 204);font-size: 15px;text-align: start;"><code style="box-sizing: border-box;font-family: Courier, &quot;Courier New&quot;, monospace;display: inline;overflow: initial;line-height: inherit;overflow-wrap: normal;border-width: 0px;border-style: initial;border-color: initial;font-size: 13.5px;"><span style="box-sizing: border-box;color: rgb(249, 145, 87);">@Bean</span><br><span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;Job&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">footballJob</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">return</span>&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">this</span>.jobBuilderFactory.get(<span style="box-sizing: border-box;color: rgb(153, 204, 153);">"footballJob"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.start(playerLoad())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.next(gameLoad())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.next(playerSummarization())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.end()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.build();<br>}<br><br><br></code></pre><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">這個配置的意思是:首先給這個 job 起了一個名字叫 footballJob,接着指定了這個 job 的三個 step,他們分別由方法,playerLoad,gameLoad, playerSummarization 實現。</p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 JobInstance</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">我們在上文已經提到了 JobInstance,他是 Job 的更加底層的一個抽象,他的定義如下:</p><pre style="box-sizing: border-box;padding: 0.5em;font-family: Courier, &quot;Courier New&quot;, monospace;background: rgb(45, 45, 45);border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);overflow-x: auto;color: rgb(204, 204, 204);font-size: 15px;text-align: start;"><code style="box-sizing: border-box;font-family: Courier, &quot;Courier New&quot;, monospace;display: inline;overflow: initial;line-height: inherit;overflow-wrap: normal;border-width: 0px;border-style: initial;border-color: initial;font-size: 13.5px;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">interface</span>&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">JobInstance</span>&nbsp;</span>{<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;unique&nbsp;id&nbsp;for&nbsp;this&nbsp;JobInstance.<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;instance&nbsp;id<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">long</span>&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getInstanceId</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;job&nbsp;name.<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;value&nbsp;of&nbsp;'id'&nbsp;attribute&nbsp;from&nbsp;&lt;job&gt;<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;String&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getJobName</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;&nbsp;<br>}<br><br><br></code></pre><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">他的方法很簡單,一個是返回 Job 的 id,另一個是返回 Job 的名字。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">JobInstance 指的是 job 運行當中,作業執行過程當中的概念。Instance 本就是實例的意思。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">比如說現在有一個批處理的 job,它的功能是在一天結束時執行行一次。我們假定這個批處理 job 的名字為'EndOfDay'。在這個情況下,那么每天就會有一個邏輯意義上的 JobInstance, 而我們必須記錄 job 的每次運行的情況。</p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 JobParameters</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">在上文當中我們提到了,同一個 job 每天運行一次的話,那么每天都有一個 jobIntsance,但他們的 job 定義都是一樣的,那么我們怎么來區別一個 job 的不同 jobinstance 了。不妨先做個猜想,雖然 jobinstance 的 job 定義一樣,但是他們有的東西就不一樣,例如運行時間。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">spring batch 中提供的用來標識一個 jobinstance 的東西是:JobParameters。JobParameters 對象包含一組用於啟動批處理作業的參數,它可以在運行期間用於識別或甚至用作參考數據。我們假設的運行時間,就可以作為一個 JobParameters。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">例如, 我們前面的'EndOfDay'的 job 現在已經有了兩個實例,一個產生於 1 月 1 日,另一個產生於 1 月 2 日,那么我們就可以定義兩個 JobParameter 對象:一個的參數是 01-01, 另一個的參數是 01-02。因此,識別一個 JobInstance 的方法可以定義為:</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><a href="http://mp.weixin.qq.com/s?__biz=MzU2MTI4MjI0MQ==&amp;mid=2247493168&amp;idx=3&amp;sn=4a2eb3c0ad574dd58bda8664746aae00&amp;chksm=fc798b9ecb0e0288697af194f241f3c3da73381f69116f72db3a0fdf92b6aa4e19dd3c5c285d&amp;scene=21#wechat_redirect" style="box-sizing: border-box;color: rgb(30, 107, 184);word-break: break-all !important;" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="inset: auto;margin: 0px;"><img data-ratio="0.22777777777777777" data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOibutf8HupicCk3RwtB7DsLA0B6eUzGCn2WzB3RVA8oyibazibEskicIcYag/640?wx_fmt=png" data-type="png" data-w="1080" style="box-sizing: border-box; border-width: 2px; border-style: solid; border-color: rgb(238, 238, 238); border-radius: 6px; margin: 0px; width: 923px !important; height: auto !important; visibility: visible !important;" _width="923px" class="" src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOibutf8HupicCk3RwtB7DsLA0B6eUzGCn2WzB3RVA8oyibazibEskicIcYag/640?wx_fmt=png&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" crossorigin="anonymous" alt="圖片" data-fail="0"></span></a></p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">因此,我么可以通過 Jobparameter 來操作正確的 JobInstance</p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 JobExecution</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">JobExecution 指的是單次嘗試運行一個我們定義好的 Job 的代碼層面的概念。job 的一次執行可能以失敗也可能成功。只有當執行成功完成時,給定的與執行相對應的 JobInstance 才也被視為完成。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">還是以前面描述的 EndOfDay 的 job 作為示例,假設第一次運行 01-01-2019 的 JobInstance 結果是失敗。那么此時如果使用與第一次運行相同的 Jobparameter 參數(即 01-01-2019)作業參數再次運行,那么就會創建一個對應於之前 jobInstance 的一個新的 JobExecution 實例, JobInstance 仍然只有一個。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">JobExecution 的接口定義如下:</p><pre style="box-sizing: border-box;padding: 0.5em;font-family: Courier, &quot;Courier New&quot;, monospace;background: rgb(45, 45, 45);border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);overflow-x: auto;color: rgb(204, 204, 204);font-size: 15px;text-align: start;"><code style="box-sizing: border-box;font-family: Courier, &quot;Courier New&quot;, monospace;display: inline;overflow: initial;line-height: inherit;overflow-wrap: normal;border-width: 0px;border-style: initial;border-color: initial;font-size: 13.5px;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">interface</span>&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">JobExecution</span>&nbsp;</span>{<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;unique&nbsp;id&nbsp;for&nbsp;this&nbsp;JobExecution.<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;execution&nbsp;id<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">long</span>&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getExecutionId</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;job&nbsp;name.<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;value&nbsp;of&nbsp;'id'&nbsp;attribute&nbsp;from&nbsp;&lt;job&gt;<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;String&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getJobName</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;&nbsp;<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;batch&nbsp;status&nbsp;of&nbsp;this&nbsp;execution.<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;batch&nbsp;status&nbsp;value.<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;BatchStatus&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getBatchStatus</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;time&nbsp;execution&nbsp;entered&nbsp;STARTED&nbsp;status.&nbsp;<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;date&nbsp;(time)<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;Date&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getStartTime</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;time&nbsp;execution&nbsp;entered&nbsp;end&nbsp;status:&nbsp;COMPLETED,&nbsp;STOPPED,&nbsp;FAILED&nbsp;<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;date&nbsp;(time)<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;Date&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getEndTime</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;execution&nbsp;exit&nbsp;status.<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;exit&nbsp;status.<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;String&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getExitStatus</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;time&nbsp;execution&nbsp;was&nbsp;created.<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;date&nbsp;(time)<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;Date&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getCreateTime</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;time&nbsp;execution&nbsp;was&nbsp;last&nbsp;updated&nbsp;updated.<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;date&nbsp;(time)<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;Date&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getLastUpdatedTime</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<span style="box-sizing: border-box;color: rgb(153, 153, 153);">/**<br>&nbsp;&nbsp;*&nbsp;Get&nbsp;job&nbsp;parameters&nbsp;for&nbsp;this&nbsp;execution.<br>&nbsp;&nbsp;*&nbsp;<span style="box-sizing: border-box;font-weight: 700;">@return</span>&nbsp;job&nbsp;parameters&nbsp;&nbsp;<br>&nbsp;&nbsp;*/</span><br>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;Properties&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">getJobParameters</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span></span>;<br>&nbsp;<br>}<br><br><br></code></pre><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">每一個方法的注釋已經解釋的很清楚,這里不再多做解釋。只提一下 BatchStatus,JobExecution 當中提供了一個方法 getBatchStatus 用於獲取一個 job 某一次特地執行的一個狀態。BatchStatus 是一個代表 job 狀態的枚舉類,其定義如下:</p><pre style="box-sizing: border-box;padding: 0.5em;font-family: Courier, &quot;Courier New&quot;, monospace;background: rgb(45, 45, 45);border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);overflow-x: auto;color: rgb(204, 204, 204);font-size: 15px;text-align: start;"><code style="box-sizing: border-box;font-family: Courier, &quot;Courier New&quot;, monospace;display: inline;overflow: initial;line-height: inherit;overflow-wrap: normal;border-width: 0px;border-style: initial;border-color: initial;font-size: 13.5px;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">enum</span>&nbsp;BatchStatus&nbsp;{STARTING,&nbsp;STARTED,&nbsp;STOPPING,&nbsp;<br>&nbsp;&nbsp;&nbsp;STOPPED,&nbsp;FAILED,&nbsp;COMPLETED,&nbsp;ABANDONED&nbsp;}<br><br><br></code></pre><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">這些屬性對於一個 job 的執行來說是非常關鍵的信息,並且 spring batch 會將他們持久到數據庫當中. 在使用 Spring batch 的過程當中 spring batch 會自動創建一些表用於存儲一些 job 相關的信息,用於存儲 JobExecution 的表為<code style="box-sizing: border-box;padding-right: 5px;padding-left: 5px;font-family: Courier, &quot;Courier New&quot;, monospace;background-color: bisque;">batch_job_execution</code>, 下面是一個從數據庫當中截圖的實例:</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><a href="http://mp.weixin.qq.com/s?__biz=MzU2MTI4MjI0MQ==&amp;mid=2247493168&amp;idx=3&amp;sn=4a2eb3c0ad574dd58bda8664746aae00&amp;chksm=fc798b9ecb0e0288697af194f241f3c3da73381f69116f72db3a0fdf92b6aa4e19dd3c5c285d&amp;scene=21#wechat_redirect" style="box-sizing: border-box;color: rgb(30, 107, 184);word-break: break-all !important;" data-linktype="1"><span class="js_jump_icon h5_image_link" data-positionback="static" style="inset: auto;margin: 0px;"><img data-ratio="0.15648148148148147" data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHO44wIpktQ1MlLKbvzxueibJ8NTvAWgOPs8n1BqHyv7nPdiaf0EzEyTNQw/640?wx_fmt=png" data-type="png" data-w="1080" style="box-sizing: border-box; border-width: 2px; border-style: solid; border-color: rgb(238, 238, 238); border-radius: 6px; margin: 0px; width: 923px !important; height: 147.806px !important;" _width="923px" class="img_loading" src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" crossorigin="anonymous" alt="圖片"></span></a></p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 Step</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">每一個 Step 對象都封裝了批處理作業的一個獨立的階段。事實上,每一個 Job 本質上都是由一個或多個步驟組成。每一個 step 包含定義和控制實際批處理所需的所有信息。任何特定的內容都由編寫 Job 的開發人員自行決定。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">一個 step 可以非常簡單也可以非常復雜。例如,一個 step 的功能是將文件中的數據加載到數據庫中,那么基於現在 spring batch 的支持則幾乎不需要寫代碼。更復雜的 step 可能具有復雜的業務邏輯,這些邏輯作為處理的一部分。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">與 Job 一樣,Step 具有與 JobExecution 類似的 StepExecution,如下圖所示:</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><img data-ratio="0.7348643006263048" data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHORQYh6icbz3wicnsDM7NDEJicG3Pe795WBrWKFz52ic7kGjYTcSXwXoPuqw/640?wx_fmt=png" data-type="png" data-w="479" style="box-sizing: border-box; border-width: 2px; border-style: solid; border-color: rgb(238, 238, 238); border-radius: 6px; width: 479px !important; height: 353.061px !important;" _width="479px" class="img_loading" src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" crossorigin="anonymous" alt="圖片"></p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 StepExecution</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">StepExecution 表示一次執行 Step, 每次運行一個 Step 時都會創建一個新的 StepExecution,類似於 JobExecution。但是,某個步驟可能由於其之前的步驟失敗而無法執行。且僅當 Step 實際啟動時才會創建 StepExecution。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">一次 step 執行的實例由 StepExecution 類的對象表示。每個 StepExecution 都包含對其相應步驟的引用以及 JobExecution 和事務相關的數據,例如提交和回滾計數以及開始和結束時間。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">此外,每個步驟執行都包含一個 ExecutionContext,其中包含開發人員需要在批處理運行中保留的任何數據,例如重新啟動所需的統計信息或狀態信息。下面是一個從數據庫當中截圖的實例:</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><img data-ratio="0.14907407407407408" data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOAdyggEFJibtcJLjYv9CIz44n9E4l7yuJv6YqyPM1LH72MnWV1YszwIw/640?wx_fmt=png" data-type="png" data-w="1080" style="box-sizing: border-box; border-width: 2px; border-style: solid; border-color: rgb(238, 238, 238); border-radius: 6px; width: 923px !important; height: 140.999px !important;" _width="923px" class="img_loading" src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" crossorigin="anonymous" alt="圖片"></p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 ExecutionContext</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">ExecutionContext 即每一個 StepExecution 的執行環境。它包含一系列的鍵值對。我們可以用如下代碼獲取 ExecutionContext</p><pre style="box-sizing: border-box;padding: 0.5em;font-family: Courier, &quot;Courier New&quot;, monospace;background: rgb(45, 45, 45);border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);overflow-x: auto;color: rgb(204, 204, 204);font-size: 15px;text-align: start;"><code style="box-sizing: border-box;font-family: Courier, &quot;Courier New&quot;, monospace;display: inline;overflow: initial;line-height: inherit;overflow-wrap: normal;border-width: 0px;border-style: initial;border-color: initial;font-size: 13.5px;">ExecutionContext&nbsp;ecStep&nbsp;=&nbsp;stepExecution.getExecutionContext();<br>ExecutionContext&nbsp;ecJob&nbsp;=&nbsp;jobExecution.getExecutionContext();<br><br><br></code></pre><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 JobRepository</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">JobRepository 是一個用於將上述 job,step 等概念進行持久化的一個類。它同時給 Job 和 Step 以及下文會提到的 JobLauncher 實現提供 CRUD 操作。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">首次啟動 Job 時,將從 repository 中獲取 JobExecution,並且在執行批處理的過程中,StepExecution 和 JobExecution 將被存儲到 repository 當中。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><code style="box-sizing: border-box;padding-right: 5px;padding-left: 5px;font-family: Courier, &quot;Courier New&quot;, monospace;background-color: bisque;">@EnableBatchProcessing</code>注解可以為 JobRepository 提供自動配置。</p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 JobLauncher</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">JobLauncher 這個接口的功能非常簡單,它是用於啟動指定了 JobParameters 的 Job,為什么這里要強調指定了 JobParameter,原因其實我們在前面已經提到了,jobparameter 和 job 一起才能組成一次 job 的執行。下面是代碼實例:</p><pre style="box-sizing: border-box;padding: 0.5em;font-family: Courier, &quot;Courier New&quot;, monospace;background: rgb(45, 45, 45);border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);overflow-x: auto;color: rgb(204, 204, 204);font-size: 15px;text-align: start;"><code style="box-sizing: border-box;font-family: Courier, &quot;Courier New&quot;, monospace;display: inline;overflow: initial;line-height: inherit;overflow-wrap: normal;border-width: 0px;border-style: initial;border-color: initial;font-size: 13.5px;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;<span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">interface</span>&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">JobLauncher</span>&nbsp;</span>{<br>&nbsp;<br><span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;JobExecution&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">run</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">(Job&nbsp;job,&nbsp;JobParameters&nbsp;jobParameters)</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">throws</span>&nbsp;JobExecutionAlreadyRunningException,&nbsp;JobRestartException,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JobInstanceAlreadyCompleteException,&nbsp;JobParametersInvalidException</span>;<br>}<br><br><br></code></pre><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">上面 run 方法實現的功能是根據傳入的 job 以及 jobparamaters 從 JobRepository 獲取一個 JobExecution 並執行 Job。</p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 Item Reader</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">ItemReader 是一個讀數據的抽象,它的功能是為每一個 Step 提供數據輸入。當 ItemReader 以及讀完所有數據時,它會返回 null 來告訴后續操作數據已經讀完。Spring Batch 為 ItemReader 提供了非常多的有用的實現類,比如 JdbcPagingItemReader,JdbcCursorItemReader 等等。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">ItemReader 支持的讀入的數據源也是非常豐富的,包括各種類型的數據庫,文件,數據流,等等。幾乎涵蓋了我們的所有場景。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">下面是一個 JdbcPagingItemReader 的例子代碼:</p><pre style="box-sizing: border-box;padding: 0.5em;font-family: Courier, &quot;Courier New&quot;, monospace;background: rgb(45, 45, 45);border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);overflow-x: auto;color: rgb(204, 204, 204);font-size: 15px;text-align: start;"><code style="box-sizing: border-box;font-family: Courier, &quot;Courier New&quot;, monospace;display: inline;overflow: initial;line-height: inherit;overflow-wrap: normal;border-width: 0px;border-style: initial;border-color: initial;font-size: 13.5px;"><span style="box-sizing: border-box;color: rgb(249, 145, 87);">@Bean</span><br><span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;JdbcPagingItemReader&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">itemReader</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">(DataSource&nbsp;dataSource,&nbsp;PagingQueryProvider&nbsp;queryProvider)</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Map&lt;String,&nbsp;Object&gt;&nbsp;parameterValues&nbsp;=&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">new</span>&nbsp;HashMap&lt;&gt;();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameterValues.put(<span style="box-sizing: border-box;color: rgb(153, 204, 153);">"status"</span>,&nbsp;<span style="box-sizing: border-box;color: rgb(153, 204, 153);">"NEW"</span>);<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">return</span>&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">new</span>&nbsp;JdbcPagingItemReaderBuilder&lt;CustomerCredit&gt;()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.name(<span style="box-sizing: border-box;color: rgb(153, 204, 153);">"creditReader"</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.dataSource(dataSource)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.queryProvider(queryProvider)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.parameterValues(parameterValues)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.rowMapper(customerCreditMapper())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.pageSize(<span style="box-sizing: border-box;color: rgb(249, 145, 87);">1000</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.build();<br>}<br>&nbsp;<br><span style="box-sizing: border-box;color: rgb(249, 145, 87);">@Bean</span><br><span style="box-sizing: border-box;"><span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">public</span>&nbsp;SqlPagingQueryProviderFactoryBean&nbsp;<span style="box-sizing: border-box;color: rgb(102, 153, 204);font-weight: 700;">queryProvider</span><span style="box-sizing: border-box;color: rgb(249, 145, 87);">()</span>&nbsp;</span>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SqlPagingQueryProviderFactoryBean&nbsp;provider&nbsp;=&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">new</span>&nbsp;SqlPagingQueryProviderFactoryBean();<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;provider.setSelectClause(<span style="box-sizing: border-box;color: rgb(153, 204, 153);">"select&nbsp;id,&nbsp;name,&nbsp;credit"</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;provider.setFromClause(<span style="box-sizing: border-box;color: rgb(153, 204, 153);">"from&nbsp;customer"</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;provider.setWhereClause(<span style="box-sizing: border-box;color: rgb(153, 204, 153);">"where&nbsp;status=:status"</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;provider.setSortKey(<span style="box-sizing: border-box;color: rgb(153, 204, 153);">"id"</span>);<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">return</span>&nbsp;provider;<br>}<br><br><br></code></pre><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">JdbcPagingItemReader 必須指定一個 PagingQueryProvider,負責提供 SQL 查詢語句來按分頁返回數據。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">下面是一個 JdbcCursorItemReader 的例子代碼:</p><pre style="box-sizing: border-box;padding: 0.5em;font-family: Courier, &quot;Courier New&quot;, monospace;background: rgb(45, 45, 45);border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);overflow-x: auto;color: rgb(204, 204, 204);font-size: 15px;text-align: start;"><code style="box-sizing: border-box;font-family: Courier, &quot;Courier New&quot;, monospace;display: inline;overflow: initial;line-height: inherit;overflow-wrap: normal;border-width: 0px;border-style: initial;border-color: initial;font-size: 13.5px;">&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">private</span>&nbsp;JdbcCursorItemReader&lt;Map&lt;String,&nbsp;Object&gt;&gt;&nbsp;buildItemReader(<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">final</span>&nbsp;DataSource&nbsp;dataSource,&nbsp;String&nbsp;tableName,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;tenant)&nbsp;{<br>&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JdbcCursorItemReader&lt;Map&lt;String,&nbsp;Object&gt;&gt;&nbsp;itemReader&nbsp;=&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">new</span>&nbsp;JdbcCursorItemReader&lt;&gt;();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemReader.setDataSource(dataSource);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemReader.setSql(<span style="box-sizing: border-box;color: rgb(153, 204, 153);">"sql&nbsp;here"</span>);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;itemReader.setRowMapper(<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">new</span>&nbsp;RowMapper());<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">return</span>&nbsp;itemReader;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br><br></code></pre><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 Item Writer</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">既然 ItemReader 是讀數據的一個抽象,那么 ItemWriter 自然就是一個寫數據的抽象,它是為每一個 step 提供數據寫出的功能。寫的單位是可以配置的,我們可以一次寫一條數據,也可以一次寫一個 chunk 的數據,關於 chunk 下文會有專門的介紹。ItemWriter 對於讀入的數據是不能做任何操作的。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">Spring Batch 為 ItemWriter 也提供了非常多的有用的實現類,當然我們也可以去實現自己的 writer 功能。</p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">什么是 Item Processor</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">ItemProcessor 對項目的業務邏輯處理的一個抽象, 當 ItemReader 讀取到一條記錄之后,ItemWriter 還未寫入這條記錄之前,I 我們可以借助 temProcessor 提供一個處理業務邏輯的功能,並對數據進行相應操作。如果我們在 ItemProcessor 發現一條數據不應該被寫入,可以通過返回 null 來表示。ItemProcessor 和 ItemReader 以及 ItemWriter 可以非常好的結合在一起工作,他們之間的數據傳輸也非常方便。我們直接使用即可。</p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">chunk 處理流程</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">spring batch 提供了讓我們按照 chunk 處理數據的能力,一個 chunk 的示意圖如下:</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><img data-ratio="0.5497326203208556" data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOXcCrjgicNUd4DHZaLN1NXTDZGsx0FWvVeDqYX1brCvOOwVZRGFiaZInQ/640?wx_fmt=png" data-type="png" data-w="935" style="box-sizing: border-box; border-width: 2px; border-style: solid; border-color: rgb(238, 238, 238); border-radius: 6px; width: 923px !important; height: 509.204px !important;" _width="923px" class="img_loading" src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" crossorigin="anonymous" alt="圖片"></p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">它的意思就和圖示的一樣,由於我們一次 batch 的任務可能會有很多的數據讀寫操作,因此一條一條的處理並向數據庫提交的話效率不會很高,因此 spring batch 提供了 chunk 這個概念,我們可以設定一個 chunk size,spring batch 將一條一條處理數據,但不提交到數據庫,只有當處理的數據數量達到 chunk size 設定的值得時候,才一起去 commit.</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">java 的實例定義代碼如下:</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><img data-ratio="0.7247706422018348" data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOfokDClf8icibOjezVniaW8mZK6huZUwVE2OCGic3aPq6oF8aGjJCiaw3RQw/640?wx_fmt=png" data-type="png" data-w="654" style="box-sizing: border-box; border-width: 2px; border-style: solid; border-color: rgb(238, 238, 238); border-radius: 6px; width: 654px !important; height: 475.101px !important;" _width="654px" class="img_loading" src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" crossorigin="anonymous" alt="圖片"></p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">在上面這個 step 里面,chunk size 被設為了 10,當 ItemReader 讀的數據數量達到 10 的時候,這一批次的數據就一起被傳到 itemWriter,同時 transaction 被提交。</p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">skip 策略和失敗處理</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">一個 batch 的 job 的 step,可能會處理非常大數量的數據,難免會遇到出錯的情況,出錯的情況雖出現的概率較小,但是我們不得不考慮這些情況,因為我們做數據遷移最重要的是要保證數據的最終一致性。spring batch 當然也考慮到了這種情況,並且為我們提供了相關的技術支持,請看如下 bean 的配置:</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><img data-ratio="0.4603550295857988" data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOQzliaaGe4mTNGZb8oFdkE0tsm4l5zR1UClxrVYt4ebxtwXoAe2eYUhg/640?wx_fmt=png" data-type="png" data-w="845" style="box-sizing: border-box; border-width: 2px; border-style: solid; border-color: rgb(238, 238, 238); border-radius: 6px; width: 845px !important; height: 391.159px !important;" _width="845px" class="img_loading" src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" crossorigin="anonymous" alt="圖片"></p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">我們需要留意這三個方法,分別是 skipLimit(),skip(),noSkip(),</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">skipLimit 方法的意思是我們可以設定一個我們允許的這個 step 可以跳過的異常數量,假如我們設定為 10,則當這個 step 運行時,只要出現的異常數目不超過 10,整個 step 都不會 fail。注意,若不設定 skipLimit,則其默認值是 0.</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">skip 方法我們可以指定我們可以跳過的異常,因為有些異常的出現,我們是可以忽略的。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">noSkip 方法的意思則是指出現這個異常我們不想跳過,也就是從 skip 的所以 exception 當中排除這個 exception,從上面的例子來說,也就是跳過所有除 FileNotFoundException 的 exception。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">那么對於這個 step 來說,FileNotFoundException 就是一個 fatal 的 exception,拋出這個 exception 的時候 step 就會直接 fail</p><h2 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 20px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">批處理操作指南</h2><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">本部分是一些使用 spring batch 時的值得注意的點</p><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">批處理原則</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">在構建批處理解決方案時,應考慮以下關鍵原則和注意事項。</p><ul style="padding-left: 20px;list-style-position: initial;list-style-image: initial;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;margin-top: 2px !important;list-style-type: square !important;" class="list-paddingleft-2"><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">批處理體系結構通常會影響體系結構</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">盡可能簡化並避免在單批應用程序中構建復雜的邏輯結構</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">保持數據的處理和存儲在物理上靠得很近(換句話說,將數據保存在處理過程中)。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">最大限度地減少系統資源的使用,尤其是 I / O. 在 internal memory 中執行盡可能多的操作。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">查看應用程序 I / O(分析 SQL 語句)以確保避免不必要的物理 I / O. 特別是,需要尋找以下四個常見缺陷:</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">當數據可以被讀取一次並緩存或保存在工作存儲中時,讀取每個事務的數據。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">重新讀取先前在同一事務中讀取數據的事務的數據。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">導致不必要的表或索引掃描。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">未在 SQL 語句的 WHERE 子句中指定鍵值。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">在批處理運行中不要做兩次一樣的事情。例如,如果需要數據匯總以用於報告目的,則應該(如果可能)在最初處理數據時遞增存儲的總計,因此您的報告應用程序不必重新處理相同的數據。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">在批處理應用程序開始時分配足夠的內存,以避免在此過程中進行耗時的重新分配。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">總是假設數據完整性最差。插入適當的檢查和記錄驗證以維護數據完整性。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">盡可能實施校驗和以進行內部驗證。例如,對於一個文件里的數據應該有一個數據條數紀錄,告訴文件中的記錄總數以及關鍵字段的匯總。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">在具有真實數據量的類似生產環境中盡早計划和執行壓力測試。</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p style="box-sizing: border-box;margin-bottom: 15px;">在大批量系統中,數據備份可能具有挑戰性,特別是如果系統以 24-7 在線的情況運行。數據庫備份通常在在線設計中得到很好的處理,但文件備份應該被視為同樣重要。如果系統依賴於文件,則文件備份過程不僅應該到位並記錄在案,還應定期進行測試。</p></li></ul><h2 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 20px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">如何默認不啟動 job</h2><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">在使用 java config 使用 spring batch 的 job 時,如果不做任何配置,項目在啟動時就會默認去跑我們定義好的批處理 job。那么如何讓項目在啟動時不自動去跑 job 呢?</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">spring batch 的 job 會在項目啟動時自動 run,如果我們不想讓他在啟動時 run 的話,可以在 application.properties 中添加如下屬性:</p><pre style="box-sizing: border-box;padding: 0.5em;font-family: Courier, &quot;Courier New&quot;, monospace;background: rgb(45, 45, 45);border-width: 1px;border-style: solid;border-color: rgb(221, 221, 221);overflow-x: auto;color: rgb(204, 204, 204);font-size: 15px;text-align: start;"><code style="box-sizing: border-box;font-family: Courier, &quot;Courier New&quot;, monospace;display: inline;overflow: initial;line-height: inherit;overflow-wrap: normal;border-width: 0px;border-style: initial;border-color: initial;font-size: 13.5px;">spring.batch.job.enabled=<span style="box-sizing: border-box;font-weight: 700;color: rgb(204, 153, 204);">false</span><br><br><br></code></pre><h4 style="box-sizing: border-box;margin-top: 2rem;margin-bottom: 0.5rem;font-weight: 700;color: rgb(255, 140, 0);line-height: 1.35;font-size: 15px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微軟雅黑, monospace !important;">在讀數據時內存不夠</h4><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">在使用 spring batch 做數據遷移時,發現在 job 啟動后,執行到一定時間點時就卡在一個地方不動了,且 log 也不再打印,等待一段時間之后,得到如下錯誤:</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><img data-ratio="0.14629629629629629" data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOwbolkNknYpaicYLSy3sH4YPx8SC8xVRVyoCOFbnhpXr1icFSdY1yjykw/640?wx_fmt=png" data-type="png" data-w="1080" style="box-sizing: border-box; border-width: 2px; border-style: solid; border-color: rgb(238, 238, 238); border-radius: 6px; width: 923px !important; height: 138.446px !important;" _width="923px" class="img_loading" src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" crossorigin="anonymous" alt="圖片"></p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">紅字的信息為:<code style="box-sizing: border-box;padding-right: 5px;padding-left: 5px;font-family: Courier, &quot;Courier New&quot;, monospace;background-color: bisque;">Resource exhaustion event:the JVM was unable to allocate memory from the heap.</code></p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">翻譯過來的意思就是項目發出了一個資源耗盡的事件,告訴我們 java 虛擬機無法再為堆分配內存。</p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;">造成這個錯誤的原因是: 這個項目里的 batch job 的 reader 是一次性拿回了數據庫里的所有數據,並沒有進行分頁,當這個數據量太大時,就會導致內存不夠用。解決的辦法有兩個:</p><ul style="padding-left: 20px;list-style-position: initial;list-style-image: initial;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;margin-top: 2px !important;list-style-type: square !important;" class="list-paddingleft-2"><li style="box-sizing: border-box;margin-top: 2px !important;"><p>調整 reader 讀數據邏輯,按分頁讀取,但實現上會麻煩一些,且運行效率會下降</p></li><li style="box-sizing: border-box;margin-top: 2px !important;"><p>增大 service 內存</p></li></ul><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><span style="box-sizing: border-box;">(感謝閱讀,希望對你所有幫助)</span></p><p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;color: rgb(80, 97, 109);font-family: Helvetica, Arial, sans-serif;font-size: 15px;text-align: start;white-space: normal;"><span style="box-sizing: border-box;">來源:blog.csdn.net/topdeveloperr/article/details/84337956</span></p><p style="text-align: center;"><img data-src="https://mmbiz.qpic.cn/mmbiz_png/J5qtMtfPAwsYKtaCyzX14icyq81t3ItHOZcqeviaJCxfria9UsIWZZX4LoCGcQIqoeicEdC2p4VBLrrS3qaRFZwoUw/640?wx_fmt=png" data-type="png" style="width: 360px !important; height: 200.889px !important;" data-ratio="0.5555555555555556" data-w="1080" _width="360px" class="img_loading" src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" crossorigin="anonymous" alt="圖片"></p>
            </div>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM