From 87dea6017afdbb5974a85050e0dfd6bb59e425a7 Mon Sep 17 00:00:00 2001 From: kimjihun Date: Mon, 5 Aug 2024 18:38:25 +0900 Subject: [PATCH] =?UTF-8?q?*=20fifth=20Batch=20=EC=83=81=ED=83=9C=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=20=EA=B8=88=EC=A7=80=20close=20#6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/springbatch/batch/ExcelRowWriter.java | 14 +++++++------- .../com/example/springbatch/batch/FifthBatch.java | 7 ++++++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java b/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java index 2499e3f..8d5e363 100644 --- a/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java +++ b/src/main/java/com/example/springbatch/batch/ExcelRowWriter.java @@ -6,6 +6,7 @@ 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.ExecutionContext; import org.springframework.batch.item.ItemStreamException; import org.springframework.batch.item.ItemStreamWriter; @@ -17,16 +18,16 @@ public class ExcelRowWriter implements ItemStreamWriter { private final String filePath; private Workbook workbook; private Sheet sheet; - private int rowIndex = 0; + private int currentRowNumber; public ExcelRowWriter(String filePath) throws IOException { this.filePath = filePath; - initialize(); + currentRowNumber = 0; } - private void initialize() throws ItemStreamException { - + @Override + public void open(ExecutionContext executionContext) throws ItemStreamException { workbook = new XSSFWorkbook(); sheet = workbook.createSheet("Sheet1"); } @@ -34,7 +35,7 @@ public class ExcelRowWriter implements ItemStreamWriter { @Override public void write(Chunk chunk) { for (BeforeEntity entity : chunk) { - Row row = sheet.createRow(rowIndex++); + Row row = sheet.createRow(currentRowNumber++); row.createCell(0).setCellValue(entity.getUsername()); } } @@ -49,10 +50,9 @@ public class ExcelRowWriter implements ItemStreamWriter { } finally { try { workbook.close(); - rowIndex = 0; } catch (IOException e) { throw new ItemStreamException(e); } } } -} +} \ No newline at end of file diff --git a/src/main/java/com/example/springbatch/batch/FifthBatch.java b/src/main/java/com/example/springbatch/batch/FifthBatch.java index 90c6d4e..286b293 100644 --- a/src/main/java/com/example/springbatch/batch/FifthBatch.java +++ b/src/main/java/com/example/springbatch/batch/FifthBatch.java @@ -58,13 +58,18 @@ public class FifthBatch { @Bean public RepositoryItemReader fifthBeforeReader() { - return new RepositoryItemReaderBuilder() + RepositoryItemReader reader = new RepositoryItemReaderBuilder() .name("beforeReader") .pageSize(10) .methodName("findAll") .repository(beforeRepository) .sorts(Map.of("id", Sort.Direction.ASC)) .build(); + + // 전체 데이터 셋에서 어디까지 수행 했는지의 값을 저장하지 않음 + reader.setSaveState(false); + + return reader; } @Bean