房间停留可获得礼物-定时任务-先去掉有问题的定时任务

This commit is contained in:
2022-12-21 22:11:26 +08:00
parent b401c4049d
commit c05c750ded

View File

@@ -1,79 +0,0 @@
package com.accompany.scheduler.task.room;
import com.accompany.business.dto.room.RoomFreeGiftConfigDto;
import com.accompany.business.service.room.RoomFreeGiftService;
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.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 ApplicationListener<EnvironmentChangeEvent>,CommandLineRunner {
@Autowired
private RoomFreeGiftService roomFreeGiftService;
@Autowired
private TaskScheduler taskScheduler;
private volatile ScheduledFuture<?> taskFuture;
/**
* 业务触发器
* @return
*/
private Trigger getTrigger() {
return triggerContext -> {
RoomFreeGiftConfigDto config = roomFreeGiftService.getConfig();
// 触发器
CronTrigger trigger = new CronTrigger(config.getResetTimeCron());
return trigger.nextExecutionTime(triggerContext);
};
}
/**
* 业务执行方法
* @return
*/
private Runnable doTask() {
return () -> {
roomFreeGiftService.resetFreeGift();
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();
}
}