记:Spring boot 定时任务

https://www.jianshu.com/p/ef18af5a9c1d

@Component
public class GetIndexInfo {
    public final static long ONE_Minute =  60 * 1000;

    /*当任务执行完毕后1分钟在执行*/
    @Scheduled(fixedDelay=ONE_Minute)
    public void fixedDelayJob(){
        System.out.println("---------------1-----------------");
        System.out.println(new Date().getTime());
    }

    /*每多次分钟一次*/
    @Scheduled(fixedRate=ONE_Minute)
    public void fixedRateJob(){
        System.out.println("---------------2-----------------");
        System.out.println(new Date().getTime());
    }

    /*每周日凌晨三点执行*/
    @Scheduled(cron="0 10 3 ? * 1")
    public void cronJob(){
        System.out.println("---------------3-----------------");
        System.out.println(new Date().getTime());
    }

    synchronized static void writeFile(String filePath, JSONObject json) {
        try {
            FileWriter fw = new FileWriter(filePath, false);
            PrintWriter out = new PrintWriter(fw);
            out.write(json.toString());
            out.println();
            fw.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

你可能感兴趣的:(记:Spring boot 定时任务)