FabricantDistributeurBatchService.java

  1. package com.sintia.ffl.admin.optique.catalogue.batch.batchservice;

  2. import lombok.RequiredArgsConstructor;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.batch.core.BatchStatus;
  7. import org.springframework.batch.core.Job;
  8. import org.springframework.batch.core.JobExecution;
  9. import org.springframework.batch.core.JobParameters;
  10. import org.springframework.batch.core.JobParametersBuilder;
  11. import org.springframework.batch.core.JobParametersInvalidException;
  12. import org.springframework.batch.core.launch.support.SimpleJobLauncher;
  13. import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
  14. import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
  15. import org.springframework.batch.core.repository.JobRepository;
  16. import org.springframework.batch.core.repository.JobRestartException;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.core.task.SimpleAsyncTaskExecutor;
  20. import org.springframework.core.task.TaskExecutor;
  21. import org.springframework.stereotype.Service;

  22. import java.util.Date;

  23. @Service
  24. @RequiredArgsConstructor
  25. @Slf4j
  26. public class FabricantDistributeurBatchService {
  27.    
  28.     private final Job importFabricantDistributeur;
  29.    
  30.     private final JobRepository jobRepository;

  31.     private final TaskExecutor asyncExecutor;
  32.    
  33.     @Value("${fabricantdistributeur.filename}")
  34.     private String makerProviderfileName;
  35.    
  36.     @Value("${local.resources.directory}")
  37.     private String resourcesDirectory;
  38.    
  39.     private static final Logger LOGGER = LoggerFactory.getLogger(FabricantDistributeurBatchService.class);
  40.    
  41.     public BatchStatus importFabricantDistributeur() throws InterruptedException {
  42.         LOGGER.info("Launching maker/provider creation job manually");
  43.         Date scriptRunningDate = new Date();
  44.         SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
  45.         simpleJobLauncher.setTaskExecutor(asyncExecutor);
  46.         simpleJobLauncher.setJobRepository(jobRepository);
  47.        
  48.         JobParameters jobParameters = new JobParametersBuilder()
  49.                 .addLong("time", System.currentTimeMillis())
  50.                 .addString("fileName", makerProviderfileName)
  51. //              .addString("localDirectory", Paths.get(resourcesDirectory).toString())
  52.                 .addDate("scriptDate", scriptRunningDate)
  53.                 .toJobParameters();
  54.        
  55.         try {
  56.             JobExecution status = simpleJobLauncher.run(importFabricantDistributeur, jobParameters);
  57.             return status.getStatus();
  58.         } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
  59.                 | JobParametersInvalidException e) {
  60.             LOGGER.error("Error executing job 'importFabricantDistributeur'", e); return BatchStatus.FAILED;
  61.         } finally {
  62.             LOGGER.info("Batch started");
  63.         }
  64.     }
  65.    
  66. }