2016-04-03 22 views
1

Amazon S3'e bir dosya yükleyen bu görevin var. Şimdi, bir AmazonClientException atıldığında görev yürütme işlemini yeniden denemek istiyorum. İşi yapacak @Retryable notunu kullanarak düşündüm.Yay Batch: @Retryable ve @EnableRetry ek açıklamasını kullanarak bir görevlendirmeyi yeniden deneme

Tasklet:

@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; 
    } 
} 

Meslek yapılandırma:

@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(); 
    } 
} 

Ancak, bunu yapmak için ne gerekiyor yapmaz. Toplu iş, istisna atıldığında hala görev yapmayı tekrar denemez.

Herhangi bir düşünce? İşte

da

@Configuration 
public class ReportConfiguration { 
    ... 

    @Autowired 
    private S3UploadTasklet s3UploadTasklet; 

    ... 

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

Yeniden denemediğini söylediğinizde, istisna atıldığında ne olur? –

+0

@MichaelPralow İstisnaın atıldığını görüyorum ancak daha sonra bir sonraki adıma geçiyor. – makalshrek

+0

Yeniden deneme ekinin Görev Kümesinin kendisinde yer alması gerektiğine inanmıyorum. Daha ziyade, aramayı kendi yöntemiyle Amazon'a koyarım ve orada geri ödemeyi kullanabilirim. – IcedDante

cevap

0

@Retryable yapılandırma var (= durum bilgisi gerçek değeri = AmazonClientException.class, geri çekilme = Backoff (2000) @) ek açıklama denemek istiyor yöntemle değil olmalıdır sınıf.

+0

[Geri Ödeme] (http://docs.spring.io/spring-retry/docs/1.1.2.RELEASE/apidocs/org/springframework/retry/annotation/Retryable.html) ek açıklamanın '@Target (value = {YÖNTEM, TİP}) ', böylece bir yöntem/sınıf/arayüz üzerinde olabilir. '@ Retryable' ile bir sınıfa not eklenmesi, sınıftaki tüm yöntemleri ekleyerek aynıdır. Yani, sınıfınızdaki tüm yöntemler için ortak bir yeniden deneme yapılandırmanız varsa, sınıfı/arabirime açıklama ekleyin. Ve burada yöntem, "Geri alınabilir" Spring AOP kullanılarak uygulandığından, kamu yöntemlerine uygulanır. –

İlgili konular