Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,100 @@
package com.springbatch.excel.tutorial.batch;

import com.springbatch.excel.tutorial.batch.listeners.JobCompletionListener;
import com.springbatch.excel.tutorial.batch.processors.EmployeeItemProcessor;
import com.springbatch.excel.tutorial.batch.validators.EmployeeJobParametersValidator;
import com.springbatch.excel.tutorial.batch.writers.EmployeeItemWriter;
import com.springbatch.excel.tutorial.domain.Employee;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersValidator;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.CompositeJobParametersValidator;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

/**
* Configuration for batch
*/
@EnableBatchProcessing
@Configuration
public class BatchConfiguration {

public final JobBuilderFactory jobBuilderFactory;

public final StepBuilderFactory stepBuilderFactory;

public BatchConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
this.jobBuilderFactory = jobBuilderFactory;
this.stepBuilderFactory = stepBuilderFactory;
}

@Bean
public JobParametersValidator jobParametersValidator() {
return new EmployeeJobParametersValidator();
}

@Bean
public JobParametersValidator compositeJobParametersValidator() {
CompositeJobParametersValidator bean = new CompositeJobParametersValidator();
bean.setValidators(Collections.singletonList(jobParametersValidator()));
return bean;
}

@Bean
public ItemProcessor<Employee, Employee> itemProcessor() {
return new EmployeeItemProcessor();
}

@Bean
@StepScope
public ItemReader<Employee> itemReader(@Value("#{jobParameters[excelPath]}") String pathToFile) {
return new EmployeeItemReader(pathToFile);
}

@Bean
public ItemWriter<Employee> itemWriter() {
return new EmployeeItemWriter();
}

/**
* Declaration step
* @return {@link Step}
*/
@Bean
public Step employeeStep() {
return stepBuilderFactory.get("employeeStep")
.<Employee, Employee>chunk(1)
.reader(itemReader(null))
.processor(itemProcessor())
.writer(itemWriter())
.build();
}

