Kaynağa Gözat

异步线程池

zuy 2 yıl önce
ebeveyn
işleme
305d32208e

+ 2 - 0
admin/src/main/java/com/kym/admin/AdminApplication.java

@@ -5,11 +5,13 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.FilterType;
+import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 
 
+@EnableAsync
 @Controller
 @EnableScheduling
 @SpringBootApplication

+ 26 - 0
admin/src/main/java/com/kym/admin/config/AsyncConfig.java

@@ -0,0 +1,26 @@
+package com.kym.admin.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.ThreadPoolExecutor;
+
+
+@Configuration
+public class AsyncConfig {
+
+    @Bean("AsyncExecutor")
+    public Executor customAsyncExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        executor.setCorePoolSize(3);
+        executor.setMaxPoolSize(20);
+        executor.setQueueCapacity(1<<10);
+        executor.setKeepAliveSeconds(60);
+        executor.setThreadNamePrefix("async-exec-");
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
+        executor.initialize();
+        return executor;
+    }
+}

+ 25 - 0
service/src/main/java/com/kym/service/admin/impl/ActivityServiceImpl.java

@@ -21,6 +21,7 @@ import com.kym.service.admin.StationService;
 import com.kym.service.miniapp.UserRechargeRightsService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
+import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -156,10 +157,34 @@ public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> i
      * @param rechargeAmount
      */
     @Override
+    @Async("AsyncExecutor")
     public void handleRechargeActivity(long userId, int rechargeAmount) {
+//        asyncHandleRechargeActivity(userId,rechargeAmount);
         executor.execute(new RechargeActivityTask(userId, rechargeAmount));
     }
 
+    @Transactional(rollbackFor = Exception.class)
+    public void asyncHandleRechargeActivity(long userId, int rechargeAmount){
+        log.info("RechargeActivityTask run....");
+        // 进行中的充值权益活动
+        var activity = lambdaQuery().eq(Activity::getDiscountType, DISCOUNT_TYPE_服务费折扣权益).eq(Activity::getStatus, Activity.STATUS_进行中).one();
+        // 充值金额,匹配到到具体的充值权益,生成用户权益
+        if (activity != null) {
+            var rechargeRights = rechargeRightsService.lambdaQuery().eq(RechargeRights::getActivityId, activity.getId())
+                    .le(RechargeRights::getAmountMin, rechargeAmount)
+                    .ge(RechargeRights::getAmountMax, rechargeAmount) // 最后一档最大值设置成10000
+                    .one();
+            if (rechargeRights != null) {
+                var userRechargeRights = new UserRechargeRights().setRightsId(rechargeRights.getId()).setUserId(userId).setRightsBalance(rechargeAmount);
+                BeanUtils.copyProperties(rechargeRights, userRechargeRights, "id");
+                // 计算有效期
+                var endTime = LocalDateTime.now().with(LocalTime.MAX).plusDays(rechargeRights.getValidity() - 1);
+                userRechargeRightsService.save(userRechargeRights.setStartTime(LocalDateTime.now()).setEndTime(endTime));
+            }
+        }
+        log.info("RechargeActivityTask run end....");
+    }
+
     /**
      * 处理充值活动
      */