From b025439a347f5b2c028803bd11021dff9283a1a1 Mon Sep 17 00:00:00 2001 From: hyeonjeongs Date: Mon, 26 Feb 2024 12:17:25 +0900 Subject: [PATCH] YEL-214 [setting] spring batch setting --- .../infrastructure/batch/ChunkProcessor.java | 20 ++++++++++ .../infrastructure/batch/ChunkReader.java | 33 +++++++++++++++ .../infrastructure/batch/ChunkWriter.java | 22 ++++++++++ .../batch/JobConfiguration.java | 26 ++++++++++++ .../batch/StepConfiguration.java | 40 +++++++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java create mode 100644 src/main/java/com/yello/server/infrastructure/batch/ChunkReader.java create mode 100644 src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.java create mode 100644 src/main/java/com/yello/server/infrastructure/batch/JobConfiguration.java create mode 100644 src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java diff --git a/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java b/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java new file mode 100644 index 00000000..b9be89f7 --- /dev/null +++ b/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java @@ -0,0 +1,20 @@ +package com.yello.server.infrastructure.batch; + + +import com.yello.server.domain.user.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@RequiredArgsConstructor +@Configuration +public class ChunkProcessor { + + @Bean + @StepScope + public ItemProcessor lunchEventProcessor() { + return user -> new User); + } +} diff --git a/src/main/java/com/yello/server/infrastructure/batch/ChunkReader.java b/src/main/java/com/yello/server/infrastructure/batch/ChunkReader.java new file mode 100644 index 00000000..9665eba9 --- /dev/null +++ b/src/main/java/com/yello/server/infrastructure/batch/ChunkReader.java @@ -0,0 +1,33 @@ +package com.yello.server.infrastructure.batch; + +import com.yello.server.domain.user.entity.User; +import com.yello.server.domain.user.repository.UserJpaRepository; +import com.yello.server.domain.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.configuration.annotation.StepScope; +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 java.util.Collections; + +@RequiredArgsConstructor +@Configuration +public class ChunkReader { + private final UserJpaRepository userRepository; + + @Bean + @StepScope + public RepositoryItemReader userDataReader() { + + return new RepositoryItemReaderBuilder() + .name("userDataReader") + .repository(userRepository) + .methodName("findAllByPageable") + .pageSize(100) + .sorts(Collections.singletonMap("id", Sort.Direction.ASC)) + .build(); + } +} diff --git a/src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.java b/src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.java new file mode 100644 index 00000000..ae2d0b18 --- /dev/null +++ b/src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.java @@ -0,0 +1,22 @@ +package com.yello.server.infrastructure.batch; + +import com.yello.server.domain.user.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.batch.item.ItemWriter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +@RequiredArgsConstructor +@Configuration +public class ChunkWriter { + + @Bean + @StepScope + public ItemWriter lunchEventWriter() { + return items -> items.forEach(item -> { + + }); + } +} diff --git a/src/main/java/com/yello/server/infrastructure/batch/JobConfiguration.java b/src/main/java/com/yello/server/infrastructure/batch/JobConfiguration.java new file mode 100644 index 00000000..41084f11 --- /dev/null +++ b/src/main/java/com/yello/server/infrastructure/batch/JobConfiguration.java @@ -0,0 +1,26 @@ +package com.yello.server.infrastructure.batch; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.job.builder.JobBuilder; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Slf4j +@RequiredArgsConstructor +@Configuration +public class JobConfiguration { + private final StepConfiguration stepConfiguration; + + @Bean + public Job myJob(JobRepository jobRepository, PlatformTransactionManager transactionManager) { + return new JobBuilder("myJob", jobRepository) + .start(stepConfiguration.lunchEventAlarmStep(jobRepository, transactionManager)) + .build(); + } + + +} diff --git a/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java b/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java new file mode 100644 index 00000000..9b2a3708 --- /dev/null +++ b/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java @@ -0,0 +1,40 @@ +package com.yello.server.infrastructure.batch; + +import com.yello.server.domain.user.entity.User; +import com.yello.server.domain.user.entity.UserData; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +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.repeat.RepeatStatus; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +@Slf4j +@RequiredArgsConstructor +@Configuration +public class StepConfiguration { + private final ChunkReader chunkReader; + private final ChunkProcessor chunkProcessor; + private final ChunkWriter chunkWriter; + + @Bean + public Step myStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) { + return new StepBuilder("myStep", jobRepository) + .tasklet(((contribution, chunkContext) -> RepeatStatus.FINISHED), transactionManager) // or .chunk(chunkSize, transactionManager) + .build(); + } + + @Bean + public Step lunchEventAlarmStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) { + return new StepBuilder("lunchEventStep", jobRepository) + .chunk(100, transactionManager) + .reader(chunkReader.userDataReader()) + .writer(chunkWriter.lunchEventWriter()) + .build(); + } +}