-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#108] refactor: 포스트 조회수 통계 배치 작업 스프링 배치로 마이그레이션
- Loading branch information
1 parent
74b4fe7
commit ca019a9
Showing
12 changed files
with
257 additions
and
218 deletions.
There are no files selected for viewing
84 changes: 0 additions & 84 deletions
84
src/main/java/com/mallang/statistics/batch/PostViewStatisticJobLegacy.java
This file was deleted.
Oops, something went wrong.
30 changes: 0 additions & 30 deletions
30
src/main/java/com/mallang/statistics/batch/PostViewStatisticJobSchedulerLegacy.java
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
src/main/java/com/mallang/statistics/batch/exeution/JobExecution.java
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
src/main/java/com/mallang/statistics/batch/exeution/JobExecutionRepository.java
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
src/main/java/com/mallang/statistics/batch/exeution/JobHistoryRecorder.java
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
src/main/java/com/mallang/statistics/batch/exeution/Status.java
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/main/java/com/mallang/statistics/batch/job/PostViewHistoryDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.mallang.statistics.batch.job; | ||
|
||
import java.time.LocalDate; | ||
|
||
public record PostViewHistoryDto( | ||
Long postId, | ||
Long blogId, | ||
LocalDate date, | ||
int viewCount | ||
) { | ||
} |
123 changes: 123 additions & 0 deletions
123
src/main/java/com/mallang/statistics/batch/job/PostViewStatisticJobConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.mallang.statistics.batch.job; | ||
|
||
import com.mallang.post.domain.PostId; | ||
import com.mallang.statistics.statistic.PostViewStatistic; | ||
import com.mallang.statistics.statistic.PostViewStatisticRepository; | ||
import com.mallang.statistics.statistic.source.PostViewHistoryRepository; | ||
import jakarta.persistence.EntityManagerFactory; | ||
import java.time.LocalDateTime; | ||
import javax.sql.DataSource; | ||
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.configuration.annotation.JobScope; | ||
import org.springframework.batch.core.configuration.annotation.StepScope; | ||
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.ItemWriter; | ||
import org.springframework.batch.item.database.JdbcCursorItemReader; | ||
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder; | ||
import org.springframework.batch.item.database.builder.JpaItemWriterBuilder; | ||
import org.springframework.batch.repeat.RepeatStatus; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter; | ||
import org.springframework.jdbc.core.DataClassRowMapper; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Configuration | ||
public class PostViewStatisticJobConfig { | ||
|
||
private static final int CHUNK_SIZE = 500; | ||
|
||
private final EntityManagerFactory entityManagerFactory; | ||
private final DataSource dataSource; | ||
private final JobRepository jobRepository; | ||
private final PlatformTransactionManager txManager; | ||
private final PostViewStatisticRepository postViewStatisticRepository; | ||
private final PostViewHistoryRepository postViewHistoryRepository; | ||
private final StartAndEndTimeValidator startAndEndTimeValidator; | ||
|
||
@Bean | ||
public Job postViewStatisticJob() { | ||
return new JobBuilder("postViewStatisticJob", jobRepository) | ||
.start(postViewStatisticStep()) | ||
.next(postViewHistoryDeleteStep(null, null)) | ||
.validator(startAndEndTimeValidator) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step postViewStatisticStep() { | ||
return new StepBuilder("postViewStatisticStep", jobRepository) | ||
.<PostViewHistoryDto, PostViewStatistic>chunk(CHUNK_SIZE, txManager) | ||
.reader(postViewHistoryDtoReader(null, null)) | ||
.processor(postViewHistoryToStatisticProcessor()) | ||
.writer(postViewStatisticWriter()) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@JobScope | ||
public Step postViewHistoryDeleteStep( | ||
@Value("#{jobParameters[startInclude]}") LocalDateTime startInclude, | ||
@Value("#{jobParameters[endExclude]}") LocalDateTime endExclude | ||
) { | ||
return new StepBuilder("postViewHistoryDeleteStep", jobRepository) | ||
.tasklet((contribution, chunkContext) -> { | ||
postViewHistoryRepository.deleteWithCreatedDateBetweenIncludeStartAndExcludeEnd( | ||
startInclude, endExclude | ||
); | ||
return RepeatStatus.FINISHED; | ||
}, txManager) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@StepScope | ||
public JdbcCursorItemReader<PostViewHistoryDto> postViewHistoryDtoReader( | ||
@Value("#{jobParameters[startInclude]}") LocalDateTime startInclude, | ||
@Value("#{jobParameters[endExclude]}") LocalDateTime endExclude | ||
) { | ||
return new JdbcCursorItemReaderBuilder<PostViewHistoryDto>() | ||
.fetchSize(CHUNK_SIZE) | ||
.dataSource(dataSource) | ||
.rowMapper(new DataClassRowMapper<>(PostViewHistoryDto.class)) | ||
.sql(""" | ||
SELECT pvh.post_id, pvh.blog_id, pvh.date, COUNT(*) as view_count | ||
FROM post_view_history pvh | ||
WHERE pvh.created_date >= ? AND pvh.created_date < ? | ||
GROUP BY pvh.post_id, pvh.blog_id, pvh.date | ||
""") | ||
.name("postViewHistoryDtoReader") | ||
.preparedStatementSetter(new ArgumentPreparedStatementSetter(new Object[]{startInclude, endExclude})) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public ItemProcessor<PostViewHistoryDto, PostViewStatistic> postViewHistoryToStatisticProcessor() { | ||
return historyDto -> { | ||
PostId postId = new PostId(historyDto.postId(), historyDto.blogId()); | ||
PostViewStatistic postViewStatistic = postViewStatisticRepository | ||
.findByPostIdAndStatisticDate(postId, historyDto.date()) | ||
.orElseGet(() -> postViewStatisticRepository.save( | ||
new PostViewStatistic(historyDto.date(), postId) | ||
)); | ||
postViewStatistic.addCount(historyDto.viewCount()); | ||
return postViewStatistic; | ||
}; | ||
} | ||
|
||
@Bean | ||
public ItemWriter<PostViewStatistic> postViewStatisticWriter() { | ||
return new JpaItemWriterBuilder<PostViewStatistic>() | ||
.entityManagerFactory(entityManagerFactory) | ||
.build(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/mallang/statistics/batch/job/PostViewStatisticJobScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.mallang.statistics.batch.job; | ||
|
||
import static com.mallang.common.utils.LocalDateTimeUtils.onlyHours; | ||
|
||
import java.time.LocalDateTime; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.JobParametersBuilder; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class PostViewStatisticJobScheduler { | ||
|
||
public static final String EACH_HOUR_CRON = "0 0 * * * *"; | ||
|
||
private final JobLauncher jobLauncher; | ||
private final Job postViewStatisticJob; | ||
|
||
@Scheduled(cron = EACH_HOUR_CRON) | ||
public void runTask() throws Exception { | ||
LocalDateTime now = LocalDateTime.now(); | ||
log.info("포스트 조회수 통계 작업 실행 [실행시간: {}]", now); | ||
LocalDateTime startInclude = onlyHours(now.minusHours(2)); | ||
LocalDateTime endExclude = onlyHours(now); | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addLocalDateTime("startInclude", startInclude) | ||
.addLocalDateTime("endExclude", endExclude) | ||
.toJobParameters(); | ||
jobLauncher.run(postViewStatisticJob, jobParameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.