-
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.
Browse files
Browse the repository at this point in the history
…eduler Be/feat/#255 refactor scheduler
- Loading branch information
Showing
6 changed files
with
73 additions
and
28 deletions.
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
backEnd/src/main/java/com/quiz/ourclass/domain/challenge/scheduler/ChallengeScheduler.java
This file was deleted.
Oops, something went wrong.
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
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
16 changes: 13 additions & 3 deletions
16
backEnd/src/main/java/com/quiz/ourclass/global/config/SchedulingConfig.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 |
---|---|---|
@@ -1,10 +1,20 @@ | ||
package com.quiz.ourclass.global.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableScheduling; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; | ||
|
||
@Configuration | ||
@EnableScheduling | ||
public class SchedulingConfig { | ||
|
||
} | ||
@Bean | ||
public ThreadPoolTaskScheduler taskScheduler() { | ||
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); | ||
scheduler.setPoolSize(10); // 동시에 실행할 스레드 수 설정 | ||
scheduler.setThreadNamePrefix("scheduled-task-"); //스레드 이름 접두사 | ||
scheduler.setAwaitTerminationSeconds(60); | ||
scheduler.setWaitForTasksToCompleteOnShutdown(true); //작업 완료까지 스프링종료를 대기 | ||
scheduler.initialize(); | ||
return scheduler; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
backEnd/src/main/java/com/quiz/ourclass/global/util/scheduler/SchedulingService.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,25 @@ | ||
package com.quiz.ourclass.global.util.scheduler; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.function.Consumer; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.TaskScheduler; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class SchedulingService { | ||
|
||
private final TaskScheduler taskScheduler; | ||
private final TransactionTemplate transactionTemplate; | ||
|
||
public <T> void scheduleTask(T target, Consumer<T> action, LocalDateTime executionTime) { | ||
SchedulingTask<T> task = new SchedulingTask<>(target, action, transactionTemplate); | ||
Instant executionDate = executionTime.atZone(ZoneId.systemDefault()).toInstant(); | ||
|
||
taskScheduler.schedule(task, executionDate); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
backEnd/src/main/java/com/quiz/ourclass/global/util/scheduler/SchedulingTask.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,21 @@ | ||
package com.quiz.ourclass.global.util.scheduler; | ||
|
||
import java.util.function.Consumer; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.transaction.support.TransactionTemplate; | ||
|
||
@AllArgsConstructor | ||
public class SchedulingTask<T> implements Runnable { | ||
|
||
private final T target; | ||
private final Consumer<T> task; | ||
private final TransactionTemplate transactionTemplate; | ||
|
||
@Override | ||
public void run() { | ||
transactionTemplate.execute(status -> { | ||
task.accept(target); | ||
return null; | ||
}); | ||
} | ||
} |