ItemWriter 完成后如何执行逻辑?
How to perform logic after ItemWriter has completed?
大家好,我是 SO 和 Springbatch 的新手。我用 Classifer 编写了一个批处理作业,它以两种不同的方式(即两个 ItemWriters)更新 table,具体取决于通过 ItemReader 检索到的内容以及所有工作正常的内容。现在,我想在 ItemWriters 完成更新后执行一些逻辑。我想做一些日志记录并使用之前检索到的同一组数据更新另一个 table。我怎样才能做到这一点?我查看了 ItemWriterListener,但它似乎无法执行特定于数据的逻辑。我做了一些搜索,但没有运气。任何帮助,将不胜感激。提前致谢!!
您可以尝试使用 StepExecutionListener 将其实现到您的 Writer Class 以在 ItemWriter 完成执行后执行逻辑。以下是 ItemWriter 的片段供您参考,
public class TestWriter implements ItemWriter<Test>, StepExecutionListener {
@Override
public void beforeStep(StepExecution stepExecution) {
}
@Override
public void write(List<? extends Test> items) throws Exception {
// Logic of Writer
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
// You can perform post logic after writer here inside afterStep based on your requirements
// Return custom exit status based on the run
return ExitStatus.COMPLETED;
}
}
Now, I want to perform some logic after the ItemWriters are done updating. I want to do some logging and update another table with the same set of data retrieved previously. How can I achieve this? I looked at ItemWriterListener but seems it cannot perform data specific logics.
由于您想对之前检索到的相同项目执行某些操作,因此您需要使用 ItemWriteListener#afterWrite
,因为此方法可让您访问刚刚写入的项目。
编辑:根据评论添加有关失败案例的详细信息
如果事务被回滚,方法ItemWriteListener#onWriteError
将被调用。请在 common patterns 部分找到有关此内容的更多详细信息。
大家好,我是 SO 和 Springbatch 的新手。我用 Classifer 编写了一个批处理作业,它以两种不同的方式(即两个 ItemWriters)更新 table,具体取决于通过 ItemReader 检索到的内容以及所有工作正常的内容。现在,我想在 ItemWriters 完成更新后执行一些逻辑。我想做一些日志记录并使用之前检索到的同一组数据更新另一个 table。我怎样才能做到这一点?我查看了 ItemWriterListener,但它似乎无法执行特定于数据的逻辑。我做了一些搜索,但没有运气。任何帮助,将不胜感激。提前致谢!!
您可以尝试使用 StepExecutionListener 将其实现到您的 Writer Class 以在 ItemWriter 完成执行后执行逻辑。以下是 ItemWriter 的片段供您参考,
public class TestWriter implements ItemWriter<Test>, StepExecutionListener {
@Override
public void beforeStep(StepExecution stepExecution) {
}
@Override
public void write(List<? extends Test> items) throws Exception {
// Logic of Writer
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
// You can perform post logic after writer here inside afterStep based on your requirements
// Return custom exit status based on the run
return ExitStatus.COMPLETED;
}
}
Now, I want to perform some logic after the ItemWriters are done updating. I want to do some logging and update another table with the same set of data retrieved previously. How can I achieve this? I looked at ItemWriterListener but seems it cannot perform data specific logics.
由于您想对之前检索到的相同项目执行某些操作,因此您需要使用 ItemWriteListener#afterWrite
,因为此方法可让您访问刚刚写入的项目。
编辑:根据评论添加有关失败案例的详细信息
如果事务被回滚,方法ItemWriteListener#onWriteError
将被调用。请在 common patterns 部分找到有关此内容的更多详细信息。