2017-05-25 155 views
0

如果我的平面文件的標記數不正確,我會得到下面的異常。Spring批處理FlatFileItemReader獲得iput線

2017-05-25 11:58:31,337 ERROR org.springframework.batch.item.file.FlatFileItemReader (FlatFileItemReader.java:199)- Parsing error at line: 7 in resource=file [C:\flatfiles\temp\input.JOCCH], input=[710,01,67000,C,20170328,20170327,TRAN1,HF1,,,,] 
org.springframework.batch.item.file.transform.IncorrectTokenCountException: Incorrect number of tokens found in record: expected 11 actual 12 
    at org.springframework.batch.item.file.transform.AbstractLineTokenizer.tokenize(AbstractLineTokenizer.java:90) 
    at org.springframework.batch.item.file.transform.PatternMatchingCompositeLineTokenizer.tokenize(PatternMatchingCompositeLineTokenizer.java:49) 
    at org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper.mapLine(PatternMatchingCompositeLineMapper.java:61) 
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:196) 
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:87) 
    at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:90) 
    at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:68) 
    at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:106) 
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368) 
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:214) 
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:143) 
    at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:103) 
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:68) 
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271) 
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:76) 
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368) 
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:214) 
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:143) 
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:242) 
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:198) 
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:348) 
    at org.springframework.batch.core.job.flow.FlowJob.access$100(FlowJob.java:43) 
    at org.springframework.batch.core.job.flow.FlowJob$JobFlowExecutor.executeStep(FlowJob.java:135) 
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60) 
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:144) 
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:124) 
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:103) 
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:250) 
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:110) 
    at java.lang.Thread.run(Thread.java:745) 

如果我抓住incorrectnumberoftokens例外,我想打印自定義錯誤日誌文件名,行號和行造成的異常。

幫幫我。

謝謝。

回答

0

根據Spring Batch documentation,只需將其設置爲可跳過的例外並註冊SkipListeneralso well documented)。

實施例的上下文配置:

<step id="step1" xmlns="http://www.springframework.org/schema/batch"> 
    <tasklet> 
     <chunk reader="flatFileItemReader" writer="itemWriter" commit-interval="10" skip-limit="10"> 
      <skippable-exception-classes> 
       <include class="org.springframework.batch.item.file.transform.IncorrectTokenCountException"/> 
      </skippable-exception-classes> 
     </chunk> 
     <listeners> 
      <listener ref="loggingSkipListener" /> 
     </listeners> 
    </tasklet> 
</step> 

<bean id="loggingSkipListener" class="com.example.MyLoggingSkipListener" /> 

SkipListener接口來實現:

public interface SkipListener<T,S> extends StepListener { 

    void onSkipInRead(Throwable t); 
    void onSkipInProcess(T item, Throwable t); 
    void onSkipInWrite(S item, Throwable t); 

}