老蒋的知识库

  • 首页
  • 文章归档
  • 关于页面

  • 搜索

Java Spring 定时任务

发表于 2023-11-02 | 分类于 Java | 0 | 阅读次数 58

添加

@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");
    }

}

  • 本文作者: jagger
  • 本文链接: /archives/javaspring-ding-shi-ren-wu
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!
Nginx 防御
Minio 文件系统部署与使用
jagger

jagger

66 日志
31 分类
0 标签
Creative Commons
0%
© 2026 jagger
由 Halo 强力驱动