當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot Quartz应用
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot Quartz应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
簡單用法
配置cronSchedule的寫法
?
簡單用法
直接@EnableScheduling后,方法上加上@Scheduled(cron = "0 */1 * * * * ")就行了。
此種方式需要寫死時間、寫死實現,生產環境不方便配置控制。
?
?
@EnableScheduling @SpringBootApplication public class TestApplication {public static void main(String[] args) {SpringApplication.run(DataCheckApplication.class, args);} //30秒執行一次 @Scheduled(fixedRate = 1000 * 30)public void reportCurrentTime(){System.out.println ("Scheduling Tasks Examples: The time is now " + dateFormat ().format (new Date ()));} //每1分鐘執行一次@Scheduled(cron = "0 */1 * * * * ")public void reportCurrentByCron(){System.out.println ("Scheduling Tasks Examples By Cron: The time is now " + dateFormat ().format (new Date ()));}private SimpleDateFormat dateFormat(){return new SimpleDateFormat ("HH:mm:ss");} }?
配置cronSchedule的寫法
啟動類
?
@EnableScheduling @SpringBootApplication public class TestApplication {public static void main(String[] args) {SpringApplication.run(DataCheckApplication.class, args);}?
?
任務類
public class CheckJob implements Job {@Overridepublic void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {System.out.println ("Scheduling Tasks Examples")} }初始任務
?
public void initTask(){//1.創建Scheduler的工廠SchedulerFactory sf = new StdSchedulerFactory();try {//2.從工廠中獲取調度器實例Scheduler scheduler = sf.getScheduler();//3.啟動 調度器scheduler.start();//4.創建JobDetailJobDetail jobDetail =JobBuilder.newJob(CheckJob.class).withDescription("數據庫操作定時任務").withIdentity("jobName", "group").usingJobData("name", 1).build();//5.創建TriggerTrigger trigger = TriggerBuilder.newTrigger().withDescription("定時任務,可自定義時間執行").withIdentity("jobName", "group")//默認在當前時間啟動.startAt(new Date())//重復執行的次數.withSchedule(CronScheduleBuilder.cronSchedule("0 0/5 * * * ?")).build();//6.注冊任務和定時器scheduler.scheduleJob(jobDetail, trigger);} catch (Exception e) {e.printStackTrace();} }這樣任務就可以每隔五分鐘執行一次了。
?
總結
以上是生活随笔為你收集整理的Spring Boot Quartz应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL配置文件my.ini
- 下一篇: scrollwidth ,client