ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

批处理框架 Spring Batch 这么强,你会用吗

2021-07-06 22:32:55  阅读:195  来源: 互联网

标签:批处理 Spring batch Batch job nbsp spring


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>

标签:批处理,Spring,batch,Batch,job,nbsp,spring
来源: https://www.cnblogs.com/sunny3158/p/14979297.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有