From 03a9898d3bde98ba4ac1432f384901c6e2f0aae0 Mon Sep 17 00:00:00 2001 From: hyeonjeongs Date: Thu, 29 Feb 2024 10:59:23 +0900 Subject: [PATCH] =?UTF-8?q?YEL-214=20[feat]=20spring=20batch=20reader=20?= =?UTF-8?q?=EB=90=98=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EC=9D=B4=EC=8A=88=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 +++- .../infrastructure/batch/ChunkProcessor.java | 1 + .../infrastructure/batch/ChunkReader.java | 19 ++++++++++++++++--- .../infrastructure/batch/ChunkWriter.java | 1 + .../batch/JobConfiguration.java | 3 ++- .../batch/StepConfiguration.java | 8 +++++--- .../infrastructure/batch/UserRowMapper.java | 18 ++++++++++++++++++ .../service/NotificationFcmService.java | 1 - .../scheduler/EventScheduler.java | 2 +- 9 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/yello/server/infrastructure/batch/UserRowMapper.java diff --git a/build.gradle b/build.gradle index 6a48428a..909410ed 100644 --- a/build.gradle +++ b/build.gradle @@ -48,9 +48,11 @@ dependencies { // Repositories implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.amqp:spring-rabbit:3.1.1' + implementation 'org.hibernate:hibernate-core:6.4.4.Final' + implementation 'mysql:mysql-connector-java:8.0.33' runtimeOnly 'com.h2database:h2' - runtimeOnly 'com.mysql:mysql-connector-j:8.0.31' + runtimeOnly 'com.mysql:mysql-connector-j:8.2.0' // Validations implementation 'org.springframework.boot:spring-boot-starter-validation' diff --git a/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java b/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java index 7159f88c..f0e2dfdb 100644 --- a/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java +++ b/src/main/java/com/yello/server/infrastructure/batch/ChunkProcessor.java @@ -3,6 +3,7 @@ import com.yello.server.domain.user.entity.User; import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.item.ItemProcessor; import org.springframework.context.annotation.Configuration; diff --git a/src/main/java/com/yello/server/infrastructure/batch/ChunkReader.java b/src/main/java/com/yello/server/infrastructure/batch/ChunkReader.java index 0cf181f2..3dc3058a 100644 --- a/src/main/java/com/yello/server/infrastructure/batch/ChunkReader.java +++ b/src/main/java/com/yello/server/infrastructure/batch/ChunkReader.java @@ -6,7 +6,9 @@ import java.util.Collections; import javax.sql.DataSource; import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.data.RepositoryItemReader; import org.springframework.batch.item.data.builder.RepositoryItemReaderBuilder; import org.springframework.batch.item.database.JdbcCursorItemReader; @@ -23,8 +25,8 @@ import org.springframework.data.domain.Sort; import org.springframework.jdbc.core.BeanPropertyRowMapper; -@RequiredArgsConstructor @Configuration +@RequiredArgsConstructor public class ChunkReader { private final UserJpaRepository userRepository; @@ -54,6 +56,17 @@ public JdbcCursorItemReader jdbcCursorItemReader() { .build(); } + @Bean + @StepScope + public ItemReader userDataItemReader() { + return new JpaPagingItemReaderBuilder() + .name("exampleItemReader") + .entityManagerFactory(this.entityManagerFactory) + .pageSize(10) + .queryString("SELECT u FROM User u") + .build(); + } + @Bean @StepScope public JpaPagingItemReader userDataJpaPagingItemReader() { @@ -61,7 +74,7 @@ public JpaPagingItemReader userDataJpaPagingItemReader() { return new JpaPagingItemReaderBuilder() .name("userDataReader") .pageSize(100) - .queryString("SELECT u FROM USER u ORDER BY id") + .queryString("SELECT u FROM User u WHERE deletedAt is NULL ORDER BY id") .entityManagerFactory(entityManagerFactory) .build(); } @@ -75,7 +88,7 @@ public JdbcPagingItemReader userDataJdbcPagingItemReader() throws Exceptio .fetchSize(100) .dataSource(dataSource) .queryProvider(createUserDataQueryProvider()) - .rowMapper(new BeanPropertyRowMapper<>(User.class)) + .rowMapper(new UserRowMapper()) .name("jdbcPagingItemReader") .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 index 8b3f8ed7..17941a7c 100644 --- a/src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.java +++ b/src/main/java/com/yello/server/infrastructure/batch/ChunkWriter.java @@ -3,6 +3,7 @@ import com.yello.server.domain.user.entity.User; import com.yello.server.infrastructure.firebase.service.NotificationService; import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.StepScope; import org.springframework.batch.item.ItemWriter; import org.springframework.context.annotation.Bean; diff --git a/src/main/java/com/yello/server/infrastructure/batch/JobConfiguration.java b/src/main/java/com/yello/server/infrastructure/batch/JobConfiguration.java index 8bfe424d..9bfd713e 100644 --- a/src/main/java/com/yello/server/infrastructure/batch/JobConfiguration.java +++ b/src/main/java/com/yello/server/infrastructure/batch/JobConfiguration.java @@ -3,6 +3,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.batch.core.Job; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.job.builder.JobBuilder; import org.springframework.batch.core.repository.JobRepository; import org.springframework.context.annotation.Bean; @@ -10,8 +11,8 @@ import org.springframework.transaction.PlatformTransactionManager; @Slf4j -@RequiredArgsConstructor @Configuration +@RequiredArgsConstructor public class JobConfiguration { private final StepConfiguration stepConfiguration; diff --git a/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java b/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java index f46f6717..f4bd9d11 100644 --- a/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java +++ b/src/main/java/com/yello/server/infrastructure/batch/StepConfiguration.java @@ -4,6 +4,8 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.configuration.annotation.JobScope; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.builder.StepBuilder; import org.springframework.context.annotation.Bean; @@ -11,8 +13,8 @@ import org.springframework.transaction.PlatformTransactionManager; @Slf4j -@RequiredArgsConstructor @Configuration +@RequiredArgsConstructor public class StepConfiguration { private final ChunkReader chunkReader; @@ -20,12 +22,12 @@ public class StepConfiguration { private final ChunkWriter chunkWriter; @Bean + @JobScope public Step lunchEventAlarmStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) throws Exception { return new StepBuilder("lunchEventStep", jobRepository) - .chunk(10, transactionManager) + .chunk(100, transactionManager) .reader(chunkReader.userDataJdbcPagingItemReader()) - .processor(chunkProcessor.lunchEventProcessor()) .writer(chunkWriter.lunchEventWriter()) .build(); } diff --git a/src/main/java/com/yello/server/infrastructure/batch/UserRowMapper.java b/src/main/java/com/yello/server/infrastructure/batch/UserRowMapper.java new file mode 100644 index 00000000..02d8abf0 --- /dev/null +++ b/src/main/java/com/yello/server/infrastructure/batch/UserRowMapper.java @@ -0,0 +1,18 @@ +package com.yello.server.infrastructure.batch; + +import com.yello.server.domain.user.entity.User; +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class UserRowMapper implements RowMapper { + @Override + public User mapRow(ResultSet rs, int rowNum) throws SQLException { + return User.builder() + .id(rs.getLong("id")) + .name(rs.getString("name")) + .yelloId(rs.getString("yello_id")) + .build(); + } +} diff --git a/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java b/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java index 0ff43101..58aa485c 100644 --- a/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java +++ b/src/main/java/com/yello/server/infrastructure/firebase/service/NotificationFcmService.java @@ -120,7 +120,6 @@ public EmptyObject adminSendCustomNotification(Long adminId, NotificationCustomM @Override public void sendLunchEventNotification(User user) { - System.out.println(user.getId() + " asfsfsdfsd"); final User receiver = userRepository.getById(user.getId()); NotificationMessage notificationMessage = diff --git a/src/main/java/com/yello/server/infrastructure/scheduler/EventScheduler.java b/src/main/java/com/yello/server/infrastructure/scheduler/EventScheduler.java index 729cfa58..8138eeb7 100644 --- a/src/main/java/com/yello/server/infrastructure/scheduler/EventScheduler.java +++ b/src/main/java/com/yello/server/infrastructure/scheduler/EventScheduler.java @@ -34,7 +34,7 @@ public class EventScheduler { private final PlatformTransactionManager transactionManager; private final UserDataRepository userDataRepository; - @Scheduled(cron="0 19 2 * * ?") + @Scheduled(cron="0 58 10 * * ?") public void lunchEventRunJob() { //JobParamter의 역할은 반복해서 실행되는 Job의 유일한 ID임, 동일한 값이 세팅되면 두번째부터 실행안됨)