SpringBoot-2.X 學習筆記03 異步任務與定時任務
在線工具 在線工具
1 定時任務
1.1 在 SprinBoot 啓動類中添加 @EnableScheduling() 註解。
package com.xu.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableScheduling()//定時任務
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
1.2 新建一個定時任務類並添加 @Component 註解,在方法上添加@Scheduled() 註解。
package com.xu.springboot.task;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @Author: hyacinth
* @ClassName: SendEmail
* @Description: TODO
* @Date: 2019年8月11日 下午9:11:38
* @Copyright: hyacinth
*/
@Component
public class SchedulTask{
/**
*
* @Autdor: hyacintd
* @Title: scheduling
* @Description: SprinBoot-2.* 定時任務
* @return void
* <table border="1" cellpadding="10">
* <tr><th colspan="2" align="center">方法一 cornexpression</th></tr>
* <tr><td align="center">符號</td><td align="center">解釋</td></tr>
* <tr><td align="center">*</td><td align="left">表示所有值,在分鐘裏表示每一分鐘觸發。在小時,日期,月份等裏面表示每一小時,每一日,每一月。</td></tr>
* <tr><td align="center">?</td><td align="left">表示不指定值,表示不關心當前位置設置的值。比如不關心是周幾。則周的位置填寫?。主要是由於日期跟周是有重複的所以兩者必須有一者設置爲?。</td></tr>
* <tr><td align="center">-</td><td align="left">表示區間,小時設置爲10-12表示10,11,12點均會觸發。</td></tr>
* <tr><td align="center">,</td><td align="left">表示多個值,小時設置成10,12表示10點和12點會觸發。</td></tr>
* <tr><td align="center">/</td><td align="left">表示遞增觸發,5/15表示從第5秒開始,每隔15秒觸發。</td></tr>
* <tr><td align="center">L</td><td align="left">表示最後的意思,日上表示最後一天,星期上表示星期六或7,L前加數據,表示該數據的最後一個,星期上設置6L表示最後一個星期五。6表示星期五。</td></tr>
* <tr><td align="center">W</td><td align="left">表示離指定日期最近的工作日觸發,15W離該月15號最近的工作日觸發。</td></tr>
* <tr><td align="center">#</td><td align="left">表示每月的第幾個周幾,6#3表示該月的第三個週五。</td></tr>
* <tr><th colspan="2" align="center">方法一例子</th></tr>
* <tr><td align="center">@Scheduled(cron = "0 0 12 * * ?")</td><td align="left">每天中午12點執行</td></tr>
* <tr><td align="center">@Scheduled(cron = "0 30 10 L * ?")</td><td align="left">每月最後一日的上午10:30執行</td></tr>
* <tr><th colspan="2" align="center">方法二 Rate/Delay (毫秒值)</th></tr>
* <tr><td align="center">參數名稱</td><td align="center">參數解釋</td></tr>
* <tr><td align="center">fixedRate</td><td align="left">上一次開始執行時間點後每隔*毫秒執行一次。</td></tr>
* <tr><td align="center">fixedDelay</td><td align="left">上一次開始執行時間點後每隔*毫秒執行一次。</td></tr>
* <tr><th colspan="2" align="center">方法二例子</th></tr>
* <tr><td align="center">@Scheduled(fixedRate = 6000)</td><td align="left">上一次開始執行時間點後每隔6秒執行一次。</td></tr>
* <tr><td align="center">@Scheduled(fixedDelay = 6000)</td><td align="left">上一次執行完畢時間點之後6秒再執行。</td></tr>
* <tr><td align="center">@Scheduled(initialDelay=1000, fixedRate=6000)</td><td align="left">第一次延遲1秒後執行,之後按fixedRate的規則每6秒執行一次。</td></tr>
* <tr><td align="center">幫助</td><td align="left">可以查看(https://www.cnblogs.com/lilinzhiyu/p/8108538.html)或者crontab工具(https://tool.lu/crontab/)</td></tr>
* @date: 2019年8月11日 下午9:28:03
*/
@Scheduled(cron = "0 0 12 * * ?")
public void scheduling() {
System.out.println(new Date());
}
}
2 異步任務
2.1 在 SprinBoot 啓動類中添加 @EnableAsync 註解。
package com.xu.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@EnableAsync//開啓異步任務
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2.2 新建一個定時任務類並添加 @Component 註解,在方法或整個類上添加@Async 註解。
package com.xu.springboot.task;
import java.util.concurrent.Future;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;
/**
* @Author: hyacinth
* @ClassName: AsyncTask
* @Description: TODO
* @Date: 2019年8月11日 下午9:29:47
* @Copyright: hyacinth
*/
@Component
@Async
public class AsyncTask {
/**
*
* @Author: hyacinth
* @Title: test1
* @Description: TODO
* @throws Exception
* @return void
* @date: 2019年8月11日 下午10:38:37
*/
public void test1() throws Exception {
long b=System.currentTimeMillis();
Thread.sleep(2000L);
long e=System.currentTimeMillis();
System.out.println("運行耗時:"+(e-b));
}
/**
*
* @Author: hyacinth
* @Title: test2
* @Description: TODO
* @throws Exception
* @return Future<String> 返回執行結果
* @date: 2019年8月11日 下午10:38:14
*/
public Future<String> test2() throws Exception {
long b=System.currentTimeMillis();
Thread.sleep(4000L);
long e=System.currentTimeMillis();
System.out.println("運行耗時:"+(e-b));
return new AsyncResult<String>("運行耗時:"+(e-b));
}
}