From 6615521dd277b321ce8ee3505e1ab4ccf35c0e3a Mon Sep 17 00:00:00 2001 From: kimjihun Date: Fri, 2 Aug 2024 00:20:25 +0900 Subject: [PATCH] =?UTF-8?q?*=20FourthBatch=20=EC=97=91=EC=85=80=20?= =?UTF-8?q?=EC=9D=BD=EA=B8=B0=20=EC=9E=91=EC=84=B1=20=EC=A4=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../example/springbatch/batch/FirstBatch.java | 1 - .../springbatch/batch/FourthBatch.java | 89 +++++++++++++++++++ .../springbatch/schedule/FirstSchedule.java | 2 +- 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/example/springbatch/batch/FourthBatch.java diff --git a/build.gradle b/build.gradle index ecda773..10cff73 100644 --- a/build.gradle +++ b/build.gradle @@ -24,6 +24,7 @@ repositories { } dependencies { + implementation 'org.apache.poi:poi-ooxml:5.3.0' implementation 'org.springframework.boot:spring-boot-starter-batch' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-jdbc' diff --git a/src/main/java/com/example/springbatch/batch/FirstBatch.java b/src/main/java/com/example/springbatch/batch/FirstBatch.java index 138a983..e4297e8 100644 --- a/src/main/java/com/example/springbatch/batch/FirstBatch.java +++ b/src/main/java/com/example/springbatch/batch/FirstBatch.java @@ -7,7 +7,6 @@ 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.launch.support.RunIdIncrementer; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.builder.StepBuilder; import org.springframework.batch.item.ItemProcessor; diff --git a/src/main/java/com/example/springbatch/batch/FourthBatch.java b/src/main/java/com/example/springbatch/batch/FourthBatch.java new file mode 100644 index 0000000..5268a35 --- /dev/null +++ b/src/main/java/com/example/springbatch/batch/FourthBatch.java @@ -0,0 +1,89 @@ +package com.example.springbatch.batch; + +import com.example.springbatch.entity.AfterEntity; +import com.example.springbatch.repository.AfterRepository; +import org.apache.poi.ss.usermodel.Row; +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.ItemReader; +import org.springframework.batch.item.data.RepositoryItemWriter; +import org.springframework.batch.item.data.builder.RepositoryItemWriterBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +import java.io.IOException; + +@Configuration +public class FourthBatch { + + private final JobRepository jobRepository; + private final PlatformTransactionManager platformTransactionManager; + private final AfterRepository afterRepository; + + public FourthBatch(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager, AfterRepository afterRepository) { + this.jobRepository = jobRepository; + this.platformTransactionManager = platformTransactionManager; + this.afterRepository = afterRepository; + } + + @Bean + public Job fourthJob() { + + System.out.println("fourth job"); + + return new JobBuilder("fourthJob", jobRepository) + .start(fourthStep()) + .build(); + } + + @Bean + public Step fourthStep() { + + return new StepBuilder("fourthStep", jobRepository) + . chunk(10, platformTransactionManager) + .reader(excelReader()) + .processor(middleProcessor()) + .writer(afterWriter()) + .build(); + } + + @Bean + public ItemReader excelReader() { + + try { + return new ExcelRowReader("path/to/your/excel/file.xlsx"); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Bean + public ItemProcessor middleProcessor() { + + return new ItemProcessor() { + + @Override + public AfterEntity process(Row item) throws Exception { + + AfterEntity afterEntity = new AfterEntity(); + afterEntity.setUsername(item.getCell(0).getStringCellValue()); + + return afterEntity; + } + }; + } + + @Bean + public RepositoryItemWriter afterWriter() { + + return new RepositoryItemWriterBuilder() + .repository(afterRepository) + .methodName("save") + .build(); + } +} diff --git a/src/main/java/com/example/springbatch/schedule/FirstSchedule.java b/src/main/java/com/example/springbatch/schedule/FirstSchedule.java index 498e38f..2ba5b23 100644 --- a/src/main/java/com/example/springbatch/schedule/FirstSchedule.java +++ b/src/main/java/com/example/springbatch/schedule/FirstSchedule.java @@ -21,7 +21,7 @@ public class FirstSchedule { this.jobRegistry = jobRegistry; } - @Scheduled(cron = "10 * * * * *", zone = "Asia/Seoul") + //@Scheduled(cron = "10 * * * * *", zone = "Asia/Seoul") public void runFirstJob() throws Exception { System.out.println("first schedule start");