1:在启动类上写@EnableScheduling注解java
2:在要定时任务的类上写@component web
3:在要定时执行的方法上写@Scheduled(fixedRate=毫秒数)。spring
主类api
@SpringBootApplication @EnableScheduling //开启定时任务 public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } }
定时任务类app
import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class Jobs { //表示方法执行完成后5秒 @Scheduled(fixedDelay=5000) public void fixedDelayJob() throws InterruptedException{ System.out.println("fixedDelay 每隔5秒"+new Date()); } //表示每隔3秒 @Scheduled(fixedRate=3000) public void fixedRateJob(){ System.out.println("fixedRate 每隔3秒"+new Date()); } //表示天天8时30分0秒执行 @Scheduled(cron="0 0,30 0,8 ? * ? ")
public void cronJob(){ System.out.println(new Date()+" ...>>cron...."); } }
效果:异步
1.fixedDelay和fixedRate,单位是毫秒,这里这里就是5秒和3秒,它们的区别就是:async
,fixedRate就是每屡次分钟一次,不论你业务执行花费了多少时间。我都是1分钟执行1次,而fixedDelay是当任务执行完毕后1分钟在执行。因此根据实际业务不一样,咱们会选择不一样的方式。ui
2.cron表达式:好比你要设置天天何时执行,就能够用它spa
cron表达式,有专门的语法,并且感受有点绕人,不过简单来讲,你们记住一些经常使用的用法便可,特殊的语法能够单独去查。
cron一共有7位,可是最后一位是年,能够留空,因此咱们能够写6位:3d
* 第一位,表示秒,取值0-59 * 第二位,表示分,取值0-59 * 第三位,表示小时,取值0-23 * 第四位,日期天/日,取值1-31 * 第五位,日期月份,取值1-12 * 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思 另外:1表示星期天,2表示星期一。 * 第7为,年份,能够留空,取值1970-2099
cron中,还有一些特殊的符号,含义以下:
(*)星号:能够理解为每的意思,每秒,每分,天天,每个月,每一年...
(?)问号:问号只能出如今日期和星期这两个位置。
(-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12
(,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期一,星期二,星期四
(/)斜杠:如:x/y,x是开始值,y是步长,好比在第一位(秒) 0/15就是,从0秒开始,每15秒,最后就是0,15,30,45,60 另:*/y,等同于0/y
下面列举几个例子供你们来验证:
0 0 3 * * ? 天天3点执行 0 5 3 * * ? 天天3点5分执行 0 5 3 ? * * 天天3点5分执行,与上面做用相同 0 5/10 3 * * ? 天天3点的 5分,15分,25分,35分,45分,55分这几个时间点执行 0 10 3 ? * 1 每周星期天,3点10分 执行,注:1表示星期天 0 10 3 ? * 1#3 每月的第三个星期,星期天 执行,#号只能出如今星期的位置
1: 启动类里面使用@EnableAsync注解开启功能,自动扫描
2:在要异步任务的类上写@component
3:在定义异步任务类写@Async(写在类上表明整个类都是异步,在方法加上表明该类异步执行)
主类
@SpringBootApplication @EnableAsync //开启异步任务 public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } }
异步类
import java.util.concurrent.Future; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Component; /** * 功能描述:异步任务业务类 */ @Component @Async public class AsyncTask { //获取异步结果 public Future<String> task4() throws InterruptedException{ long begin = System.currentTimeMillis(); Thread.sleep(2000L); long end = System.currentTimeMillis(); System.out.println("任务4耗时="+(end-begin)); return new AsyncResult<String>("任务4"); } public Future<String> task5() throws InterruptedException{ long begin = System.currentTimeMillis(); Thread.sleep(3000L); long end = System.currentTimeMillis(); System.out.println("任务5耗时="+(end-begin)); return new AsyncResult<String>("任务5"); } public Future<String> task6() throws InterruptedException{ long begin = System.currentTimeMillis(); Thread.sleep(1000L); long end = System.currentTimeMillis(); System.out.println("任务6耗时="+(end-begin)); return new AsyncResult<String>("任务6"); } }
controller类
import java.util.concurrent.Future; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.jincou.task.AsyncTask; import com.jincou.util.JsonData; @RestController @RequestMapping("/api/v1") public class UserController { @Autowired private AsyncTask task; @GetMapping("async_task") public JsonData exeTask() throws InterruptedException{ long begin = System.currentTimeMillis(); Future<String> task4 = task.task4(); Future<String> task5 = task.task5(); Future<String> task6 = task.task6(); //若是都执行往就能够跳出循环,isDone方法若是此任务完成,true for(;;){ if (task4.isDone() && task5.isDone() && task6.isDone()) { break; } } long end = System.currentTimeMillis(); long total = end-begin; System.out.println("执行总耗时="+total); return JsonData.buildSuccess(total); } }
结果:
页面:
后台:
从上面示例咱们能够看出:若是同步方法,那咱们须要6秒,而异步执行,咱们只须要3秒左右,这就是异步的做用。
1)要把异步任务封装到类里面,不能直接写到Controller
2)增长Future<String> 返回结果 AsyncResult<String>("task执行完成");
3)若是须要拿到结果 须要判断所有的 task.isDone()
想太多,作太少,中间的落差就是烦恼。想没有烦恼,要么别想,要么多作。上尉【10】