Skip to content

Commit

Permalink
feat: update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Raouf25 committed Nov 19, 2023
1 parent 0ece3ee commit 92289f6
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 170 deletions.
23 changes: 3 additions & 20 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<version>2.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springbatch.excel</groupId>
Expand All @@ -14,8 +14,8 @@
<name>tutorial</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<poi.version>4.1.2</poi.version>
<java.version>20</java.version>
<poi.version>5.2.4</poi.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -58,22 +58,5 @@

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>


</project>
23 changes: 23 additions & 0 deletions run-database-mongo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

# Exécuter MongoDB Container
docker run -d -p 27017:27017 \
--name test-mongo \
-v mongo-data:/data/db \
-e MONGODB_INITDB_ROOT_USERNAME=admin \
-e MONGODB_INITDB_ROOT_PASSWORD=pass \
-e MONGODB_INITDB_DATABASE=spring_batch_excel_mongodb_db \
mongo:latest


# Exécuter MongoDB Express Container
docker run --link test-mongo:mongo \
-p 8222:8222 \
-e ME_CONFIG_MONGODB_URL="mongodb://mongo:27017" \
-e PORT=8222 \
mongo-express

# "Utilisateur MongoDB Express: admin"
# "Mot de passe MongoDB Express: pass"


Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.springbatch.excel.tutorial.batch.processors.EmployeeItemProcessor;
import com.springbatch.excel.tutorial.batch.validators.EmployeeJobParametersValidator;
import com.springbatch.excel.tutorial.domain.Employee;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersValidator;
import org.springframework.batch.core.Step;
Expand All @@ -27,17 +28,13 @@
*/
@EnableBatchProcessing
@Configuration
@RequiredArgsConstructor
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();
Expand All @@ -62,13 +59,16 @@ public ItemReader<Employee> itemReader() {

@Bean
public MongoItemWriter<Employee> writer(MongoTemplate mongoTemplate) {
return new MongoItemWriterBuilder<Employee>().template(mongoTemplate).collection("employee")
return new MongoItemWriterBuilder<Employee>()
.template(mongoTemplate)
.collection("employee")
.build();
}


/**
* step declaration
*
* @return {@link Step}
*/
@Bean
Expand All @@ -81,10 +81,9 @@ public Step employeeStep(MongoItemWriter<Employee> itemWriter) {
.build();
}



/**
* job declaration
*
* @param listener {@link JobCompletionListener}
* @return {@link Job}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import com.springbatch.excel.tutorial.batch.mappers.EmployeeItemRowMapper;
import com.springbatch.excel.tutorial.domain.Employee;
import com.springbatch.excel.tutorial.support.poi.AbstractExcelPoi;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.eval.NotImplementedException;
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;
Expand All @@ -15,10 +14,9 @@

import java.util.List;

@Slf4j
@Component
public class EmployeeItemReader extends AbstractExcelPoi<Employee> implements ItemReader<Employee> , StepExecutionListener {

private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeItemReader.class);
public class EmployeeItemReader extends AbstractExcelPoi<Employee> implements ItemReader<Employee>, StepExecutionListener {

private int employeeIndex = 0;

Expand All @@ -40,7 +38,7 @@ public Employee read() {
// read data in file
employeeList = read(path, new EmployeeItemRowMapper());

if(!employeeList.isEmpty()) {
if (!employeeList.isEmpty()) {

if (employeeIndex < employeeList.size()) {
employee = employeeList.get(employeeIndex);
Expand All @@ -50,14 +48,14 @@ public Employee read() {
}
}
} catch (Exception e) {
LOGGER.warn("Cannot read the excel file: {}", e.getMessage());
log.warn("Cannot read the excel file: {}", e.getMessage());
}

return employee;
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.springbatch.excel.tutorial.batch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
Expand All @@ -19,38 +19,33 @@
/**
* @author aek
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class EmployeeJobLauncher {

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

private final Job job;

private final JobLauncher jobLauncher;

@Value("${employee.excel.processingfolder}")
private String processingDir;

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

// run every 2 min
@Scheduled(fixedRate = 120000)
void launchFileToJob() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException, JobRestartException {
LOGGER.info("Starting job");
log.info("Starting job");
String excelFilePath = String.format("%s/employee.xlsx", processingDir);

JobParameters params = new JobParametersBuilder()
.addLong("jobId",System.currentTimeMillis())
.addDate("currentTime",new Date())
.addString("excelPath",excelFilePath)
.addLong("jobId", System.currentTimeMillis())
.addDate("currentTime", new Date())
.addString("excelPath", excelFilePath)
.toJobParameters();

jobLauncher.run(job, params);

LOGGER.info("Stopping job");
log.info("Stopping job");
}

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
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;

@Slf4j
@Component
public class JobCompletionListener extends JobExecutionListenerSupport {

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

@Override
public void afterJob(JobExecution jobExecution) {

String jobId = jobExecution.getJobParameters().getString("jobId");
String excelFilePath = jobExecution.getJobParameters().getString("excelPath");

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

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

LOGGER.info("==========JOB FINISHED=======");
LOGGER.info("JobId : {}",jobId);
LOGGER.info("excel Path : {}",excelFilePath);
LOGGER.info("Start Date: {}", start);
LOGGER.info("End Date: {}", end);
LOGGER.info("==============================");
log.info("==========JOB FINISHED=======");
log.info("JobId : {}", jobId);
log.info("excel Path : {}", excelFilePath);
log.info("Start Date: {}", start);
log.info("End Date: {}", end);
log.info("==============================");
}

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ public class EmployeeItemRowMapper extends CellFactory implements RowMapper<Empl

@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.setNumber((String) getCellValue(row.getCell(2)));
employee.setEmail((String) getCellValue(row.getCell(3)));
employee.setDepartment((String) getCellValue(row.getCell(4)));
employee.setSalary((Double) getCellValue(row.getCell(5)));

return employee;
return new Employee(null,
(String) getCellValue(row.getCell(0)),
(String) getCellValue(row.getCell(1)),
(String) getCellValue(row.getCell(2)),
(String) getCellValue(row.getCell(3)),
(String) getCellValue(row.getCell(4)),
(Double) getCellValue(row.getCell(5)));
}
}
Loading

0 comments on commit 92289f6

Please sign in to comment.