常见定时任务区别
1、常见定时任务 Java自带的java.util.Timer类
timer:配置比较麻烦,时间延后问题
timertask:不推荐
2、Quartz框架
配置更简单
xml或者注解
3、SpringBoot使用注解方式开启定时任务
1)启动类里面 @EnableScheduling开启定时任务,自动扫描
package net.myclass;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@MapperScan("net.myclass.mapper")
@EnableScheduling
public class MyApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(MyApplication.class, args);
}
}
2)定时任务业务类 加注解 @Component被容器扫描
3)定时执行的方法加上注解 @Scheduled(fixedRate=2000) 定期执行一次
SpringBoot常用定时任务表达式配置和在线生成器
1、cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒
crontab 工具 https://tool.lu/crontab/
2、fixedRate: 定时多久执行一次(上一次开始执行时间点后xx秒再次执行;)
3、fixedDelay: 上一次执行结束时间点后xx秒再次执行
4、fixedDelayString: 字符串形式,可以通过配置文件指定
package net.myclass.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class TestTask {
@Scheduled(fixedRate = 2000)//两秒执行一次
public void sum(){
System.out.println("当前时间"+new Date());
}
}
SpringBoot2.x异步任务
用订单/库存系统举例,几千人都在买买买,一时间几千个订单请求到controller,然后调用service(注:service还是跟上面一样要几十秒),一到service,判断是个异步方法,于是赶紧让处理异步任务的线程过来慢慢处理就好,controller可以直接响应用户“订单成功”。用户极短时间就收到响应,于是可以继续买买买。
异步:它会把需要执行的放在一个执行队列中,像是这种需要等待的程序,他在会在执行了之后将他排在后面,然后执行其他的任务,指导它等待结束之后,执行它的回调。
ajax请求,在这样的请求中会有较大的时间开销,但是这一过程中并没有太大的资源消耗,或者说对于javascript所执行的环境来说几乎没有什么资源的消耗。所以在这样的过程中,其实主线程是空闲着的,如果不是异步的行为,那它所做的只有一件事,那就是等待,等待请求的响应,那么,其实在这样的一个过程中,完全可以把主线程在等待时候的资源开放出来进行其他的操作,这就形成了异步。
1、使用场景:适用于处理log、发送邮件、短信……等
下单接口->查库存 100
余额校验 150
风控用户100
....
2、启动类里面使用@EnableAsync注解开启功能,自动扫描
package net.myclass;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@MapperScan("net.myclass.mapper")
@EnableScheduling
@EnableAsync
public class MyApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(MyApplication.class, args);
}
}
3、定义异步任务类并使用@Component标记组件被容器扫描,异步方法加上@Async
package net.myclass.task;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class AsyncTask {
@Async
public void task1() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(1000L);
long end = System.currentTimeMillis();
System.out.println("任务1耗时="+(end-begin));
}
@Async
public void task2() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(2000L);
long end = System.currentTimeMillis();
System.out.println("任务2耗时="+(end-begin));
}
@Async
public void task3() throws InterruptedException{
long begin = System.currentTimeMillis();
Thread.sleep(3000L);
long end = System.currentTimeMillis();
System.out.println("任务3耗时="+(end-begin));
}
}
注意点:
1)要把异步任务封装到类里面,不能直接写到Controller
2)增加Future<String> 返回结果 AsyncResult<String>("task执行完成");
3)如果需要拿到结果 需要判断全部的 task.isDone()
4、通过注入方式,注入到controller里面,如果测试前后区别则改为同步则把Async注释掉
package net.myclass.controller;
import net.myclass.domain.JsonData;
import net.myclass.task.AsyncTask;
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;
@RestController
@RequestMapping("/api3/async")
public class AsyncController {
@Autowired
private AsyncTask task;
@GetMapping("async_task")
public JsonData exeTask() throws InterruptedException{
long begin=System.currentTimeMillis();
task.task1();
task.task2();
task.task3();
long end =System.currentTimeMillis();
long total=end-begin;
System.out.println("执行时间="+total);
return JsonData.buildSuccess(total);
}
}