From e280769bae9f0b22cae43e01ea4fde9ffa0a7241 Mon Sep 17 00:00:00 2001 From: yeungchihang <842328916@qq.com> Date: Wed, 21 Dec 2022 17:17:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=88=BF=E9=97=B4=E5=81=9C=E7=95=99=E5=8F=AF?= =?UTF-8?q?=E8=8E=B7=E5=BE=97=E7=A4=BC=E7=89=A9-=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1-=E7=9B=91=E5=90=AC=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=87=8D=E7=BD=AE=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../scheduler/task/room/RoomFreeGiftTask.java | 50 ++++++++++++++----- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/accompany-scheduler/accompany-scheduler-service/src/main/java/com/accompany/scheduler/task/room/RoomFreeGiftTask.java b/accompany-scheduler/accompany-scheduler-service/src/main/java/com/accompany/scheduler/task/room/RoomFreeGiftTask.java index c4ae47cfd..7d27e2028 100644 --- a/accompany-scheduler/accompany-scheduler-service/src/main/java/com/accompany/scheduler/task/room/RoomFreeGiftTask.java +++ b/accompany-scheduler/accompany-scheduler-service/src/main/java/com/accompany/scheduler/task/room/RoomFreeGiftTask.java @@ -2,27 +2,30 @@ package com.accompany.scheduler.task.room; import com.accompany.business.dto.room.RoomFreeGiftConfigDto; import com.accompany.business.service.room.RoomFreeGiftService; -import com.accompany.common.status.BusiStatus; -import com.accompany.core.exception.ServiceException; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.Trigger; -import org.springframework.scheduling.annotation.SchedulingConfigurer; -import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; + +import java.util.Set; +import java.util.concurrent.ScheduledFuture; @Slf4j @Component -public class RoomFreeGiftTask implements SchedulingConfigurer { +public class RoomFreeGiftTask implements ApplicationListener,CommandLineRunner { @Autowired private RoomFreeGiftService roomFreeGiftService; + @Autowired + private TaskScheduler taskScheduler; - @Override - public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { - taskRegistrar.addTriggerTask(doTask(), getTrigger()); - } + private volatile ScheduledFuture taskFuture; /** * 业务触发器 @@ -31,9 +34,6 @@ public class RoomFreeGiftTask implements SchedulingConfigurer { private Trigger getTrigger() { return triggerContext -> { RoomFreeGiftConfigDto config = roomFreeGiftService.getConfig(); - if (null == config){ - throw new ServiceException(BusiStatus.PARAMERROR); - } // 触发器 CronTrigger trigger = new CronTrigger(config.getResetTimeCron()); return trigger.nextExecutionTime(triggerContext); @@ -50,4 +50,30 @@ public class RoomFreeGiftTask implements SchedulingConfigurer { roomFreeGiftService.sendResetChatRoomMsg(); }; } + + private void fresh(){ + RoomFreeGiftConfigDto config = roomFreeGiftService.getConfig(); + boolean hasConfig = null != config; + log.info("[房间免费礼物] 获取配置 {}", hasConfig); + if (hasConfig){ + if (null != taskFuture){ + taskFuture.cancel(true); + } + taskFuture = taskScheduler.schedule(doTask(), getTrigger()); + log.info("[房间免费礼物] 开始重置定时任务 {}", config.getResetTimeCron()); + } + } + + @Override + public void onApplicationEvent(EnvironmentChangeEvent event) { + Set keys = event.getKeys(); + if (!CollectionUtils.isEmpty(keys) && keys.stream().anyMatch(key->key.startsWith("sysconf"))){ + fresh(); + } + } + + @Override + public void run(String... args) throws Exception { + fresh(); + } }