diff --git a/src/main/java/com/example/springbatch/batch/ExcelRowReader.java b/src/main/java/com/example/springbatch/batch/ExcelRowReader.java index d7b817d..eb5b347 100644 --- a/src/main/java/com/example/springbatch/batch/ExcelRowReader.java +++ b/src/main/java/com/example/springbatch/batch/ExcelRowReader.java @@ -4,34 +4,42 @@ 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.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.IOException; import java.util.Iterator; -public class ExcelRowReader implements ItemReader { +public class ExcelRowReader implements ItemStreamReader { private final String filePath; + private FileInputStream fileInputStream; + private Workbook workbook; private Iterator rowCursor; + public ExcelRowReader(String filePath) throws IOException { this.filePath = filePath; - initialize(); } - private void initialize() throws IOException { + @Override + public void open(ExecutionContext executionContext) throws ItemStreamException { - FileInputStream fileInputStream = new FileInputStream(filePath); - Workbook workbook = WorkbookFactory.create(fileInputStream); - Sheet sheet = workbook.getSheetAt(0); - this.rowCursor = sheet.iterator(); + try { + fileInputStream = new FileInputStream(filePath); + workbook = WorkbookFactory.create(fileInputStream); + Sheet sheet = workbook.getSheetAt(0); + this.rowCursor = sheet.iterator(); - if (rowCursor.hasNext()) { - rowCursor.next(); + if (rowCursor.hasNext()) { + rowCursor.next(); + } + } catch (IOException e) { + throw new ItemStreamException(e); } } @@ -44,4 +52,19 @@ public class ExcelRowReader implements ItemReader { 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); + } + } } diff --git a/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java b/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java new file mode 100644 index 0000000..f2c7e51 --- /dev/null +++ b/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java @@ -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 { + + 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 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); + } + } + } +} diff --git a/src/main/java/com/example/springbatch/batch/FifthBatch.java b/src/main/java/com/example/springbatch/batch/FifthBatch.java new file mode 100644 index 0000000..a66083a --- /dev/null +++ b/src/main/java/com/example/springbatch/batch/FifthBatch.java @@ -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) + . chunk(10, platformTransactionManager) + .reader(fifthBeforeReader()) + .processor(fifthProcessor()) + .writer(excelWriter()) + .build(); + } + + @Bean + public RepositoryItemReader fifthBeforeReader() { + + return new RepositoryItemReaderBuilder() + .name("beforeReader") + .pageSize(10) + .methodName("findAll") + .repository(beforeRepository) + .sorts(Map.of("id", Sort.Direction.ASC)) + .build(); + } + + @Bean + public ItemProcessor fifthProcessor() { + + return item -> item; + } + + @Bean + public ItemStreamWriter excelWriter() { + + try { + return new ExcelRowWriter("C:\\Users\\kim\\Desktop\\result.xlsx"); + //리눅스나 맥은 /User/형태로 + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} diff --git a/src/main/java/com/example/springbatch/batch/FourthBatch.java b/src/main/java/com/example/springbatch/batch/FourthBatch.java index dba2d6f..46cbc98 100644 --- a/src/main/java/com/example/springbatch/batch/FourthBatch.java +++ b/src/main/java/com/example/springbatch/batch/FourthBatch.java @@ -9,7 +9,7 @@ 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.ItemReader; +import org.springframework.batch.item.ItemStreamReader; import org.springframework.batch.item.data.RepositoryItemWriter; import org.springframework.batch.item.data.builder.RepositoryItemWriterBuilder; import org.springframework.context.annotation.Bean; @@ -53,7 +53,7 @@ public class FourthBatch { } @Bean - public ItemReader excelReader() { + public ItemStreamReader excelReader() { try { return new ExcelRowReader("C:\\Users\\kim\\Desktop\\yummi.xlsx"); @@ -87,4 +87,4 @@ public class FourthBatch { .methodName("save") .build(); } -} +} \ No newline at end of file diff --git a/src/main/java/com/example/springbatch/controller/MainController.java b/src/main/java/com/example/springbatch/controller/MainController.java index 6a86404..495b06d 100644 --- a/src/main/java/com/example/springbatch/controller/MainController.java +++ b/src/main/java/com/example/springbatch/controller/MainController.java @@ -61,5 +61,17 @@ public class MainController { 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 }