自定义ListItemReader春季批次
我对 listItemreader 的自定义实现进行了编码,并在Spring Batch的GitHub中遵循该示例。无论如何,就我而言,我需要从 JobContext 中读取一个变量,此变量是我必须读取包含文件的途径。我无法使用构造函数,因为构造函数在 beforestep 事件之前执行,并且目前我没有这些。
无论如何,这将首先执行,但是如果列表再也不会转到null,我将无法再次执行 initialize
方法。
如果我尝试在!list.isempty()
条件中添加另一个,将我的列表设置为null。我进入无限循环。
还有其他方法可以解决这个问题吗?也许我过度复杂化了。
public class ListItemReader<Path> implements ItemReader<Path>, StepExecutionListener {
private List<Path> list;
private org.springframework.batch.core.JobExecution jobExecution;
public ListItemReader() {
this.list = new ArrayList<>();
}
public void initialize(){
//Here I made an listdirectories of a path and add all the files to the list
String pathString = jobExecution.getExecutionContext().getString(Constants.CONTEXT_PATH);
Path path = Paths.get(pathString );
...
items.add(Paths.get(..path..));
}
@Nullable
@Override
public T read() {
if(list == null) initialize();
if (!list.isEmpty()) {
return list.remove(0);
}
return null;
}
@Override
public ExitStatus afterStep(StepExecution se) {
return ExitStatus.COMPLETED;
}
@Override
public void beforeStep(StepExecution se) {
jobExecution = se.getJobExecution();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您可以使用
@jobscope
和@stepscope
延迟bean构造函数。此外,您可以使用@Value
和SpringSpel
注入您的JobParameters
。在您的情况下,您可能需要重写代码,例如:
开始工作之前,您可以添加一些
JobParameters
:并将其添加到您的工作中:
参考
Actually, you can delay your bean constructor by using
@JobScope
and@StepScope
. Additionally, you can use@Value
and SpringSpEL
to inject yourjobParameters
.In your case, you might want to rewrite your code, for e.g:
Before starting your job, you can add some
jobParameters
:and add it to your job:
References