* 엑셀 배치 처리 완료 close #3
This commit is contained in:
@@ -4,34 +4,42 @@ import org.apache.poi.ss.usermodel.Row;
|
|||||||
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||||
import org.springframework.batch.item.ItemReader;
|
import org.springframework.batch.item.ExecutionContext;
|
||||||
|
import org.springframework.batch.item.ItemStreamException;
|
||||||
|
import org.springframework.batch.item.ItemStreamReader;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
public class ExcelRowReader implements ItemReader<Row> {
|
public class ExcelRowReader implements ItemStreamReader<Row> {
|
||||||
|
|
||||||
private final String filePath;
|
private final String filePath;
|
||||||
|
private FileInputStream fileInputStream;
|
||||||
|
private Workbook workbook;
|
||||||
private Iterator<Row> rowCursor;
|
private Iterator<Row> rowCursor;
|
||||||
|
|
||||||
|
|
||||||
public ExcelRowReader(String filePath) throws IOException {
|
public ExcelRowReader(String filePath) throws IOException {
|
||||||
|
|
||||||
this.filePath = filePath;
|
this.filePath = filePath;
|
||||||
initialize();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initialize() throws IOException {
|
@Override
|
||||||
|
public void open(ExecutionContext executionContext) throws ItemStreamException {
|
||||||
|
|
||||||
FileInputStream fileInputStream = new FileInputStream(filePath);
|
try {
|
||||||
Workbook workbook = WorkbookFactory.create(fileInputStream);
|
fileInputStream = new FileInputStream(filePath);
|
||||||
Sheet sheet = workbook.getSheetAt(0);
|
workbook = WorkbookFactory.create(fileInputStream);
|
||||||
this.rowCursor = sheet.iterator();
|
Sheet sheet = workbook.getSheetAt(0);
|
||||||
|
this.rowCursor = sheet.iterator();
|
||||||
|
|
||||||
|
|
||||||
if (rowCursor.hasNext()) {
|
if (rowCursor.hasNext()) {
|
||||||
rowCursor.next();
|
rowCursor.next();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ItemStreamException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,4 +52,19 @@ public class ExcelRowReader implements ItemReader<Row> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws ItemStreamException {
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (workbook != null) {
|
||||||
|
workbook.close();
|
||||||
|
}
|
||||||
|
if (fileInputStream != null) {
|
||||||
|
fileInputStream.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ItemStreamException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.example.springbatch.batch;
|
||||||
|
|
||||||
|
import com.example.springbatch.entity.BeforeEntity;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
import org.springframework.batch.item.Chunk;
|
||||||
|
import org.springframework.batch.item.ItemStreamException;
|
||||||
|
import org.springframework.batch.item.ItemStreamWriter;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ExcelRowWriter implements ItemStreamWriter<BeforeEntity> {
|
||||||
|
|
||||||
|
private final String filePath;
|
||||||
|
private Workbook workbook;
|
||||||
|
private Sheet sheet;
|
||||||
|
private int rowIndex = 0;
|
||||||
|
|
||||||
|
public ExcelRowWriter(String filePath) throws IOException {
|
||||||
|
|
||||||
|
this.filePath = filePath;
|
||||||
|
initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initialize() throws ItemStreamException {
|
||||||
|
|
||||||
|
workbook = new XSSFWorkbook();
|
||||||
|
sheet = workbook.createSheet("Sheet1");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(Chunk<? extends BeforeEntity> chunk) throws Exception {
|
||||||
|
for (BeforeEntity entity : chunk) {
|
||||||
|
Row row = sheet.createRow(rowIndex++);
|
||||||
|
row.createCell(0).setCellValue(entity.getUsername());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws ItemStreamException {
|
||||||
|
|
||||||
|
try (FileOutputStream fileOut = new FileOutputStream(filePath)) {
|
||||||
|
workbook.write(fileOut);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ItemStreamException(e);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
workbook.close();
|
||||||
|
rowIndex = 0;
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ItemStreamException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
86
src/main/java/com/example/springbatch/batch/FifthBatch.java
Normal file
86
src/main/java/com/example/springbatch/batch/FifthBatch.java
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
package com.example.springbatch.batch;
|
||||||
|
|
||||||
|
import com.example.springbatch.entity.BeforeEntity;
|
||||||
|
import com.example.springbatch.repository.BeforeRepository;
|
||||||
|
import org.springframework.batch.core.Job;
|
||||||
|
import org.springframework.batch.core.Step;
|
||||||
|
import org.springframework.batch.core.job.builder.JobBuilder;
|
||||||
|
import org.springframework.batch.core.repository.JobRepository;
|
||||||
|
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||||
|
import org.springframework.batch.item.ItemProcessor;
|
||||||
|
import org.springframework.batch.item.ItemStreamWriter;
|
||||||
|
import org.springframework.batch.item.data.RepositoryItemReader;
|
||||||
|
import org.springframework.batch.item.data.builder.RepositoryItemReaderBuilder;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class FifthBatch {
|
||||||
|
|
||||||
|
private final JobRepository jobRepository;
|
||||||
|
private final PlatformTransactionManager platformTransactionManager;
|
||||||
|
private final BeforeRepository beforeRepository;
|
||||||
|
|
||||||
|
public FifthBatch(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager, BeforeRepository beforeRepository) {
|
||||||
|
this.jobRepository = jobRepository;
|
||||||
|
this.platformTransactionManager = platformTransactionManager;
|
||||||
|
this.beforeRepository = beforeRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Job fifthJob() {
|
||||||
|
|
||||||
|
System.out.println("fifth job");
|
||||||
|
|
||||||
|
return new JobBuilder("fifthJob", jobRepository)
|
||||||
|
.start(fifthStep())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Step fifthStep() {
|
||||||
|
|
||||||
|
System.out.println("fifth step");
|
||||||
|
|
||||||
|
return new StepBuilder("fifthStep", jobRepository)
|
||||||
|
.<BeforeEntity, BeforeEntity> chunk(10, platformTransactionManager)
|
||||||
|
.reader(fifthBeforeReader())
|
||||||
|
.processor(fifthProcessor())
|
||||||
|
.writer(excelWriter())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RepositoryItemReader<BeforeEntity> fifthBeforeReader() {
|
||||||
|
|
||||||
|
return new RepositoryItemReaderBuilder<BeforeEntity>()
|
||||||
|
.name("beforeReader")
|
||||||
|
.pageSize(10)
|
||||||
|
.methodName("findAll")
|
||||||
|
.repository(beforeRepository)
|
||||||
|
.sorts(Map.of("id", Sort.Direction.ASC))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ItemProcessor<BeforeEntity, BeforeEntity> fifthProcessor() {
|
||||||
|
|
||||||
|
return item -> item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ItemStreamWriter<BeforeEntity> excelWriter() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
return new ExcelRowWriter("C:\\Users\\kim\\Desktop\\result.xlsx");
|
||||||
|
//리눅스나 맥은 /User/형태로
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,7 +9,7 @@ import org.springframework.batch.core.job.builder.JobBuilder;
|
|||||||
import org.springframework.batch.core.repository.JobRepository;
|
import org.springframework.batch.core.repository.JobRepository;
|
||||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
import org.springframework.batch.core.step.builder.StepBuilder;
|
||||||
import org.springframework.batch.item.ItemProcessor;
|
import org.springframework.batch.item.ItemProcessor;
|
||||||
import org.springframework.batch.item.ItemReader;
|
import org.springframework.batch.item.ItemStreamReader;
|
||||||
import org.springframework.batch.item.data.RepositoryItemWriter;
|
import org.springframework.batch.item.data.RepositoryItemWriter;
|
||||||
import org.springframework.batch.item.data.builder.RepositoryItemWriterBuilder;
|
import org.springframework.batch.item.data.builder.RepositoryItemWriterBuilder;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@@ -53,7 +53,7 @@ public class FourthBatch {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ItemReader<Row> excelReader() {
|
public ItemStreamReader<Row> excelReader() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return new ExcelRowReader("C:\\Users\\kim\\Desktop\\yummi.xlsx");
|
return new ExcelRowReader("C:\\Users\\kim\\Desktop\\yummi.xlsx");
|
||||||
|
|||||||
@@ -61,5 +61,17 @@ public class MainController {
|
|||||||
return "ok";
|
return "ok";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/fifth")
|
||||||
|
private String fifthApi(@RequestParam("value") String value) throws Exception {
|
||||||
|
|
||||||
|
JobParameters jobParameters = new JobParametersBuilder()
|
||||||
|
.addString("date", value)
|
||||||
|
.toJobParameters();
|
||||||
|
|
||||||
|
jobLauncher.run(jobRegistry.getJob("fifthJob"), jobParameters);
|
||||||
|
|
||||||
|
return "ok";
|
||||||
|
}
|
||||||
|
|
||||||
//https://docs.spring.io/spring-batch/reference/job/configuring-launcher.html
|
//https://docs.spring.io/spring-batch/reference/job/configuring-launcher.html
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user