Skip to content

Commit e5a450a

Browse files
author
aek
committed
insert data in mongo db
1 parent 6521a3a commit e5a450a

File tree

14 files changed

+96
-127
lines changed

14 files changed

+96
-127
lines changed

src/main/java/com/springbatch/excel/tutorial/TutorialApplication.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
6+
import org.springframework.scheduling.annotation.EnableScheduling;
57

6-
@SpringBootApplication
8+
@EnableScheduling
9+
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
710
public class TutorialApplication {
811

912
public static void main(String[] args) {

src/main/java/com/springbatch/excel/tutorial/batch/BatchConfiguration.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@
33
import com.springbatch.excel.tutorial.batch.listeners.JobCompletionListener;
44
import com.springbatch.excel.tutorial.batch.processors.EmployeeItemProcessor;
55
import com.springbatch.excel.tutorial.batch.validators.EmployeeJobParametersValidator;
6-
import com.springbatch.excel.tutorial.batch.writers.EmployeeItemWriter;
76
import com.springbatch.excel.tutorial.domain.Employee;
87
import org.springframework.batch.core.Job;
98
import org.springframework.batch.core.JobParametersValidator;
109
import org.springframework.batch.core.Step;
1110
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
1211
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
1312
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
14-
import org.springframework.batch.core.configuration.annotation.StepScope;
1513
import org.springframework.batch.core.job.CompositeJobParametersValidator;
1614
import org.springframework.batch.core.launch.support.RunIdIncrementer;
1715
import org.springframework.batch.item.ItemProcessor;
1816
import org.springframework.batch.item.ItemReader;
19-
import org.springframework.batch.item.ItemWriter;
20-
import org.springframework.beans.factory.annotation.Value;
17+
import org.springframework.batch.item.data.MongoItemWriter;
18+
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder;
2119
import org.springframework.context.annotation.Bean;
2220
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.data.mongodb.core.MongoTemplate;
2322

2423
import java.util.Collections;
2524

@@ -57,41 +56,44 @@ public ItemProcessor<Employee, Employee> itemProcessor() {
5756
}
5857

5958
@Bean
60-
@StepScope
61-
public ItemReader<Employee> itemReader(@Value("#{jobParameters[excelPath]}") String pathToFile) {
62-
return new EmployeeItemReader(pathToFile);
59+
public ItemReader<Employee> itemReader() {
60+
return new EmployeeItemReader();
6361
}
6462

6563
@Bean
66-
public ItemWriter<Employee> itemWriter() {
67-
return new EmployeeItemWriter();
64+
public MongoItemWriter<Employee> writer(MongoTemplate mongoTemplate) {
65+
return new MongoItemWriterBuilder<Employee>().template(mongoTemplate).collection("employee")
66+
.build();
6867
}
6968

69+
7070
/**
7171
* Declaration step
7272
* @return {@link Step}
7373
*/
7474
@Bean
75-
public Step employeeStep() {
75+
public Step employeeStep(MongoItemWriter<Employee> itemWriter) {
7676
return stepBuilderFactory.get("employeeStep")
77-
.<Employee, Employee>chunk(1)
78-
.reader(itemReader(null))
77+
.<Employee, Employee>chunk(50)
78+
.reader(itemReader())
7979
.processor(itemProcessor())
80-
.writer(itemWriter())
80+
.writer(itemWriter)
8181
.build();
8282
}
8383

84+
85+
8486
/**
8587
* Declaration job
8688
* @param listener {@link JobCompletionListener}
8789
* @return {@link Job}
8890
*/
8991
@Bean
90-
public Job employeeJob(JobCompletionListener listener) {
92+
public Job employeeJob(JobCompletionListener listener, Step employeeStep) {
9193
return jobBuilderFactory.get("employeeJob")
9294
.incrementer(new RunIdIncrementer())
9395
.listener(listener)
94-
.flow(employeeStep())
96+
.flow(employeeStep)
9597
.end()
9698
.validator(compositeJobParametersValidator())
9799
.build();

src/main/java/com/springbatch/excel/tutorial/batch/EmployeeItemReader.java

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,71 @@
33
import com.springbatch.excel.tutorial.batch.mappers.EmployeeItemRowMapper;
44
import com.springbatch.excel.tutorial.domain.Employee;
55
import com.springbatch.excel.tutorial.support.poi.AbstractExcelPoi;
6-
import org.springframework.batch.core.configuration.annotation.StepScope;
6+
import org.apache.poi.ss.formula.eval.NotImplementedException;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.batch.core.ExitStatus;
10+
import org.springframework.batch.core.StepExecution;
11+
import org.springframework.batch.core.StepExecutionListener;
712
import org.springframework.batch.item.ItemReader;
13+
import org.springframework.core.io.ClassPathResource;
814
import org.springframework.stereotype.Component;
915

1016
import java.util.List;
1117

1218
@Component
13-
@StepScope
14-
public class EmployeeItemReader extends AbstractExcelPoi<Employee> implements ItemReader<Employee> {
19+
public class EmployeeItemReader extends AbstractExcelPoi<Employee> implements ItemReader<Employee> , StepExecutionListener {
1520

16-
private int nextBeneficiaryIndex = 0;
21+
private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeItemReader.class);
1722

18-
private final String filePath;
23+
private int employeeIndex = 0;
1924

20-
public EmployeeItemReader(String filePath) {
21-
super();
22-
this.filePath = filePath;
23-
}
25+
private String excelFilePath;
2426

2527
/**
2628
* {@inheritDoc}
2729
*/
2830
@Override
2931
public Employee read() {
3032

31-
List<Employee> beneficiaryDossierDatas;
32-
Employee nextBeneficiary = null;
33+
List<Employee> employeeList;
34+
Employee employee = null;
35+
36+
try {
3337

34-
// read data in file
35-
beneficiaryDossierDatas = read(filePath, new EmployeeItemRowMapper());
38+
String path = new ClassPathResource(excelFilePath).getFile().getPath();
3639

37-
if(!beneficiaryDossierDatas.isEmpty()) {
40+
// read data in file
41+
employeeList = read(path, new EmployeeItemRowMapper());
3842

39-
if (nextBeneficiaryIndex < beneficiaryDossierDatas.size()) {
40-
nextBeneficiary = beneficiaryDossierDatas.get(nextBeneficiaryIndex);
41-
nextBeneficiaryIndex++;
42-
} else {
43-
nextBeneficiaryIndex = 0;
43+
if(!employeeList.isEmpty()) {
44+
45+
if (employeeIndex < employeeList.size()) {
46+
employee = employeeList.get(employeeIndex);
47+
employeeIndex++;
48+
} else {
49+
employeeIndex = 0;
50+
}
4451
}
52+
} catch (Exception e) {
53+
LOGGER.warn("Cannot read the excel file: {}", e.getMessage());
4554
}
4655

47-
return nextBeneficiary;
56+
return employee;
4857
}
4958

5059
@Override
5160
public void write(String filePath , List<Employee> aList) {
61+
throw new NotImplementedException("No need to implement this method in the context");
62+
}
5263

64+
@Override
65+
public void beforeStep(StepExecution stepExecution) {
66+
excelFilePath = stepExecution.getJobParameters().getString("excelPath");
67+
}
68+
69+
@Override
70+
public ExitStatus afterStep(StepExecution stepExecution) {
71+
return null;
5372
}
5473
}

src/main/java/com/springbatch/excel/tutorial/batch/EmployeeJobLauncher.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
55
import org.springframework.batch.core.Job;
6-
import org.springframework.batch.core.JobParameter;
76
import org.springframework.batch.core.JobParameters;
7+
import org.springframework.batch.core.JobParametersBuilder;
88
import org.springframework.batch.core.JobParametersInvalidException;
99
import org.springframework.batch.core.launch.JobLauncher;
1010
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
@@ -15,8 +15,6 @@
1515
import org.springframework.stereotype.Component;
1616

1717
import java.util.Date;
18-
import java.util.HashMap;
19-
import java.util.Map;
2018

2119
/**
2220
* @author aek
@@ -31,29 +29,27 @@ public class EmployeeJobLauncher {
3129
private final JobLauncher jobLauncher;
3230

3331
@Value("${employee.excel.path}")
34-
private String excelPath;
32+
private String excelFilePath;
3533

3634
EmployeeJobLauncher(Job job, JobLauncher jobLauncher) {
3735
this.job = job;
3836
this.jobLauncher = jobLauncher;
3937
}
4038

41-
@Scheduled(cron = "*/2 * * * *")
39+
//@Scheduled(cron = "*/2 * * * *")
40+
@Scheduled(fixedRate = 120000)
4241
void launchFileToJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException, JobRestartException {
4342
LOGGER.info("Starting job");
4443

45-
jobLauncher.run(job, jobParameters());
44+
JobParameters params = new JobParametersBuilder()
45+
.addLong("jobId",System.currentTimeMillis())
46+
.addDate("currentTime",new Date())
47+
.addString("excelPath",excelFilePath)
48+
.toJobParameters();
4649

47-
LOGGER.info("Stopping job");
48-
}
49-
50-
private JobParameters jobParameters() {
51-
Map<String, JobParameter> parameters = new HashMap<>();
50+
jobLauncher.run(job, params);
5251

53-
parameters.put("currentTime", new JobParameter(new Date()));
54-
parameters.put("excelPath", new JobParameter(excelPath));
55-
56-
return new JobParameters(parameters);
52+
LOGGER.info("Stopping job");
5753
}
5854

5955
}

src/main/java/com/springbatch/excel/tutorial/batch/listeners/JobCompletionListener.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ public class JobCompletionListener extends JobExecutionListenerSupport {
1414

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

17-
public JobCompletionListener() {
18-
}
19-
2017
@Override
2118
public void afterJob(JobExecution jobExecution) {
2219

23-
String pathToExtractionFile = jobExecution.getJobParameters().getString("pathToFile");
2420
String jobId = jobExecution.getJobParameters().getString("jobId");
2521

2622
// get job's start time
@@ -30,11 +26,11 @@ public void afterJob(JobExecution jobExecution) {
3026

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

33-
LOGGER.trace("===========================JOB FINISHED================================================");
34-
LOGGER.trace("JobId : {}",jobId);
35-
LOGGER.trace("Path file : {}", pathToExtractionFile);
36-
LOGGER.trace("Date: {}", end);
37-
LOGGER.trace("=======================================================================================");
29+
LOGGER.info("===========================JOB FINISHED================================================");
30+
LOGGER.info("JobId : {}",jobId);
31+
LOGGER.info("Start Date: {}", start);
32+
LOGGER.info("End Date: {}", end);
33+
LOGGER.info("=======================================================================================");
3834
}
3935

4036
}

src/main/java/com/springbatch/excel/tutorial/batch/mappers/EmployeeItemRowMapper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public Employee transformerRow(Row row) {
1414

1515
employee.setFirstName((String) getCellValue(row.getCell(0)));
1616
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)));
17+
employee.setNumber((String) getCellValue(row.getCell(2)));
18+
employee.setEmail((String) getCellValue(row.getCell(3)));
19+
employee.setDepartment((String) getCellValue(row.getCell(4)));
20+
employee.setSalary((Double) getCellValue(row.getCell(5)));
2021

2122
return employee;
2223
}

src/main/java/com/springbatch/excel/tutorial/batch/processors/EmployeeItemProcessor.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
import com.springbatch.excel.tutorial.domain.Employee;
44
import org.slf4j.Logger;
55
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;
96
import org.springframework.batch.item.ItemProcessor;
107

118

129
/**
1310
* @author Eric KOUAME
1411
*/
15-
public class EmployeeItemProcessor implements ItemProcessor<Employee, Employee>, StepExecutionListener {
12+
public class EmployeeItemProcessor implements ItemProcessor<Employee, Employee> {
1613

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

@@ -21,25 +18,14 @@ public EmployeeItemProcessor() {
2118
super();
2219
}
2320

24-
2521
/**
2622
* {@inheritDoc}
2723
*/
2824
@Override
29-
public Employee process(Employee item) throws Exception {
30-
31-
return null;
32-
}
25+
public Employee process(Employee item) {
3326

34-
@Override
35-
public void beforeStep(StepExecution stepExecution) {
36-
/* Nothing to do before */
27+
return item;
3728
}
3829

39-
@Override
40-
public ExitStatus afterStep(StepExecution stepExecution) {
41-
42-
return null;
43-
}
4430

4531
}

src/main/java/com/springbatch/excel/tutorial/batch/validators/EmployeeJobParametersValidator.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.springframework.batch.core.JobParameters;
44
import org.springframework.batch.core.JobParametersInvalidException;
5-
import org.springframework.batch.core.JobParametersValidator;
65
import org.springframework.util.StringUtils;
76

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

src/main/java/com/springbatch/excel/tutorial/batch/writers/EmployeeItemWriter.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)