Skip to content

Commit

Permalink
YEL-214 [feat] lunch event spring batch, scheduler 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjeongs committed Feb 26, 2024
1 parent 926afde commit f2b6438
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,5 @@ List<User> findAllByOtherGroupContainingYelloId(@Param("groupName") String group
+ "where LOWER(u.name) like LOWER(CONCAT('%', :name, '%'))")
Page<User> findAllByNameContaining(Pageable pageable, @Param("name") String name);

Page<User> findAllByPageable(Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,4 @@ List<User> findAllByOtherGroupContainingYelloId(String groupName, String keyword

void delete(User user);

Page<User> findAllByPageable(Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,38 @@

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 jakarta.persistence.EntityManagerFactory;
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.batch.item.database.JdbcPagingItemReader;
import org.springframework.batch.item.database.JpaPagingItemReader;
import org.springframework.batch.item.database.Order;
import org.springframework.batch.item.database.PagingQueryProvider;
import org.springframework.batch.item.database.builder.JdbcPagingItemReaderBuilder;
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder;
import org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Configuration
public class ChunkReader {
private final UserJpaRepository userRepository;
private final EntityManagerFactory entityManagerFactory;
private final DataSource dataSource;

@Bean
@StepScope
public RepositoryItemReader<User> userDataReader() {
public RepositoryItemReader<User> usersDataRepositoryItemReader() {

return new RepositoryItemReaderBuilder<User>()
.name("userDataReader")
Expand All @@ -30,4 +43,45 @@ public RepositoryItemReader<User> userDataReader() {
.sorts(Collections.singletonMap("id", Sort.Direction.ASC))
.build();
}

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

return new JpaPagingItemReaderBuilder<User>()
.name("userDataReader")
.pageSize(100)
.queryString("SELECT u FROM USER u ORDER BY id")
.entityManagerFactory(entityManagerFactory)
.build();
}

@Bean
@StepScope
public JdbcPagingItemReader<User> userDataJdbcPagingItemReader() throws Exception {

return new JdbcPagingItemReaderBuilder<User>()
.pageSize(100)
.fetchSize(100)
.dataSource(dataSource)
.rowMapper(new BeanPropertyRowMapper<>(User.class))
.queryProvider(createUserDataQueryProvider())
.name("jdbcPagingItemReader")
.build();
}

@Bean
public PagingQueryProvider createUserDataQueryProvider() throws Exception {
SqlPagingQueryProviderFactoryBean queryProvider = new SqlPagingQueryProviderFactoryBean();
queryProvider.setDataSource(dataSource);
queryProvider.setSelectClause("id");
queryProvider.setFromClause("from user");

Map<String, Order> sortKeys = new HashMap<>();
sortKeys.put("id", Order.ASCENDING);
queryProvider.setSortKeys(sortKeys);

return queryProvider.getObject();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class JobConfiguration {
private final StepConfiguration stepConfiguration;

@Bean
public Job myJob(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new JobBuilder("myJob", jobRepository)
public Job lunchEventJob(JobRepository jobRepository, PlatformTransactionManager transactionManager) throws Exception {
return new JobBuilder("lunchEventJob", jobRepository)
.start(stepConfiguration.lunchEventAlarmStep(jobRepository, transactionManager))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.PlatformTransactionManager;

@Slf4j
Expand All @@ -23,10 +24,10 @@ public class StepConfiguration {
private final ChunkWriter chunkWriter;

@Bean
public Step lunchEventAlarmStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
public Step lunchEventAlarmStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) throws Exception {
return new StepBuilder("lunchEventStep", jobRepository)
.<User, User>chunk(100, transactionManager)
.reader(chunkReader.userDataReader())
.reader(chunkReader.userDataJdbcPagingItemReader())
.writer(chunkWriter.lunchEventWriter())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.yello.server.infrastructure.scheduler;


import com.yello.server.infrastructure.batch.JobConfiguration;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
@RequiredArgsConstructor
public class EventScheduler {

private final JobLauncher jobLauncher;
private final JobConfiguration jobConfiguration;
private final JobRepository jobRepository;
private final PlatformTransactionManager transactionManager;

@Scheduled(cron="0 0 12 * * ?")
public void lunchEventRunJob() {

//JobParamter의 역할은 반복해서 실행되는 Job의 유일한 ID임, 동일한 값이 세팅되면 두번째부터 실행안됨)
JobParameters jobParameters = new JobParametersBuilder()
.addString("uuid", UUID.randomUUID().toString())
.toJobParameters();

try {
jobLauncher.run(jobConfiguration.lunchEventJob(jobRepository, transactionManager), jobParameters);
} catch (JobExecutionAlreadyRunningException | JobInstanceAlreadyCompleteException
| JobParametersInvalidException | org.springframework.batch.core.repository.JobRestartException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,4 @@ public Page<User> findAllByNameContaining(Pageable pageable, String name) {
public void delete(User user) {
data.remove(user);
}

@Override
public Page<User> findAllByPageable(Pageable pageable) {
final List<User> userList = data.stream()
.skip(pageable.getOffset())
.limit(pageable.getPageSize())
.toList();
return new PageImpl<>(userList);
}
}

0 comments on commit f2b6438

Please sign in to comment.