房间停留可获得礼物-定时任务-监听配置更新重置任务

This commit is contained in:
2022-12-21 17:17:48 +08:00
parent 17d54d7e35
commit e280769bae

View File

@@ -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<EnvironmentChangeEvent>,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<String> keys = event.getKeys();
if (!CollectionUtils.isEmpty(keys) && keys.stream().anyMatch(key->key.startsWith("sysconf"))){
fresh();
}
}
@Override
public void run(String... args) throws Exception {
fresh();
}
}