我有一个春季批处理任务,在输入文件中我有很少的记录,一些记录是有效的,而一些记录是无效的。在有效记录上,它应该写入输出文件,对于无效的记录,它应该写入错误文件,但有一些例外问题是当将某些东西写入错误文件时,它应该将退出代码设置为3.我尝试了很多方法,但无法设置退出代码,甚至在发生异常时终止该记录的实例。所以它不叫作家。
You probably don't want to use an exception here. As a general rule of thumb, it's best to avoid using exceptions for expected business logic. Instead, consider simply using your ItemProcessor
to return a GoodObject
(or the original item) if the record is valid and a BadObject
if the record is invalid.
Then, leverage a ClassifierCompositeItemWriter
to send the good records to one file ItemWriter
and the bad ones to the error file ItemWriter
.
Finally, there are a number of ways of determining whether or not any "bad" records are encountered. One simple way would be to put a class-level boolean
in your ItemProcessor
and then leverage the StepExecutionListener
afterStep
hook to check the flag and set the ExitCode
.
public class ValidatingItemProcessor implements ItemProcessor<Input, AbstractItem>, StepExecutionListener {
private boolean itemFailed = false;
@Override
public AbstractItem process(final Input item) throws Exception {
if (item.isValid()) {
return new GoodItem();
}
itemFailed = true;
return new BadItem();
}
@Override
public void beforeStep(final StepExecution stepExecution) {
//no-op
}
@Override
public ExitStatus afterStep(final StepExecution stepExecution) {
if (itemFailed) {
return new ExitStatus("3");
}
return null;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.