xiongzhu 4 лет назад
Родитель
Сommit
ec85f2fa63

+ 19 - 0
src/main/java/com/izouma/nineth/config/SchedulingConfig.java

@@ -0,0 +1,19 @@
+package com.izouma.nineth.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.TaskScheduler;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
+
+@Configuration
+public class SchedulingConfig {
+    @Bean
+    public TaskScheduler taskScheduler() {
+        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
+        // 定时任务执行线程池核心线程数
+        taskScheduler.setPoolSize(50);
+        taskScheduler.setRemoveOnCancelPolicy(true);
+        taskScheduler.setThreadNamePrefix("Scheduler-");
+        return taskScheduler;
+    }
+}

+ 8 - 0
src/main/java/com/izouma/nineth/repo/CollectionRepo.java

@@ -78,6 +78,14 @@ public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpec
     @CacheEvict(value = "collection", key = "#id")
     void setOnShelf(Long id, boolean onShelf);
 
+    @Transactional
+    @Modifying
+    @Query("update Collection c set c.onShelf = ?2, c.salable = ?2 where c.id = ?1")
+    @CacheEvict(value = "collection", key = "#id")
+    void scheduleOnShelf(Long id, boolean onShelf);
+
     @Query("select c.currentNumber from Collection c where c.id = ?1")
     Optional<Integer> getCurrentNumber(Long id);
+
+    List<Collection> findByScheduleSaleTrue();
 }

+ 9 - 2
src/main/java/com/izouma/nineth/service/CollectionService.java

@@ -24,9 +24,11 @@ import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.scheduling.TaskScheduler;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.PostConstruct;
 import javax.persistence.criteria.Predicate;
 import javax.transaction.Transactional;
 import java.time.LocalDateTime;
@@ -44,6 +46,11 @@ public class CollectionService {
     private UserRepo                      userRepo;
     private AssetService                  assetService;
     private RedisTemplate<String, Object> redisTemplate;
+    private TaskScheduler                 taskScheduler;
+
+    @PostConstruct
+    public void init() {
+    }
 
     @CacheEvict(value = "collection", allEntries = true)
     public void clearCache() {
@@ -209,11 +216,11 @@ public class CollectionService {
                 .build());
     }
 
-    @Scheduled(fixedRate = 60000)
+    @Scheduled(fixedRate = 10000)
     public void scheduleOnShelf() {
         List<Collection> collections = collectionRepo.findByScheduleSaleTrueAndOnShelfFalseAndStartTimeBeforeAndDelFalse(LocalDateTime.now());
         for (Collection collection : collections) {
-            collectionRepo.setOnShelf(collection.getId(), true);
+            collectionRepo.scheduleOnShelf(collection.getId(), true);
         }
     }