Java批处理:jobContext transientUserData未通过步骤



我使用的是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
}
}

现在,我希望在step2上调用jobContext.getTransientUserData((时实现更新的列表,但我得到的只是一个null值。

此外,每个分区都有自己的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()

这一点在以前已经被注意到是一个需要改进的领域,在雅加达批次(规范的新家(的未来增强中应该考虑这一点。

相关内容

  • 没有找到相关文章

最新更新