Skip to content

Commit 6521a3a

Browse files
author
aek
committed
base architecture
1 parent fa01cdf commit 6521a3a

18 files changed

+399
-6
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,100 @@
11
package com.springbatch.excel.tutorial.batch;
22

3+
import com.springbatch.excel.tutorial.batch.listeners.JobCompletionListener;
4+
import com.springbatch.excel.tutorial.batch.processors.EmployeeItemProcessor;
5+
import com.springbatch.excel.tutorial.batch.validators.EmployeeJobParametersValidator;
6+
import com.springbatch.excel.tutorial.batch.writers.EmployeeItemWriter;
7+
import com.springbatch.excel.tutorial.domain.Employee;
8+
import org.springframework.batch.core.Job;
9+
import org.springframework.batch.core.JobParametersValidator;
10+
import org.springframework.batch.core.Step;
11+
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
12+
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
13+
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
14+
import org.springframework.batch.core.configuration.annotation.StepScope;
15+
import org.springframework.batch.core.job.CompositeJobParametersValidator;
16+
import org.springframework.batch.core.launch.support.RunIdIncrementer;
17+
import org.springframework.batch.item.ItemProcessor;
18+
import org.springframework.batch.item.ItemReader;
19+
import org.springframework.batch.item.ItemWriter;
20+
import org.springframework.beans.factory.annotation.Value;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
24+
import java.util.Collections;
25+
26+
/**
27+
* Configuration for batch
28+
*/
29+
@EnableBatchProcessing
30+
@Configuration
331
public class BatchConfiguration {
32+
33+
public final JobBuilderFactory jobBuilderFactory;
34+
35+
public final StepBuilderFactory stepBuilderFactory;
36+
37+
public BatchConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
38+
this.jobBuilderFactory = jobBuilderFactory;
39+
this.stepBuilderFactory = stepBuilderFactory;
40+
}
41+
42+
@Bean
43+
public JobParametersValidator jobParametersValidator() {
44+
return new EmployeeJobParametersValidator();
45+
}
46+
47+
@Bean
48+
public JobParametersValidator compositeJobParametersValidator() {
49+
CompositeJobParametersValidator bean = new CompositeJobParametersValidator();
50+
bean.setValidators(Collections.singletonList(jobParametersValidator()));
51+
return bean;
52+
}
53+
54+
@Bean
55+
public ItemProcessor<Employee, Employee> itemProcessor() {
56+
return new EmployeeItemProcessor();
57+
}
58+
59+
@Bean
60+
@StepScope
61+
public ItemReader<Employee> itemReader(@Value("#{jobParameters[excelPath]}") String pathToFile) {
62+
return new EmployeeItemReader(pathToFile);
63+
}
64+
65+
@Bean
66+
public ItemWriter<Employee> itemWriter() {
67+
return new EmployeeItemWriter();
68+
}
69+
70+
/**
71+
* Declaration step
72+
* @return {@link Step}
73+
*/
74+
@Bean
75+
public Step employeeStep() {
76+
return stepBuilderFactory.get("employeeStep")
77+
.<Employee, Employee>chunk(1)
78+
.reader(itemReader(null))
79+
.processor(itemProcessor())
80+
.writer(itemWriter())
81+
.build();
82+
}
83+
84+
/**
85+
* Declaration job
86+
* @param listener {@link JobCompletionListener}
87+
* @return {@link Job}
88+
*/
89+
@Bean
90+
public Job employeeJob(JobCompletionListener listener) {
91+
return jobBuilderFactory.get("employeeJob")
92+
.incrementer(new RunIdIncrementer())
93+
.listener(listener)
94+
.flow(employeeStep())
95+
.end()
96+
.validator(compositeJobParametersValidator())
97+
.build();
98+
}
99+
4100
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.springbatch.excel.tutorial.batch;
2+
3+
import com.springbatch.excel.tutorial.batch.mappers.EmployeeItemRowMapper;
4+
import com.springbatch.excel.tutorial.domain.Employee;
5+
import com.springbatch.excel.tutorial.support.poi.AbstractExcelPoi;
6+
import org.springframework.batch.core.configuration.annotation.StepScope;
7+
import org.springframework.batch.item.ItemReader;
8+
import org.springframework.stereotype.Component;
9+
10+
import java.util.List;
11+
12+
@Component
13+
@StepScope
14+
public class EmployeeItemReader extends AbstractExcelPoi<Employee> implements ItemReader<Employee> {
15+
16+
private int nextBeneficiaryIndex = 0;
17+
18+
private final String filePath;
19+
20+
public EmployeeItemReader(String filePath) {
21+
super();
22+
this.filePath = filePath;
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
@Override
29+
public Employee read() {
30+
31+
List<Employee> beneficiaryDossierDatas;
32+
Employee nextBeneficiary = null;
33+
34+
// read data in file
35+
beneficiaryDossierDatas = read(filePath, new EmployeeItemRowMapper());
36+
37+
if(!beneficiaryDossierDatas.isEmpty()) {
38+
39+
if (nextBeneficiaryIndex < beneficiaryDossierDatas.size()) {
40+
nextBeneficiary = beneficiaryDossierDatas.get(nextBeneficiaryIndex);
41+
nextBeneficiaryIndex++;
42+
} else {
43+
nextBeneficiaryIndex = 0;
44+
}
45+
}
46+
47+
return nextBeneficiary;
48+
}
49+
50+
@Override
51+
public void write(String filePath , List<Employee> aList) {
52+
53+
}
54+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.springbatch.excel.tutorial.batch;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.batch.core.Job;
6+
import org.springframework.batch.core.JobParameter;
7+
import org.springframework.batch.core.JobParameters;
8+
import org.springframework.batch.core.JobParametersInvalidException;
9+
import org.springframework.batch.core.launch.JobLauncher;
10+
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
11+
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
12+
import org.springframework.batch.core.repository.JobRestartException;
13+
import org.springframework.beans.factory.annotation.Value;
14+
import org.springframework.scheduling.annotation.Scheduled;
15+
import org.springframework.stereotype.Component;
16+
17+
import java.util.Date;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
/**
22+
* @author aek
23+
*/
24+
@Component
25+
public class EmployeeJobLauncher {
26+
27+
private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeJobLauncher.class);
28+
29+
private final Job job;
30+
31+
private final JobLauncher jobLauncher;
32+
33+
@Value("${employee.excel.path}")
34+
private String excelPath;
35+
36+
EmployeeJobLauncher(Job job, JobLauncher jobLauncher) {
37+
this.job = job;
38+
this.jobLauncher = jobLauncher;
39+
}
40+
41+
@Scheduled(cron = "*/2 * * * *")
42+
void launchFileToJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException, JobRestartException {
43+
LOGGER.info("Starting job");
44+
45+
jobLauncher.run(job, jobParameters());
46+
47+
LOGGER.info("Stopping job");
48+
}
49+
50+
private JobParameters jobParameters() {
51+
Map<String, JobParameter> parameters = new HashMap<>();
52+
53+
parameters.put("currentTime", new JobParameter(new Date()));
54+
parameters.put("excelPath", new JobParameter(excelPath));
55+
56+
return new JobParameters(parameters);
57+
}
58+
59+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.springbatch.excel.tutorial.batch.listeners;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.batch.core.BatchStatus;
6+
import org.springframework.batch.core.JobExecution;
7+
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
8+
import org.springframework.stereotype.Component;
9+
10+
import java.util.Date;
11+
12+
@Component
13+
public class JobCompletionListener extends JobExecutionListenerSupport {
14+
15+
private static final Logger LOGGER = LoggerFactory.getLogger(JobCompletionListener.class);
16+
17+
public JobCompletionListener() {
18+
}
19+
20+
@Override
21+
public void afterJob(JobExecution jobExecution) {
22+
23+
String pathToExtractionFile = jobExecution.getJobParameters().getString("pathToFile");
24+
String jobId = jobExecution.getJobParameters().getString("jobId");
25+
26+
// get job's start time
27+
Date start = jobExecution.getCreateTime();
28+
// get job's end time
29+
Date end = jobExecution.getEndTime();
30+
31+
if(jobExecution.getStatus() == BatchStatus.COMPLETED) {
32+
33+
LOGGER.trace("===========================JOB FINISHED================================================");
34+
LOGGER.trace("JobId : {}",jobId);
35+
LOGGER.trace("Path file : {}", pathToExtractionFile);
36+
LOGGER.trace("Date: {}", end);
37+
LOGGER.trace("=======================================================================================");
38+
}
39+
40+
}
41+
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.springbatch.excel.tutorial.batch.mappers;
2+
3+
import com.springbatch.excel.tutorial.domain.Employee;
4+
import com.springbatch.excel.tutorial.support.poi.CellFactory;
5+
import com.springbatch.excel.tutorial.support.poi.RowMapper;
6+
import org.apache.poi.ss.usermodel.Row;
7+
8+
9+
public class EmployeeItemRowMapper extends CellFactory implements RowMapper<Employee> {
10+
11+
@Override
12+
public Employee transformerRow(Row row) {
13+
Employee employee = new Employee();
14+
15+
employee.setFirstName((String) getCellValue(row.getCell(0)));
16+
employee.setLastName((String) getCellValue(row.getCell(1)));
17+
employee.setEmail((String) getCellValue(row.getCell(2)));
18+
employee.setDepartment((String) getCellValue(row.getCell(3)));
19+
employee.setSalary((Double) getCellValue(row.getCell(4)));
20+
21+
return employee;
22+
}
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.springbatch.excel.tutorial.batch.processors;
2+
3+
import com.springbatch.excel.tutorial.domain.Employee;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.batch.core.ExitStatus;
7+
import org.springframework.batch.core.StepExecution;
8+
import org.springframework.batch.core.StepExecutionListener;
9+
import org.springframework.batch.item.ItemProcessor;
10+
11+
12+
/**
13+
* @author Eric KOUAME
14+
*/
15+
public class EmployeeItemProcessor implements ItemProcessor<Employee, Employee>, StepExecutionListener {
16+
17+
private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeItemProcessor.class);
18+
19+
20+
public EmployeeItemProcessor() {
21+
super();
22+
}
23+
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
@Override
29+
public Employee process(Employee item) throws Exception {
30+
31+
return null;
32+
}
33+
34+
@Override
35+
public void beforeStep(StepExecution stepExecution) {
36+
/* Nothing to do before */
37+
}
38+
39+
@Override
40+
public ExitStatus afterStep(StepExecution stepExecution) {
41+
42+
return null;
43+
}
44+
45+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.springbatch.excel.tutorial.batch.validators;
2+
3+
import org.springframework.batch.core.JobParameters;
4+
import org.springframework.batch.core.JobParametersInvalidException;
5+
import org.springframework.batch.core.JobParametersValidator;
6+
import org.springframework.util.StringUtils;
7+
8+
public class EmployeeJobParametersValidator implements org.springframework.batch.core.JobParametersValidator {
9+
10+
@Override
11+
public void validate(final JobParameters jobParameters) throws JobParametersInvalidException {
12+
String fileName = jobParameters != null ? jobParameters.getString("excelPath") : null;
13+
14+
if (fileName !=null && !StringUtils.endsWithIgnoreCase(fileName, "xlsx")) {
15+
throw new JobParametersInvalidException("The file type must be in xlsx format");
16+
}
17+
}
18+
19+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.springbatch.excel.tutorial.batch.writers;
2+
3+
import com.springbatch.excel.tutorial.domain.Employee;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.batch.core.BatchStatus;
7+
import org.springframework.batch.core.ExitStatus;
8+
import org.springframework.batch.core.StepExecution;
9+
import org.springframework.batch.core.StepExecutionListener;
10+
import org.springframework.batch.item.ItemWriter;
11+
12+
import java.util.List;
13+
14+
public class EmployeeItemWriter implements ItemWriter<Employee>, StepExecutionListener {
15+
16+
private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeItemWriter.class);
17+
18+
public EmployeeItemWriter() {
19+
super();
20+
}
21+
22+
@Override
23+
public void write(List<? extends Employee> items) throws Exception {
24+
25+
}
26+
27+
@Override
28+
public void beforeStep(StepExecution stepExecution) {
29+
/* Nothing to do before */
30+
}
31+
32+
@Override
33+
public ExitStatus afterStep(StepExecution stepExecution) {
34+
35+
if(stepExecution.getStatus() == BatchStatus.COMPLETED) {
36+
37+
}
38+
return null;
39+
}
40+
41+
}

0 commit comments

Comments
 (0)