1、SpringTask
SpringTask是spring官方给我们推荐的一个轻量级的任务调度框架,SpringTask是不能通过程序的方式控制开启或关闭。
2、SpringTask的使用
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
@Component
public class TestTask {
@Scheduled(cron = "0/3 * * * * ?") //每三秒钟打印一次
public void test() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = simpleDateFormat.format(System.currentTimeMillis());
System.out.println("你好......"+format);
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling //SpringTask任务开启
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
测试结果: