Spring Batch学习笔记——steps之间共享数据

<h2>名词说明:</h2> <p>上下文:</p> <p>执行:</p> <p>执行上下文:</p> <h2>案例:</h2> <p>警告:一旦steps共享数据,这些数据就会把这些steps链接起来。努力使steps独立。若是你实在是不能独立他们,才使用下面的技术。你应该把数据共享做为steps不能独立的后备方案。</p> <h2>1 数据共享方式:</h2> <ul> <li>a step存储共享数据到数据库,receiving step从数据库读取他们 </li> <li>Execution context(执行上下文): 使用Spring&#160; Batch execution context 做为data容器。a step往上下文写数据,一个step从上下文读数据 </li> <li>Holder:使用Spring bean和依赖注入。Spring往communicating beans中注入一个holder bean。第一个step往holder中set values,另外一个step从holder中reads values </li> </ul> <h2>2 使用execution context来共享数据</h2> <p>什么是execution context:ExecutionContext类就表明了执行上下文,它就是一个适合与批应用的 键值对的一个 map。下面就是一个从上下文读写数据的例子:</p> <blockquote> <pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">executionContext.putString(&quot;importId&quot;, importId); </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">String importId = jobExecutionContext.getString(&quot;importId&quot;);</pre></pre> </blockquote>数据库

<p>an execution context只是a job execution的一部分,而且不一样的执行上下文</p>ide

<h3>Jobs 和 steps 有它们本身的execution context</h3>spa

<p>&#160;&#160;&#160; Spring Batch提供了两种execution context: job execution context和step execution context。它们都是ExecutionContext类型可是它们的做用域不一样。下图说明了在一个job execution过程当中的两种执行上下文。</p>.net

<p><a href="http://static.oschina.net/uploads/img/201504/03000640_mlZS.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://static.oschina.net/uploads/img/201504/03000640_aOZS.png" width="244" height="95" /></a></p>接口

<p>那么怎么访问执行上下文呢?你须要引用对应的执行:JobExecution:若是你想访问job执行上下文,StepExecution:若是你想访问step执行上下文。几乎全部的Spring Batch artifacts可以很容易的访问JobExecution和StepExecution,不幸的是item reader,processor,writer功能不能访问它们。不过你能够实现一个监听接口(如:ItemStream)来洞察execution.下面列举了ItemReader经过实现ItemStream接口来访问执行上下文</p>作用域

<p>清单1:</p>get

<pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FilesInDirectoryItemReader <span style="color: #0000ff">implements</span> </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> ItemReader&lt;File&gt;, ItemStream {it

</pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">@Override </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> open(ExecutionContext executionContext) </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> <span style="color: #0000ff">throws</span> ItemStreamException { } </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">@Override </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> update(ExecutionContext executionContext) </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> <span style="color: #0000ff">throws</span> ItemStreamException { } </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">@Override </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> close() <span style="color: #0000ff">throws</span> ItemStreamException { } </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">@Override </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"><span style="color: #0000ff">public</span> File read() <span style="color: #0000ff">throws</span> Exception, UnexpectedInputException, </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff"> ParseException, NonTransientResourceException { (...) } </pre><pre style="font-size: 12px; font-family: consolas,&#39;Courier New&#39;,courier,monospace; width: 100%; margin: 0em; background-color: #ffffff">}</pre></pre>io

<pre>用job execution context在steps之间共享数据</pre>class

相关文章
相关标签/搜索