springBoot整合定时任务和异步任务处理

释放双眼,带上耳机,听听看~!

1 springboot 定时任务schedule

简介:
1,常见定时任务 Java自带的java.util.timer类
timer:配置比较麻烦,时间延后问题
timertask:不推荐
2,Quartz框架 (ssm框架使用)
配置更简单
xml或者注解
3,Springboot使用注解方式 (springboot使用)

  1. 启动类里面 @EnableScheduling 开启定时任务,自动扫描
  2. 定时任务业务类 加注解 @Component 被容器扫描
  3. 定时执行的方法加上注解 @Scheduled(fixedRate = 2000) 定期(每两秒)执行一次

启动类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.scheduling.annotation.EnableScheduling;
4
5@SpringBootApplication
6@EnableScheduling  //开启定时任务
7public class ScheduleApplication {
8
9   public static void main(String[] args) {
10      SpringApplication.run(ScheduleApplication.class, args);
11  }
12
13}
14
15
16

定时任务业务类


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1import org.springframework.scheduling.annotation.Scheduled;
2import org.springframework.stereotype.Component;
3
4import java.util.Date;
5
6@Component
7public class TestTask {
8
9    @Scheduled(fixedRate = 2000) //两秒执行一次
10    public void sum(){
11        System.out.println("当前时间"+new Date());
12
13    }
14}
15
16

打印结果


1
2
3
4
5
6
7
8
9
10
11
1当前时间Tue Jan 07 09:16:20 CST 2020
2当前时间Tue Jan 07 09:16:22 CST 2020
3当前时间Tue Jan 07 09:16:24 CST 2020
4当前时间Tue Jan 07 09:16:26 CST 2020
5当前时间Tue Jan 07 09:16:28 CST 2020
6当前时间Tue Jan 07 09:16:30 CST 2020
7当前时间Tue Jan 07 09:16:32 CST 2020
8当前时间Tue Jan 07 09:16:34 CST 2020
9当前时间Tue Jan 07 09:16:36 CST 2020
10
11

常用定时任务配置
常用定时任务表达式配置和在线生成器
1,cron定时任务表达式@Scheduled(cron="*/1*****")表示每秒
ps:crontab 参考工具 https://tool.lu/crontab/
2,fixedRate:定时多久执行一次(上一次开始执行时间点后xx秒再次执行)
3,fixedDelay:定时多久执行一次(上一次执行结束时间点后xx秒再次执行)
4,fixedDelayString:字符串形式,可以通过配置文件指定

springboot异步任务实战
1.什么是异步任务和使用场景:适用于处理log,发送邮件,短信…等
下单接口->查询库存 100
余额校验 150
风控用户 100

