如果我的平面文件的令牌数量不正确,则会低于Exception。
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)
如果我抓到了wrongnumberoftokens异常,我想打印自定义错误日志,文件名,行号和行导致异常。
帮助我。
谢谢。
答案 0 :(得分:0)
根据Spring Batch documentation,只需将其设置为可跳过的例外并注册SkipListener
(also 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);
}