Skip to content

Commit

Permalink
YEL-214 [feat] spring batch reader 되지 않는 이슈 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjeongs committed Feb 29, 2024
1 parent a127d97 commit 03a9898
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 10 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -54,14 +56,25 @@ public JdbcCursorItemReader<User> jdbcCursorItemReader() {
.build();
}

@Bean
@StepScope
public ItemReader<User> userDataItemReader() {
return new JpaPagingItemReaderBuilder<User>()
.name("exampleItemReader")
.entityManagerFactory(this.entityManagerFactory)
.pageSize(10)
.queryString("SELECT u FROM User u")
.build();
}

@Bean
@StepScope
public JpaPagingItemReader<User> userDataJpaPagingItemReader() {

return new JpaPagingItemReaderBuilder<User>()
.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();
}
Expand All @@ -75,7 +88,7 @@ public JdbcPagingItemReader<User> userDataJdbcPagingItemReader() throws Exceptio
.fetchSize(100)
.dataSource(dataSource)
.queryProvider(createUserDataQueryProvider())
.rowMapper(new BeanPropertyRowMapper<>(User.class))
.rowMapper(new UserRowMapper())
.name("jdbcPagingItemReader")
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
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;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Slf4j
@RequiredArgsConstructor
@Configuration
@RequiredArgsConstructor
public class JobConfiguration {
private final StepConfiguration stepConfiguration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,30 @@
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;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

@Slf4j
@RequiredArgsConstructor
@Configuration
@RequiredArgsConstructor
public class StepConfiguration {

private final ChunkReader chunkReader;
private final ChunkProcessor chunkProcessor;
private final ChunkWriter chunkWriter;

@Bean
@JobScope
public Step lunchEventAlarmStep(JobRepository jobRepository,
PlatformTransactionManager transactionManager) throws Exception {
return new StepBuilder("lunchEventStep", jobRepository)
.<User, User>chunk(10, transactionManager)
.<User, User>chunk(100, transactionManager)
.reader(chunkReader.userDataJdbcPagingItemReader())
.processor(chunkProcessor.lunchEventProcessor())
.writer(chunkWriter.lunchEventWriter())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<User> {
@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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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임, 동일한 값이 세팅되면 두번째부터 실행안됨)
Expand Down

0 comments on commit 03a9898

Please sign in to comment.