diff --git a/src/main/java/com/example/springbatch/batch/ExcelRowReader.java b/src/main/java/com/example/springbatch/batch/ExcelRowReader.java new file mode 100644 index 0000000..e94e26c --- /dev/null +++ b/src/main/java/com/example/springbatch/batch/ExcelRowReader.java @@ -0,0 +1,45 @@ +package com.example.springbatch.batch; + +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 java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Iterator; + +public class ExcelRowReader implements ItemReader { + + private final String filePath; + private Iterator rowCursor; + + public ExcelRowReader(String filePath) throws IOException { + + this.filePath = filePath; + initialize(); + } + + private void initialize() throws IOException { + + FileInputStream fileInputStream = new FileInputStream(filePath); + Workbook workbook = WorkbookFactory.create(fileInputStream); + Sheet sheet = workbook.getSheetAt(0); + this.rowCursor = sheet.iterator(); + // Skip header row if necessary + if (rowCursor.hasNext()) { + rowCursor.next(); + } + } + + @Override + public Row read() { + if (rowCursor != null && rowCursor.hasNext()) { + return rowCursor.next(); + } else { + return null; // No more rows to read + } + } +} diff --git a/src/main/java/com/example/springbatch/batch/FourthBatch.java b/src/main/java/com/example/springbatch/batch/FourthBatch.java index 5268a35..dba2d6f 100644 --- a/src/main/java/com/example/springbatch/batch/FourthBatch.java +++ b/src/main/java/com/example/springbatch/batch/FourthBatch.java @@ -47,8 +47,8 @@ public class FourthBatch { return new StepBuilder("fourthStep", jobRepository) . chunk(10, platformTransactionManager) .reader(excelReader()) - .processor(middleProcessor()) - .writer(afterWriter()) + .processor(fourthProcessor()) + .writer(fourthAfterWriter()) .build(); } @@ -56,19 +56,20 @@ public class FourthBatch { public ItemReader excelReader() { try { - return new ExcelRowReader("path/to/your/excel/file.xlsx"); + return new ExcelRowReader("C:\\Users\\kim\\Desktop\\yummi.xlsx"); + //리눅스나 맥은 /User/형태로 } catch (IOException e) { throw new RuntimeException(e); } } @Bean - public ItemProcessor middleProcessor() { + public ItemProcessor fourthProcessor() { return new ItemProcessor() { @Override - public AfterEntity process(Row item) throws Exception { + public AfterEntity process(Row item) { AfterEntity afterEntity = new AfterEntity(); afterEntity.setUsername(item.getCell(0).getStringCellValue()); @@ -79,7 +80,7 @@ public class FourthBatch { } @Bean - public RepositoryItemWriter afterWriter() { + public RepositoryItemWriter fourthAfterWriter() { return new RepositoryItemWriterBuilder() .repository(afterRepository) diff --git a/src/main/java/com/example/springbatch/controller/MainController.java b/src/main/java/com/example/springbatch/controller/MainController.java index 96a7a03..6a86404 100644 --- a/src/main/java/com/example/springbatch/controller/MainController.java +++ b/src/main/java/com/example/springbatch/controller/MainController.java @@ -49,5 +49,17 @@ public class MainController { return "ok"; } + @GetMapping("/fourth") + private String fourthApi(@RequestParam("value") String value) throws Exception { + + JobParameters jobParameters = new JobParametersBuilder() + .addString("date", value) + .toJobParameters(); + + jobLauncher.run(jobRegistry.getJob("fourthJob"), jobParameters); + + return "ok"; + } + //https://docs.spring.io/spring-batch/reference/job/configuring-launcher.html }