简体   繁体   English

Spring批处理中的验证步骤

[英]Validation step in Spring batch

I'm working with Spring batch for the first time and I need some help about validation step.我第一次使用 Spring 批处理,我需要一些有关验证步骤的帮助。

Before realy starting my batch Job, I need some validation on the file to be handled like :在真正开始我的批处理作业之前,我需要对要处理的文件进行一些验证,例如:

  • Check the name with information from database使用数据库中的信息检查名称
  • Check first and last line (wich are specific) with information from system and database使用来自系统和数据库的信息检查第一行和最后一行(这是特定的)
  • Check total line number with a data in the first line用第一行的数据检查总行号

After that I can really start my batch job.之后我就可以真正开始我的批处理工作了。

I have in mind multiple step chaining each others, the initial step doing the validation, if the file is not valid the process go in error step.我想到了相互链接的多个步骤,最初的步骤是进行验证,如果文件无效,则该过程将进入错误步骤。

How can I doing this validation?我该如何进行此验证? All reader found give me only one line of the file, I have to create my own reader?找到的所有阅读器只给我一行文件,我必须创建自己的阅读器吗? Which approach will you take to manage this behaviour?您将采用哪种方法来管理这种行为?

For first point use a JobExecutionDecider ;首先,使用JobExecutionDecider if test is not passed end your job with error(s).如果测试未通过,则以错误结束您的工作。 For remaining points you can approach the problem writing your own reader or对于剩余的点,您可以编写自己的阅读器或

  • for first line use FlatFileItemReader.setLinesToSkip() and FlatFileItemReader.setSkippedLinesCallback()第一行使用FlatFileItemReader.setLinesToSkip()FlatFileItemReader.setSkippedLinesCallback()
  • use a PeekableItemReader in order to check if you are on last line and perform different validation in your processor phase.使用PeekableItemReader来检查您是否在最后一行并在处理器阶段执行不同的验证。

This kind of approach could requires you to write different domain objects for first line,last line and other lines.这种方法可能需要您为第一行、最后一行和其他行编写不同的域对象。

You can use single step and make a listener and you can validate whatever you want in beforeStep or you can have two steps and first step is for validation and return appropriate return status in afterstep method of StepExecutionListener .您可以使用单步并制作一个侦听器,您可以在 beforeStep 中验证您想要的任何内容,或者您​​可以有两个步骤,第一步是验证并在 StepExecutionListener 的 afterstep 方法中返回适当的返回状态。

In short- use listeners to validate , refer a very nice example here简而言之-使用侦听器进行验证,请 在此处参考一个非常好的示例

To validate input file, you can refer this example要验证输入文件,您可以参考此示例

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

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