diff --git a/src/main/java/org/withtime/be/withtimebe/domain/log/dateplacelog/scheduler/DatePlaceLogScheduler.java b/src/main/java/org/withtime/be/withtimebe/domain/log/dateplacelog/scheduler/DatePlaceLogScheduler.java index 3982543..92e29bc 100644 --- a/src/main/java/org/withtime/be/withtimebe/domain/log/dateplacelog/scheduler/DatePlaceLogScheduler.java +++ b/src/main/java/org/withtime/be/withtimebe/domain/log/dateplacelog/scheduler/DatePlaceLogScheduler.java @@ -4,6 +4,8 @@ import java.time.LocalDateTime; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -24,8 +26,15 @@ public class DatePlaceLogScheduler { private final DatePlaceRepository datePlaceRepository; private final DatePlaceLogRepository datePlaceLogRepository; + @Async("logTaskExecutor") @Scheduled(cron = "${scheduler.logs.date-place.sync-cron}") @Transactional(readOnly = true) + @CacheEvict( + value = "date-place-log", + allEntries = true, + cacheManager = "redisCacheManager", + beforeInvocation = false + ) public void syncPlaceCategoryLogsToDB() { LocalDate now = LocalDate.from(LocalDateTime.now().minusMinutes(1)); diff --git a/src/main/java/org/withtime/be/withtimebe/domain/log/dateplacelog/service/query/DatePlaceLogQueryServiceImpl.java b/src/main/java/org/withtime/be/withtimebe/domain/log/dateplacelog/service/query/DatePlaceLogQueryServiceImpl.java index 5cab6d5..ef2e830 100644 --- a/src/main/java/org/withtime/be/withtimebe/domain/log/dateplacelog/service/query/DatePlaceLogQueryServiceImpl.java +++ b/src/main/java/org/withtime/be/withtimebe/domain/log/dateplacelog/service/query/DatePlaceLogQueryServiceImpl.java @@ -2,6 +2,7 @@ import java.util.List; +import org.springframework.cache.annotation.Cacheable; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.aggregation.Aggregation; diff --git a/src/main/java/org/withtime/be/withtimebe/domain/log/placecategorylog/aop/LogPlaceCategoryAspect.java b/src/main/java/org/withtime/be/withtimebe/domain/log/placecategorylog/aop/LogPlaceCategoryAspect.java index 2bede1e..5148bed 100644 --- a/src/main/java/org/withtime/be/withtimebe/domain/log/placecategorylog/aop/LogPlaceCategoryAspect.java +++ b/src/main/java/org/withtime/be/withtimebe/domain/log/placecategorylog/aop/LogPlaceCategoryAspect.java @@ -27,7 +27,7 @@ public class LogPlaceCategoryAspect { private final RedisTemplate redisTemplate; - @Async + @Async("logTaskExecutor") @AfterReturning("@annotation(org.withtime.be.withtimebe.domain.log.placecategorylog.annotation.LogPlaceCategory)") public void logPlaceCategory(JoinPoint joinPoint) { diff --git a/src/main/java/org/withtime/be/withtimebe/domain/log/placecategorylog/scheduler/PlaceCategoryLogScheduler.java b/src/main/java/org/withtime/be/withtimebe/domain/log/placecategorylog/scheduler/PlaceCategoryLogScheduler.java index ccfa9cd..fff2d30 100644 --- a/src/main/java/org/withtime/be/withtimebe/domain/log/placecategorylog/scheduler/PlaceCategoryLogScheduler.java +++ b/src/main/java/org/withtime/be/withtimebe/domain/log/placecategorylog/scheduler/PlaceCategoryLogScheduler.java @@ -1,12 +1,10 @@ package org.withtime.be.withtimebe.domain.log.placecategorylog.scheduler; import java.time.LocalDate; -import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; @@ -15,10 +13,9 @@ import org.springframework.cache.annotation.CacheEvict; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ZSetOperations; +import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; -import org.withtime.be.withtimebe.domain.date.entity.PlaceCategory; -import org.withtime.be.withtimebe.domain.date.repository.PlaceCategoryRepository; import org.withtime.be.withtimebe.domain.log.placecategorylog.converter.PlaceCategoryLogConverter; import org.withtime.be.withtimebe.domain.log.placecategorylog.model.PlaceCategoryLog; import org.withtime.be.withtimebe.domain.log.placecategorylog.repository.PlaceCategoryLogRepository; @@ -35,10 +32,11 @@ public class PlaceCategoryLogScheduler { private final RedisTemplate redisTemplate; private final PlaceCategoryLogRepository placeCategoryLogRepository; - @Scheduled(cron = "${scheduler.logs.place-category.sync-cron}") // 매 5분마다 + @Async("logTaskExecutor") + @Scheduled(cron = "${scheduler.logs.place-category.sync-cron}") @CacheEvict( value = "place-category-log", - key = "'weekly:' + T(java.time.LocalDate).now().getYear() + '-' + T(java.time.temporal.WeekFields).ISO.weekOfYear().getFrom(T(java.time.LocalDate).now())", + allEntries = true, cacheManager = "redisCacheManager", beforeInvocation = false ) diff --git a/src/main/java/org/withtime/be/withtimebe/global/config/AsyncConfig.java b/src/main/java/org/withtime/be/withtimebe/global/config/AsyncConfig.java new file mode 100644 index 0000000..8aeaa32 --- /dev/null +++ b/src/main/java/org/withtime/be/withtimebe/global/config/AsyncConfig.java @@ -0,0 +1,35 @@ +package org.withtime.be.withtimebe.global.config; + +import java.util.concurrent.Executor; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +@EnableAsync +@Configuration +public class AsyncConfig { + + @Bean(name = "logTaskExecutor") + public Executor logTaskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(1); + executor.setMaxPoolSize(3); + executor.setQueueCapacity(5); + executor.setThreadNamePrefix("Executor-Log-"); + executor.initialize(); + return executor; + } + + @Bean(name = "weatherTaskExecutor") + public Executor weatherTaskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(1); + executor.setMaxPoolSize(3); + executor.setQueueCapacity(5); + executor.setThreadNamePrefix("Executor-Weather-"); + executor.initialize(); + return executor; + } +}