java - 编写器中的Spring批量读取作业参数
问题描述
我目前正在命令行中传递 spring 批处理作业参数中的文件名并运行我的作业,spring 批处理作业将查找文件并读取、处理和写入文件。我目前是读取器中的作业参数文件名并读取文件名,如何在处理器和写入器中使用相同的作业参数文件名。
解决方案
访问作业参数的一种方法是将 StepExecutionListener 实现到您的处理器/编写器类,以利用其覆盖的方法 beforeStep 和 afterStep,
public class TestWriter implements ItemWriter<TestData>,StepExecutionListener {
private String filePath;
@Override
public void beforeStep(StepExecution stepExecution) {
try {
filePath = (String) stepExecution.getJobExecution().getExecutionContext()
.get("filePath");
} catch (Exception e) {
logger.error("Exception while performing read {}", e);
}
}
@Override
public void write(List<? extends TestData> items) throws Exception {
// filePath value read from the job execution can be used inside read use case impl
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
return ExitStatus.COMPLETED;
}
}
要将值添加到 jobExecutionContext,您可以使用如下所示,
stepExecution.getJobExecution().getExecutionContext().put("path", filePath);
推荐阅读
- reactjs - 使用 React.lazy 给出的元素类型无效
- java - 如何将多个服务放在一个类中,该类在 Activiti 中实现 JavaDelegate 接口
- python - sys.argv[0] 总是什么都不返回
- c - 依次出现 fwrite 和 fread 问题
- xml - 如何使用 minidom 正确解析 xml
- java - 我如何将完全标记的数据集拆分为标记和未标记的数据以用于半监督学习
- python - Anaconda unsatisfiederror的Keras安装错误
- excel - Excel:使用IF和Count函数检查列中是否存在日期范围?
- javascript - 古兰经/经典阿拉伯语的语音识别
- ubuntu - 如何优化 nginx 负载均衡器组中 mysql vm 的性能?