2016-08-04 73 views
1

ファイルを読み込んでデータベースに書き込む最初のステップを持つSpringバッチジョブがあります。ファイルが存在しない場合、システムはx分待ってからy回まで試してみます。これどうやってするの?Springバッチ - 読み込むファイルが利用できません

詳細

ItemReaderは次のとおりです。fileが存在しない場合

@Bean 
    public ItemReader<Car> reader(LineMapper<Car> lineMapper, File file) { 
     FlatFileItemReader<Car> flatFileItemReader = new FlatFileItemReader<Car>(); 
     flatFileItemReader.setResource(new FileSystemResource(file)); 
     final int NUMBER_OF_HEADER_LINES = 1; 
     flatFileItemReader.setLinesToSkip(NUMBER_OF_HEADER_LINES); 
     flatFileItemReader.setLineMapper(lineMapper); 
     return flatFileItemReader; 
    } 

は現在、次の例外がスローされ、ジョブが失敗します。

2016-08-05 09:30:10.277 ERROR 7668 --- [   main] o.s.batch.core.step.AbstractStep   : Encountered an error executing step stepLoadFile in job testJob 

org.springframework.batch.item.ItemStreamException: Failed to initialize the reader 
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147) ~[spring-batch-infrastructure-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96) ~[spring-batch-infrastructure-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310) ~[spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197) ~[spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:392) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) [spring-core-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91] 
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91] 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) [spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) [spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) [spring-batch-core-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) [spring-aop-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at com.sun.proxy.$Proxy78.run(Unknown Source) [na:na] 
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:215) [spring-boot-autoconfigure-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:232) [spring-boot-autoconfigure-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:124) [spring-boot-autoconfigure-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:118) [spring-boot-autoconfigure-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:775) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:366) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1124) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1113) [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] 
    at com.myorg.LoadApplication.main(LoadApplication.java:15) [bin/:na] 
Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode): file [C:\test\TestData.csv] 
    at org.springframework.batch.item.file.FlatFileItemReader.doOpen(FlatFileItemReader.java:251) ~[spring-batch-infrastructure-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144) ~[spring-batch-infrastructure-3.0.6.RELEASE.jar:3.0.6.RELEASE] 
    ... 33 common frames omitted 

com.myorg.LoadApplicationコードは、バッチ・アプリケーションを起動し、次のとおりです。

@SpringBootApplication 
public class LoadApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(LoadApplication.class, args); 
    } 

} 

更新 dimitrisli答えを読んだ後、私は私の@Configurationクラス(注でこれを試してみました

:I構成クラスに@EnableRetryを追加しました)。

@Bean 
public ItemReader<Test> reader(LineMapper<Test> lineMapper, ApplicationProperties properties) { 
    FlatFileItemReader<Test> flatFileItemReader = new FlatFileItemReader<Test>() { 
     @Override 
     @Retryable(value = {ItemStreamException.class}, maxAttempts=5) 
     public void open(ExecutionContext executionContext) throws ItemStreamException { 
      super.open(executionContext); 
     } 

     @Override 
     @Retryable(maxAttempts=5) 
     public Holding read() throws UnexpectedInputException, ParseException, Exception { 
      return super.read(); 
     } 

    }; 
    flatFileItemReader.setResource(new FileSystemResource(properties.getLoadFile())); 
    flatFileItemReader.setLineMapper(lineMapper); 
    return flatFileItemReader; 
} 

ItemStreamExceptionがスローされ、アプリケーションは再試行せずに終了します。どのように再試行することができますか?

+0

Dimitrisliの答えをItemReader(上のアップデートに示されているコード)に適用することで、これを最終的に動作させることができました。アプリケーションは、spring-boot-starter-batchバージョン1.3.1.RELEASEのバグ(?)により、再試行せずに終了しました。 1.4.0.RELEASEに移動した後、再試行は期待どおりに行われました。私の元の質問に答えたようにdimitrisliの答えを正しいとマークしてください。 – James

答えて

1

Spring Batchからスタンドアロンスピンオフプロジェクト:5回を言う、あなたはから回復し、再試行したいと思うことを、潜在的にスローされた例外を想定するとは、ItemStreamExceptionであるあなたは、潜在的にこれを投げるあなたの方法を飾るために必要があります例外として:

+0

問題は、この例外が自分のコードからスローされていないことです。 Springバッチが例外をスローしています。 OPに完全なスタックトレースを追加しました。スタックトレースを見ると、 '@ Retryable'メインメソッド(Springブートバッチアプリケーションが実行される場所)を追加することができます。これはあなたのお勧めですか、このアノテーションを入れるためのより良い(よりターゲットを絞った)場所ですか? – James

0

spring-retryを使用してください。

RetryTemplateと組み合わせてTimeoutRetryPolicyを使用すると、必要な機能を書くことができます。

構成クラスに@EnableRetryを追加し、メソッドに@Retryableを追加する必要があります。

アプリケーションを起動する前にItemReaderをインスタンス化する必要がありますか?そうでない場合は、Beanを作成し、コンテキストをリフレッシュした後に別のメソッドを初期化する必要があります。 Spring Retryを使用することにより

+0

私はそのリンクを読んだことがありますが、私が説明したシナリオでそれをどのように使用/設定できるかは不明です。あなたはこれを達成する方法に関するコードサンプルを提供していただけますか? – James

関連する問題