1. 程式人生 > 其它 >SpringBoot整合JAVA定時任務之SpringTask

SpringBoot整合JAVA定時任務之SpringTask

技術標籤:javaspring bootjava

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

測試結果:
在這裡插入圖片描述