FabricantDistributeurBatchService.java
package com.sintia.ffl.admin.optique.catalogue.batch.batchservice;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
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.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service
@RequiredArgsConstructor
@Slf4j
public class FabricantDistributeurBatchService {
private final Job importFabricantDistributeur;
private final JobRepository jobRepository;
private final TaskExecutor asyncExecutor;
@Value("${fabricantdistributeur.filename}")
private String makerProviderfileName;
@Value("${local.resources.directory}")
private String resourcesDirectory;
private static final Logger LOGGER = LoggerFactory.getLogger(FabricantDistributeurBatchService.class);
public BatchStatus importFabricantDistributeur() throws InterruptedException {
LOGGER.info("Launching maker/provider creation job manually");
Date scriptRunningDate = new Date();
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setTaskExecutor(asyncExecutor);
simpleJobLauncher.setJobRepository(jobRepository);
JobParameters jobParameters = new JobParametersBuilder()
.addLong("time", System.currentTimeMillis())
.addString("fileName", makerProviderfileName)
// .addString("localDirectory", Paths.get(resourcesDirectory).toString())
.addDate("scriptDate", scriptRunningDate)
.toJobParameters();
try {
JobExecution status = simpleJobLauncher.run(importFabricantDistributeur, jobParameters);
return status.getStatus();
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
| JobParametersInvalidException e) {
LOGGER.error("Error executing job 'importFabricantDistributeur'", e); return BatchStatus.FAILED;
} finally {
LOGGER.info("Batch started");
}
}
}