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