2016-04-03 12 views
1

ファイルをAmazon S3にアップロードするタスクレットがあります。今、私はAmazonClientExceptionがスローされるたびにタスクレットの実行を再試行したいと思います。私は@Retryable注釈を使って仕事をすると考えました。Springバッチ:@ Retetableと@EnableRetryアノテーションを使用したタスクレットの再試行

タスクレット:

@Component 
@StepScope 
@Retryable(value=AmazonClientException.class, stateful=true, [email protected](2000)) 
public class S3UploadTasklet extends ArgsSupport implements Tasklet { 

    @Autowired 
    private S3Client s3Client; 

    @Autowired 
    private S3Properties s3Properties; 

    private static final Logger LOGGER = LoggerFactory.getLogger(S3UploadTasklet.class); 

    private static final String FILE_EXTENSION = ".gpg"; 

    @Override 
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { 
     try { 
      String localFilename = getTempOutputFilename() + FILE_EXTENSION; 
      String s3Filename = s3Properties.getReportPath() + getS3OutputFilename() + FILE_EXTENSION; 
      File f = new File(localFilename); 
      if(f.exists()) { 
       LOGGER.info("Uploading " + localFilename + " to s3..."); 
       s3Client.upload(localFilename, s3Filename, s3Properties.getBucketName()); 
       LOGGER.info("Uploading done!"); 
      } else { 
       throw new RuntimeException("Encrypted file not found! Encryption process might have failed."); 
      } 
     } catch(AmazonClientException e) { 
      LOGGER.error("Problems uploading to S3. " + e.getMessage(), e); 
      throw e; 
     } catch(RuntimeException e) { 
      LOGGER.error("Runtime error occured. " + e.getMessage(), e); 
      throw e; 
     } 

     return RepeatStatus.FINISHED; 
    } 
} 

ジョブ構成:

@Configuration 
@EnableBatchProcessing 
@EnableRetry 
public class BatchConfiguration { 
    @Autowired 
    private JobBuilderFactory jobBuilderFactory; 

    @Autowired 
    private Step generateReport; 

    @Autowired 
    private Step encrypt; 

    @Autowired 
    private Step upload; 

    @Autowired 
    private Step cleanUp; 

    @Bean 
    @Transactional(value="appTransactionManager", isolation=Isolation.READ_COMMITTED) 
    public Job generateReconJob() { 
     return jobBuilderFactory.get("reconJob") 
       .incrementer(new RunIdIncrementer()) 
       .start(generateReport) 
        .on("COMPLETED").to(encrypt) 
       .from(generateReport) 
        .on("NOOP").end() 
       .from(generateReport) 
        .on("FAILED").to(cleanUp) 
       .from(encrypt) 
        .on("COMPLETED").to(upload) 
       .from(encrypt) 
        .on("FAILED").to(cleanUp) 
       .from(upload) 
        .on("*").to(cleanUp) 
       .from(cleanUp) 
        .on("*").end() 
       .end() 
       .build(); 
    } 
} 

しかし、行うことになっている何をしません。バッチジョブは、例外がスローされたときでもタスクレットを再試行しません。

どのような考えですか?

ここ@Retryable(値= AmazonClientException.class、真=ステートフル、バックオフ= @バックオフ(2000))のアノテーションを使用して、再試行したい方法、ではないにする必要がありますまた、

@Configuration 
public class ReportConfiguration { 
    ... 

    @Autowired 
    private S3UploadTasklet s3UploadTasklet; 

    ... 

    @Bean 
    public Step upload() { 
     return stepBuilderFactory.get("upload") 
       .tasklet(s3UploadTasklet) 
       .build(); 
    } 
} 
+0

再試行しないと言うと、例外がスローされたらどうなりますか? –

+0

@MichaelPralow例外がスローされていることがわかりますが、次のステップに進むだけです。 – makalshrek

+0

私は再試行注釈がタスクレット自体に置かれるべきではないと思います。むしろ、私はAmazonの呼び出しを自分のメソッドの中に置き、そこで再試行することができます。 – IcedDante

答えて

0

設定ですクラス。

+0

[再試行可能](http://docs.spring.io/spring-retry/docs/1.1.2.RELEASE/apidocs/org/springframework/retry/annotation/Retryable.html)annotation have '@Target(value = {METHOD、TYPE}) 'のように、メソッド/クラス/インタフェース上に置くことができます。 '@ Retryable'でクラスに注釈を付けることは、クラス内のすべてのメソッドに注釈を付けることと同じです。したがって、クラス内のすべてのメソッドに対して共通の再試行設定がある場合は、クラス/インタフェースに注釈を付けます。 'Retryable'はSpring AOPを使って実装されているので、ここではメソッドはpublicメソッドに適用されます。 –

関連する問題