简体   繁体   English

Spring Batch 错误处理

[英]Spring Batch error handling

I am new to Spring Batch.我是 Spring Batch 的新手。 I have a simple job that includes reader, processor, writer and listeners.我有一份简单的工作,包括读者、处理者、作者和听众。

Sample of a Step method code is below Step 方法代码示例如下

@Bean public Step step1() { return stepBuilderFactory.get("step1") .<Person, Person> chunk(1) .reader(reader()).listener(customReadItemListener) .processor(processor()).listener(customProcessListener) .writer(writer()).listener(customWriteListener) taskExecutor(taskExecutor()) .build(); }

All Exceptions related to logic within Reader , Processor and Writer being handled by overwriting onReadError, onProcessError and onWriteError respectively.通过分别覆盖 onReadError、onProcessError 和 onWriteError 处理与 Reader 、 Processor 和 Writer 中的逻辑相关的所有异常。 Inside of those methods I invoke my custom ExceptionService where I am handling exception and writing what I need to the log file.在这些方法中,我调用了我的自定义 ExceptionService,在那里我处理异常并将我需要的内容写入日志文件。

However, I came across issue that I can not figure out yet.但是,我遇到了我还无法弄清楚的问题。 In my writer I am reading properties file to get output file name.在我的作者中,我正在读取属性文件以获取输出文件名。 Part of my error handling test is setting file name to non existing file and try to handle File Not found exception.我的错误处理测试的一部分是将文件名设置为不存在的文件并尝试处理 File Not found 异常。 Yet, when I am running my test I am not able to catch the exception in my customWriteListener.然而,当我运行测试时,我无法在 customWriteListener 中捕获异常。 I have tried to add StepListener, ChunkListener, but neither one able to catch the exception and it is being throwing to the console.我试图添加 StepListener、ChunkListener,但没有一个能够捕获异常并且它被抛出到控制台。 I understand that this is a System type of error and should be handled by Framework.我知道这是系统类型的错误,应该由框架处理。 My question what is the best approach to wrap it the same way I am handling business type of exception , by cathcing it and calling my ExceptionService?我的问题是用与处理业务类型异常相同的方式包装它的最佳方法是什么,通过捕获它并调用我的 ExceptionService?

Without your complete code, I don't know exactly what the issue is, but a couple things you can check:没有你的完整代码,我不知道到底是什么问题,但你可以检查几件事:

  1. Do you have your listener declared as a component (@Component)?您是否将侦听器声明为组件(@Component)?
  2. Is your listener implementing ItemWriteListener, ItemProcessListener, and ItemReadListener?您的侦听器是否实现了 ItemWriteListener、ItemProcessListener 和 ItemReadListener?
  3. Do you have @OnWriteError, @OnReadError, and @OnProcessorError declared over the corresponding methods?你是否在相应的方法上声明了@OnWriteError、@OnReadError 和 @OnProcessorError?

If you have addressed those things and have the appropriate configuration in your main configuration file, this should work.如果您已经解决了这些问题并在主配置文件中进行了适当的配置,那么这应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM