添加
@EnableScheduling // 在spring容器中启用定时任务的执行
public class GameApplication {
public static void main(String[] args) {
SpringApplication.run(GameApplication.class, args);
}
}
编写定时任务类,通过注解@Scheduled设定触发时间
import com.alibaba.fastjson2.JSONArray;
import com.douyin.game.dto.DouyinMsgDto;
import com.douyin.game.service.DouyinApiService;
import com.douyin.game.service.DouyinGameCacheService;
import com.ruoyi.common.core.constant.DouyinConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Slf4j
// 定时任务 @Scheduled 不能与 @RefreshScope 同时使用,当属性刷新时会导致定时任务失效(可以解决:需要触发刷新的时候重新装载定时任务)
// @RefreshScope
public class DouyinTask {
/**
* cron配置每30分钟执行一次
*/
@Scheduled(cron = "* */30 * * * ?")
// 上一次任务开始触发,进入倒计时5000毫秒后再次触发任务
// @Scheduled(fixedRate = 5000)
// 上一次任务执行完毕,进入倒计时5000毫秒后再次触发任务
// @Scheduled(fixedDelay = 5000)
public void updateAccessToken() {
log.info("定时任务:更新access_token");
}
}