2016-08-09 9 views
2

で使用することができます。ジョブを実行しているとき(すなわちloadFileは春のリトライが、私は<code>ItemReader</code>を以下している春のバッチFlatFileItemReader

import org.springframework.batch.item.ExecutionContext; 
import org.springframework.batch.item.ItemStreamException; 
import org.springframework.batch.item.file.FlatFileItemReader; 
import org.springframework.batch.item.file.LineMapper; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.core.io.FileSystemResource; 
import org.springframework.retry.annotation.Retryable; 
import org.springframework.stereotype.Service; 

@Service 
public class MyReader extends FlatFileItemReader<Holding> { 

    @Autowired 
    public MyReader(LineMapper<Holding> lineMapper, File loadFile) { 
     setResource(new FileSystemResource(loadFile)); 
     final int NUMBER_OF_HEADER_LINES = 1; 
     setLinesToSkip(NUMBER_OF_HEADER_LINES); 
     setLineMapper(lineMapper); 
    } 

    @Override 
    @Retryable(value=ItemStreamException.class, maxAttempts=5, [email protected](delay=1800000)) 
    public void open(ExecutionContext executionContext) throws ItemStreamException { 
       super.open(executionContext); 
    } 
} 

ファイルを読み取ることが使用できない場合もあります。ファイルが利用できない場合は、30分ほどスリープ状態にしてからファイルを開こうとします。 5回の試行後にファイルが見つからない場合は、ItemStreamExceptionを投げて通常どおり失敗する可能性があります。

残念ながら、上記のコードではファイルの開封は再試行されません。オープンする最初の呼び出しではItemStreamExceptionがスローされ、オープンは再試行されません。

誰かがこれを行う方法を説明してもらえますか?注:SpringBootApplicationクラスには@EnableRetryがあります。

答えて

0

Springブートバージョン1.3.1.RELEASEから1.4.0.RELEASE(および対応する自動バージョン管理依存関係(spring-boot-starter-batchなど)への移行)が問題を解決しました。再試行は、OPに実装されているように1.4.0.RELEASEで動作します。 1.3.1.RELEASEでは動作しません。

buildscript { 
    ext { 
     // Previously using 1.3.1.RELEASE where retry functionality does not work 
     springBootVersion = '1.4.0.RELEASE' 
    } 
    repositories { 
     mavenCentral() 
     mavenLocal() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'eclipse' 
apply plugin: 'spring-boot' 

configurations { 
    provided 
} 

sourceSets { 
    main { 
     compileClasspath += configurations.provided 
    } 
} 

jar { 
    baseName = 'load' 
    version = '1.0' 
} 
sourceCompatibility = 1.8 
targetCompatibility = 1.8 

repositories { 
    mavenCentral() 
    mavenLocal() 
} 

dependencies { 
    compile('org.springframework.boot:spring-boot-starter-batch') 
    compile('org.springframework.boot:spring-boot-configuration-processor') 
    compile('org.springframework.boot:spring-boot-starter-data-jpa') 
    compile('org.springframework.boot:spring-boot-starter-mail') 
    compile('org.springframework.boot:spring-boot-starter-aop') 
    compile('org.projectlombok:lombok:1.16.6') 
    compile('org.hibernate:hibernate-validator:5.2.4.Final') 
    compile('org.quartz-scheduler:quartz:2.2.3') 
    runtime('javax.el:javax.el-api:2.2.4') 
    runtime('org.glassfish.web:javax.el:2.2.4') 
    runtime('net.sourceforge.jtds:jtds:1.3.1') 
    testCompile('org.springframework.boot:spring-boot-starter-test') 
    testCompile('org.springframework.batch:spring-batch-test') 
} 



eclipse { 
    classpath { 
     containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER') 
     containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8' 
    } 
} 

task wrapper(type: Wrapper) { 
    gradleVersion = '2.11' 
} 

注:考慮FlatFileItemReaderを使用してステップを再試行するJobExecutionDeciderを使用して今ここに使用されているのGradleファイルです。しかし、ItemStreamExceptionは、デシリアンが実行する機会を得ることなく、ジョブとアプリケーション全体を終了させます。

0

これは動作します。私はあなたのクラスを知らないので、少し変更を加えました。

build.gradle

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath(
       "org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE", 
     ) 
    } 
} 

apply plugin: 'java' 
apply plugin: 'spring-boot' 

tasks.withType(JavaCompile) { 
    sourceCompatibility = JavaVersion.VERSION_1_8 
    targetCompatibility = JavaVersion.VERSION_1_8 
} 

repositories { 
    mavenCentral() 
} 

springBoot { 
    mainClass = "test.MyReader" 
} 

dependencies { 
    compile(
      'org.springframework.boot:spring-boot-starter', 
      'org.springframework.boot:spring-boot-starter-aop', 
      'org.springframework.retry:spring-retry', 
    ) 
} 

MyApplication.java

@EnableRetry 
@SpringBootApplication 
public class MyApplication implements CommandLineRunner { 
    private final MyReader myReader; 

    @Autowired 
    public MyApplication(MyReader myReader) { 
     this.myReader = myReader; 
    } 

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

    @Override 
    public void run(String... args) throws Exception { 
     myReader.read(); 
    } 
} 

MyReader.java

@Service 
public class MyReader { 
    private static final String PATH = "a2"; 
    private final Logger logger = LoggerFactory.getLogger(MyReader.class); 

    public MyReader() { 

    } 

    @Retryable(value = IOException.class, maxAttempts = 5, backoff = @Backoff(delay = 5000)) 
    public void read() throws IOException { 
     final Resource resource = new FileSystemResource(PATH); 
     logger.info("\n\nRead attempt: {}\n", resource); 
     try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) { 
      final String content = reader.lines().collect(Collectors.joining(" ")); 
      logger.info("\n\nFile content: {}\n", content); 
     } 
    } 
} 

ときexeファイルキュートでファイルがある場合、ログには「読み込み試行」メッセージと「ファイル内容」メッセージが1つずつ表示されます。私はそこに空の行を追加したので、今見落とすのは難しいです。

ファイルが存在しない場合、5つの「読み込み試行」メッセージが表示され、次に例外がスローされます。

リトライ時間を5秒に変更しました。十分な速さであれば、ファイルなしで起動してそこにファイルを作成することができ、それが動作することがわかります。いくつかの読み込み試行と最後にファイルの内容が表示されます。

私はあなたがこの問題で既に数日間苦労しているのを見ることができます。彼らがコミュニティを助けていないので、不必要な質問を出さないでください。将来の参考として、あなたの1つの質問に固執し、必要に応じて修正してください。

+0

私は、Spring Batchの 'FlatFileItemReader'の外で動作するようにSpring Retryを取得することもできます。 OPで、私はSpring Batchの 'FlatFileItemReader'内でこれを動作させる方法を尋ねています。 – James

関連する問題