当前位置 : 主页 > 编程语言 > java >

spring boot启动定时任务

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt package com.example.demo;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateF
gistfile1.txt
package com.example.demo;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 * Created by Administrator on 2017/11/25 0025.
 */
@Component//将MyTask对象注入spring容器
@EnableScheduling//启动定时任务
public class MyTask {

    @Scheduled(cron = "0/5 * * * * *")//通过cron表达式定义任务每5秒执行一次
    public void task(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        System.out.println("定时任务:"+simpleDateFormat.format(new Date()));
    }
}
网友评论