IseaBatchService.java
package com.sintia.ffl.admin.optique.specifique.isea.service;
import com.sintia.ffl.adminui.commons.dto.BatchInfoDTO;
import com.sintia.ffl.adminui.commons.dto.BatchInfoIseaDTO;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.*;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
@Slf4j
public class IseaBatchService {
public static final String BATCH_STARTED = "Batch started";
@Autowired
private Job chargementCatalogue;
@Autowired
private Job desactivationVerres;
@Autowired
private Job purgeVerres;
@Autowired
private JobRepository jobRepository;
@Autowired
private final JobExplorer jobExplorer;
@Value("${local.resources.directory.isea}")
private String resourcesDirectory;
private static final Logger LOGGER = LoggerFactory.getLogger(IseaBatchService.class);
public IseaBatchService(JobExplorer jobExplorer) {
this.jobExplorer = jobExplorer;
}
public BatchInfoDTO chargementCatalogue(String userName,String fileName) {
LOGGER.info("Launching chargement catalogue job manually");
Date scriptRunningDate = new Date();
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
simpleJobLauncher.setJobRepository(jobRepository);
JobParameters jobParameters = new JobParametersBuilder()
.addString("userName", userName)
.addString("fileName", fileName)
.addDate("scriptDate", scriptRunningDate)
.toJobParameters();
try {
JobExecution status = simpleJobLauncher.run(chargementCatalogue, jobParameters);
return new BatchInfoDTO(status.getCreateTime(), status.getStatus());
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
| JobParametersInvalidException e) {
LOGGER.error("Error executing job chargementCatalogue", e); return new BatchInfoDTO(scriptRunningDate, BatchStatus.FAILED);
} finally {
LOGGER.info(BATCH_STARTED);
}
}
public BatchInfoDTO desactivationVerres(String userName) {
LOGGER.info("Launching desactivation verres job manually");
Date scriptRunningDate = new Date();
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
simpleJobLauncher.setJobRepository(jobRepository);
JobParameters jobParameters = new JobParametersBuilder()
.addString("userName", userName)
.addDate("scriptDate", scriptRunningDate)
.toJobParameters();
try {
JobExecution status = simpleJobLauncher.run(desactivationVerres, jobParameters);
return new BatchInfoDTO(status.getCreateTime(), status.getStatus());
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
| JobParametersInvalidException e) {
LOGGER.error("Error executing job 'desactivationVerres'", e); return new BatchInfoDTO(scriptRunningDate, BatchStatus.FAILED);
} finally {
LOGGER.info(BATCH_STARTED);
}
}
public BatchInfoDTO purgeVerres(String userName) {
LOGGER.info("Launching purge verres job manually");
Date scriptRunningDate = new Date();
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
simpleJobLauncher.setJobRepository(jobRepository);
JobParameters jobParameters = new JobParametersBuilder()
.addString("userName", userName)
.addDate("scriptDate", scriptRunningDate)
.toJobParameters();
try {
JobExecution status = simpleJobLauncher.run(purgeVerres, jobParameters);
return new BatchInfoDTO(status.getCreateTime(), status.getStatus());
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
| JobParametersInvalidException e) {
LOGGER.error("Error executing job 'purgeVerres'", e); return new BatchInfoDTO(scriptRunningDate, BatchStatus.FAILED);
} finally {
LOGGER.info(BATCH_STARTED);
}
}
public List<BatchInfoIseaDTO> recuperationInfoBatchIsea() {
LOGGER.info("Récupération historique batch Isea");
List<BatchInfoIseaDTO> batchInfoIseaList = new LinkedList<>();
batchInfoIseaList = recuperationInfoJob("chargementCatalogue",batchInfoIseaList);
batchInfoIseaList = recuperationInfoJob("desactivationVerres",batchInfoIseaList);
batchInfoIseaList = recuperationInfoJob("purgeVerres",batchInfoIseaList);
batchInfoIseaList.sort(Comparator.comparing(BatchInfoIseaDTO::getStartDate).reversed());
return batchInfoIseaList;
}
public List<BatchInfoIseaDTO> recuperationInfoJob(String jobName, List<BatchInfoIseaDTO> listBatchInfo){
int count = 0;
try {
count = jobExplorer.getJobInstanceCount(jobName);
} catch (NoSuchJobException e) {
LOGGER.info("Pas de JOB {}", jobName);
}
List<JobInstance> jobInstanceDesactivationList = jobExplorer.getJobInstances(jobName,0,count);
for (int i = 0; i < count; i++) {
JobExecution job = jobExplorer.getJobExecution(jobInstanceDesactivationList.get(i).getInstanceId());
BatchInfoIseaDTO batch = new BatchInfoIseaDTO();
batch.setAction(job.getJobInstance().getJobName());
batch.setUserName(job.getJobParameters().getString("userName"));
batch.setStartDate(job.getStartTime());
batch.setBatchStatus(job.getStatus());
batch.setNomFichier(job.getJobParameters().getString("fileName"));
batch.setIdJob(jobInstanceDesactivationList.get(i).getInstanceId());
listBatchInfo.add(batch);
}
return listBatchInfo;
}
public String getNomFichierCR(long jobInstanceId){
return jobExplorer.getJobExecution(jobInstanceId).getExecutionContext().getString("NomFichierCR","");
}
public String getCheminFichierCR(){
return resourcesDirectory;
}
}