定时器的调度(篇一)

通过@EnableScheduling注解开启对计划任务的支持:

在这里插入图片描述
定时器调度:

package com.numberone.system.job;
import com.numberone.system.domain.GlobalSupply;
import com.numberone.system.service.IGlobalSupplyService;
import com.numberone.system.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 将redis中数据进行持久化
 */

@Component
public class RedisDataPersistenceJob {
    
    

    @Autowired
    private ITaskService taskService;
    @Autowired
    private IGlobalSupplyService globalSupplyService;
    /**
     * Cron表达式是一个字符串,字符串以56个空格隔开,分为67个域,每一个域代表一个含义,
     * Cron有如下两种语法格式:
     * 1 Seconds Minutes Hours DayofMonth Month DayofWeek Year
     *                         几号           周几     
     * 2 Seconds Minutes Hours DayofMonth Month DayofWeek     [spring]
     *                         几号           周几
     *
     *      0        0       2      1        *       ?       *   表示在每月的1日的凌晨2点调整任务
     *      0       15      10      ?        *     MON-FRI       表示周一到周五每天上午10:15执行作业
     */
    //定时任务标签
    //cron指定任务计划:什么时候执行doWork方法,周期是多少
    //定时任务时间间隔:没有标准,redis数据量决定,如果数据量较大, 时间周期适当缩短。
    @Scheduled(cron = "0 0 0/1 * * ? ")
    public void doWork(){
    
    
        taskService.updateTaskUpTime();
        System.err.println(date("执行修改任务置顶时长,执行时间为: "));
    }
    public static Object date(String msg){
    
    
        Date ss = new Date();
        SimpleDateFormat format0 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        String time = format0.format(ss.getTime());//这个就是把时间戳经过处理得到期望格式的时间
        time = format0.format(ss.getTime());
        return msg + time;
    }

    @Scheduled(cron = "0 0 0 1/1 * ? ")
    public void doWork2(){
    
    
        globalSupplyService.updateGlobalSupplyByPutTime();
        System.err.println(date("执行修改全球发布时间信息,执行时间为: "));
    }

    @Scheduled(cron = "0 0 0 1/1 * ? ")
    public void doWork3(){
    
    
        globalSupplyService.updateGlobalSupplyByStatus();
        System.err.println(date("执行修改全球发布时间状态,执行时间为: "));
    }

}

猜你喜欢

转载自blog.csdn.net/sqL520lT/article/details/111644243