2. 启动类里面使用@EnabledAsync注解开启功能,自动扫描
3. 定义异步任务类并使用@Component标记组件被容器扫描,异步方法加上@Async
注意点:

  1. 要把异步任务封装到类里面,不能直接写到controller
  2. 增加Future<String>返回结果 AsyncResult<String>(“task执行完成”)
  3. 如果需要拿到结果,需要判断全部的 task.isDone()
  1. 通过注入方式,注入到controller里面,如果测试前后区别则改为同步,则把Async注释掉


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
1import org.springframework.scheduling.annotation.Async;
2import org.springframework.scheduling.annotation.AsyncResult;
3import org.springframework.stereotype.Component;
4
5import java.util.concurrent.Future;
6
7/**
8 * 异步任务业务类
9 */
10@Component
11@Async
12public class AsyncTask {
13
14    public void task1() throws InterruptedException {
15        long begin = System.currentTimeMillis();
16        Thread.sleep(1000L);
17        long end = System.currentTimeMillis();
18        System.out.println(&quot;任务1耗时=&quot; + (end - begin));
19    }
20
21    public void task2() throws InterruptedException {
22        long begin = System.currentTimeMillis();
23        Thread.sleep(2000L);
24        long end = System.currentTimeMillis();
25        System.out.println(&quot;任务2耗时=&quot; + (end - begin));
26    }
27
28    public void task3() throws InterruptedException {
29        long begin = System.currentTimeMillis();
30        Thread.sleep(3000L);
31        long end = System.currentTimeMillis();
32        System.out.println(&quot;任务3耗时=&quot; + (end - begin));
33    }
34
35    //获取异步结果
36    public Future&lt;String&gt; task4() throws InterruptedException {
37        long begin = System.currentTimeMillis();
38        Thread.sleep(1000L);
39        long end = System.currentTimeMillis();
40        System.out.println(&quot;任务4耗时=&quot; + (end - begin));
41        return new  AsyncResult&lt;&gt;(&quot;任务4&quot;);
42    }
43    public Future&lt;String&gt; task5() throws InterruptedException {
44        long begin = System.currentTimeMillis();
45        Thread.sleep(1000L);
46        long end = System.currentTimeMillis();
47        System.out.println(&quot;任务5耗时=&quot; + (end - begin));
48        return new  AsyncResult&lt;&gt;(&quot;任务5&quot;);
49    }
50    public Future&lt;String&gt; task6() throws InterruptedException {
51        long begin = System.currentTimeMillis();
52        Thread.sleep(1000L);
53        long end = System.currentTimeMillis();
54        System.out.println(&quot;任务6耗时=&quot; + (end - begin));
55        return new  AsyncResult&lt;&gt;(&quot;任务6&quot;);
56    }
57}
58
59
60

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
1
2import com.javagirl.schedule.task.AsyncTask;
3import org.springframework.beans.factory.annotation.Autowired;
4import org.springframework.web.bind.annotation.GetMapping;
5import org.springframework.web.bind.annotation.RequestMapping;
6import org.springframework.web.bind.annotation.RestController;
7
8import java.util.HashMap;
9import java.util.Map;
10import java.util.concurrent.Future;
11
12@RestController
13@RequestMapping(&quot;/api/v1&quot;)
14public class UserController {
15
16
17    @Autowired
18    AsyncTask task;
19
20    @GetMapping(&quot;async_task&quot;)
21    public Map exeTask() {
22        Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
23
24        Map&lt;String, Object&gt; map1 = new HashMap&lt;&gt;();
25        long begin = System.currentTimeMillis();
26        try {
27            task.task1();
28            task.task2();
29            task.task3();
30            map1.put(&quot;result&quot;, &quot;success&quot;);
31        } catch (InterruptedException e) {
32            map1.put(&quot;result&quot;, e.getMessage());
33            e.printStackTrace();
34        }
35        long end = System.currentTimeMillis();
36        long total = end - begin;
37        System.out.println(&quot;map1执行总耗时=&quot; + total);
38        map1.put(&quot;map1执行总耗时&quot;, total);
39
40        Map&lt;String, Object&gt; map2 = new HashMap&lt;&gt;();
41        try {
42            long begin1 = System.currentTimeMillis();
43            Future&lt;String&gt; task4 = task.task4();
44            Future&lt;String&gt; task5 = task.task5();
45            Future&lt;String&gt; task6 = task.task6();
46            for (; ; ) {
47                if (task4.isDone() &amp;&amp; task5.isDone() &amp;&amp; task6.isDone()) {
48                    break;
49                }
50            }
51            long end1 = System.currentTimeMillis();
52            long totaol = end1 - begin1;
53            System.out.println(&quot;map2总耗时=&quot; + totaol);
54            map2.put(&quot;map2执行总耗时&quot;, totaol);
55            map2.put(&quot;result&quot;, &quot;success&quot;);
56        } catch (InterruptedException e) {
57            map2.put(&quot;result&quot;, e.getMessage());
58            e.printStackTrace();
59        }
60
61        map.put(&quot;map1&quot;, map1);
62        map.put(&quot;map2&quot;, map2);
63        return map;
64    }
65}
66
67

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1结果
2{
3    &quot;map2&quot;:{
4        &quot;result&quot;:&quot;success&quot;,
5        &quot;map2执行总耗时&quot;:1006
6    },
7    &quot;map1&quot;:{
8        &quot;result&quot;:&quot;success&quot;,
9        &quot;map1执行总耗时&quot;:4
10    }
11}
12
13//控制台
14map1执行总耗时=4
15任务6耗时=1000
16任务1耗时=1000
17任务5耗时=1000
18任务4耗时=1000
19map2总耗时=1006
20任务2耗时=2000
21任务3耗时=3000
22
23
24

//spring cron 常用实例
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 0 12 ?
* WED 表示每个星期三中午12点
“0 0 12 * * ?” 每天中午12点触发
“0 15 10 ? * *” 每天上午10:15触发
“0 15 10 * * ?” 每天上午10:15触发
“0 15 10 * * ? *” 每天上午10:15触发
“0 15 10 * * ? 2005” 2005年的每天上午10:15触发
“0 * 14 * * ?” 在每天下午2点到下午2:59期间的每1分钟触发
“0 0/5 14 * * ?” 在每天下午2点到下午2:55期间的每5分钟触发
“0 0/5 14,18 * * ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
“0 0-5 14 * * ?” 在每天下午2点到下午2:05期间的每1分钟触发
“0 10,44 14 ? 3 WED” 每年三月的星期三的下午2:10和2:44触发
“0 15 10 ? * MON-FRI” 周一至周五的上午10:15触发
“0 15 10 15 * ?” 每月15日上午10:15触发
“0 15 10 L * ?” 每月最后一日的上午10:15触发
“0 15 10 ? * 6L” 每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6L 2002-2005” 2002年至2005年的每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6#3” 每月的第三个星期五上午10:15触发

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++中引用和指针的区别

2022-1-11 12:36:11

安全经验

spring-oauth-server 0.5 发布,OAuth2 与 Spring Security 安全应用整合

2016-6-2 11:12:22

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索