/**
* Declaration job
* @param listener {@link JobCompletionListener}
* @return {@link Job}
*/
@Bean
public Job employeeJob(JobCompletionListener listener) {
return jobBuilderFactory.get("employeeJob")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(employeeStep())
.end()
.validator(compositeJobParametersValidator())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.springbatch.excel.tutorial.batch;

import com.springbatch.excel.tutorial.batch.mappers.EmployeeItemRowMapper;
import com.springbatch.excel.tutorial.domain.Employee;
import com.springbatch.excel.tutorial.support.poi.AbstractExcelPoi;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@StepScope
public class EmployeeItemReader extends AbstractExcelPoi<Employee> implements ItemReader<Employee> {

private int nextBeneficiaryIndex = 0;

private final String filePath;

public EmployeeItemReader(String filePath) {
super();
this.filePath = filePath;
}

/**
* {@inheritDoc}
*/
@Override
public Employee read() {

List<Employee> beneficiaryDossierDatas;
Employee nextBeneficiary = null;

// read data in file
beneficiaryDossierDatas = read(filePath, new EmployeeItemRowMapper());

if(!beneficiaryDossierDatas.isEmpty()) {

if (nextBeneficiaryIndex < beneficiaryDossierDatas.size()) {
nextBeneficiary = beneficiaryDossierDatas.get(nextBeneficiaryIndex);
nextBeneficiaryIndex++;
} else {
nextBeneficiaryIndex = 0;
}
}

return nextBeneficiary;
}

@Override
public void write(String filePath , List<Employee> aList) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.springbatch.excel.tutorial.batch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* @author aek
*/
@Component
public class EmployeeJobLauncher {

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

private final Job job;

private final JobLauncher jobLauncher;

@Value("${employee.excel.path}")
private String excelPath;

EmployeeJobLauncher(Job job, JobLauncher jobLauncher) {
this.job = job;
this.jobLauncher = jobLauncher;
}

@Scheduled(cron = "*/2 * * * *")
void launchFileToJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException, JobRestartException {
LOGGER.info("Starting job");

jobLauncher.run(job, jobParameters());

LOGGER.info("Stopping job");
}

private JobParameters jobParameters() {
Map<String, JobParameter> parameters = new HashMap<>();

parameters.put("currentTime", new JobParameter(new Date()));
parameters.put("excelPath", new JobParameter(excelPath));

return new JobParameters(parameters);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.springbatch.excel.tutorial.batch.listeners;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class JobCompletionListener extends JobExecutionListenerSupport {

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

public JobCompletionListener() {
}

@Override
public void afterJob(JobExecution jobExecution) {

String pathToExtractionFile = jobExecution.getJobParameters().getString("pathToFile");
String jobId = jobExecution.getJobParameters().getString("jobId");

// get job's start time
Date start = jobExecution.getCreateTime();
// get job's end time
Date end = jobExecution.getEndTime();

if(jobExecution.getStatus() == BatchStatus.COMPLETED) {

LOGGER.trace("===========================JOB FINISHED================================================");
LOGGER.trace("JobId : {}",jobId);
LOGGER.trace("Path file : {}", pathToExtractionFile);
LOGGER.trace("Date: {}", end);
LOGGER.trace("=======================================================================================");
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.springbatch.excel.tutorial.batch.mappers;

import com.springbatch.excel.tutorial.domain.Employee;
import com.springbatch.excel.tutorial.support.poi.CellFactory;
import com.springbatch.excel.tutorial.support.poi.RowMapper;
import org.apache.poi.ss.usermodel.Row;


public class EmployeeItemRowMapper extends CellFactory implements RowMapper<Employee> {

@Override
public Employee transformerRow(Row row) {
Employee employee = new Employee();

employee.setFirstName((String) getCellValue(row.getCell(0)));
employee.setLastName((String) getCellValue(row.getCell(1)));
employee.setEmail((String) getCellValue(row.getCell(2)));
employee.setDepartment((String) getCellValue(row.getCell(3)));
employee.setSalary((Double) getCellValue(row.getCell(4)));

return employee;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.springbatch.excel.tutorial.batch.processors;

import com.springbatch.excel.tutorial.domain.Employee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemProcessor;


/**
* @author Eric KOUAME
*/
public class EmployeeItemProcessor implements ItemProcessor<Employee, Employee>, StepExecutionListener {

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


public EmployeeItemProcessor() {
super();
}


/**
* {@inheritDoc}
*/
@Override
public Employee process(Employee item) throws Exception {

return null;
}

@Override
public void beforeStep(StepExecution stepExecution) {
/* Nothing to do before */
}

@Override
public ExitStatus afterStep(StepExecution stepExecution) {

return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.springbatch.excel.tutorial.batch.validators;

import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.JobParametersValidator;
import org.springframework.util.StringUtils;

public class EmployeeJobParametersValidator implements org.springframework.batch.core.JobParametersValidator {

@Override
public void validate(final JobParameters jobParameters) throws JobParametersInvalidException {
String fileName = jobParameters != null ? jobParameters.getString("excelPath") : null;

if (fileName !=null && !StringUtils.endsWithIgnoreCase(fileName, "xlsx")) {
throw new JobParametersInvalidException("The file type must be in xlsx format");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.springbatch.excel.tutorial.batch.writers;

import com.springbatch.excel.tutorial.domain.Employee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.item.ItemWriter;

import java.util.List;

public class EmployeeItemWriter implements ItemWriter<Employee>, StepExecutionListener {

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

public EmployeeItemWriter() {
super();
}

@Override
public void write(List<? extends Employee> items) throws Exception {

}

@Override
public void beforeStep(StepExecution stepExecution) {
/* Nothing to do before */
}

@Override
public ExitStatus afterStep(StepExecution stepExecution) {

if(stepExecution.getStatus() == BatchStatus.COMPLETED) {

}
return null;
}

}
Loading