我正在使用 jsr-352 规范的 JBeret 实现。
简而言之,这是我的工作配置:
<job id="expired-customer-cancellation" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd"
version="1.0" jsl-name="job-parent" parent="job-parent">
<step id="step1" next="step2">
<chunk item-count="#{jobParameters['chunksize']}?:3">
<reader ref="eccReader">
</reader>
<writer ref="eccWriter" />
</chunk>
<partition>
<mapper ref="eccMapper">
<properties>
<property name="threads" value="#{jobParameters['threads']}?:3"/>
<property name="records" value="#{jobParameters['records']}?:30"/>
</properties>
</mapper>
</partition>
</step>
<step id="step2">
<batchlet ref="eccMailBatchlet" />
</step>
</job>
Itemwriter 类执行以下操作:
@Named
public class EccWriter extends AbstractItemWriter {
@Inject
Logger logger;
@Inject
JobContext jobContext;
@Override
public void writeItems(List<Object> list) throws Exception {
@SuppressWarnings("unchecked")
ArrayList<String> processed = Optional.ofNullable(jobContext.getTransientUserData()).map(ArrayList.class::cast).orElse(new ArrayList<String>());
list.stream().map(UserLogin.class::cast).forEach(input -> {
if (someConditions) {
processed.add(input.getUserId());
}
});
jobContext.setTransientUserData(processed); // update job transient data with processed list
}
}
现在我希望在调用 jobContext.getTransientUserData() 时获得更新的列表step2,相反我得到的是null value.
此外,每个分区都有自己的 jobContext.transientUserData,因此它将始终以null分区开始时的值。
我认为 jobContext 本身可能会因其名称而误导常见错误。
在整个工作中传递一些数据的自然方式是什么?
这是当前 API 中的一个空白,我同意“线程本地”行为可能会令人惊讶。
您可以使用的一种技术是使用步骤持久用户数据。
例如。从步骤 1 开始:
StepExecution.setPersistentUserData(processed);
然后从步骤2开始:
@Inject
JobContext ctx;
List<StepExecution> stepExecs = BatchRuntime.getJobOperator().getStepExecutions(ctx.getInstanceId());
// ... not shown here, but sort through stepExecs and find the one from step1.
StepExecution step2Exec = ... ;
Serializable userData = step2Exec.getPersistentUserData()
之前已经指出这是一个需要改进的领域,并且应该在 Jakarta Batch(规范的新家)的未来增强中考虑这一点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)