Skip to content

Commit

Permalink
Merge pull request #54 from chanmin-00/issue/53-scheduled
Browse files Browse the repository at this point in the history
✨ [Feat] 스케줄링을 사용하여 영화 정보 업데이트 자동화 구축
  • Loading branch information
chanmin-00 authored Jul 24, 2024
2 parents 1e23559 + 40450d0 commit 79b1ebf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/WatchWithMe/WatchWithMeApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class WatchWithMeApplication {

public static void main(String[] args) {
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/WatchWithMe/global/config/SchedulerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package WatchWithMe.global.config;

import WatchWithMe.global.exception.GlobalException;
import WatchWithMe.global.exception.code.GlobalErrorCode;
import WatchWithMe.service.MovieService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class SchedulerConfig implements SchedulingConfigurer {

private final int POOL_SIZE = 10; // 스레드 풀 사이즈
private final MovieService movieService;

@Scheduled(initialDelay=1000, fixedDelay=60000 * 5) // 5분 마다 수행
public void updateMovieList() {
try {
movieService.updateMovieList();
log.info("영화 정보 업데이트 완료");
} catch (Exception e) {
throw new GlobalException(GlobalErrorCode._INTERNAL_SERVER_ERROR);
}
}

// 여러 개의 스레드 동시 처리
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
final ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
threadPoolTaskScheduler.setThreadNamePrefix("test-scheduled-task-pool-");
threadPoolTaskScheduler.initialize();

taskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
"/api/v1/actor/**",
"/api/v1/director/**",
"/api/v1/movie/**",
"api/v1/admin/**",
"/health"
).permitAll()
.requestMatchers("/api/v1/admin/**").hasAuthority("ADMIN")
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spring:
jpa:
database: mysql
database-platform: org.hibernate.dialect.MySQLDialect
show-sql: true
show-sql: false
hibernate:
ddl-auto: update
properties:
Expand Down

0 comments on commit 79b1ebf

Please sign in to comment.