【SpringBoot】SpringBoot2.x整合定时任务和异步任务处理
SpringBoot2.x整合定时任务和异步任务处理
一.项目环境
springboot2.x本身已经集成了定时任务模块和异步任务,可以直接使用
二.springboot常用定时任务配置
1.在启动类上使用注解@EnableScheduling开启定时任务,使用@EnableAsync开启异步任务
- @SpringBootApplication //一个注解顶下面3个
- @EnableScheduling //开启定时任务
- @EnableAsync //开启异步任务
- public class XdclassApplication {
- public static void main(String[] args) {
- SpringApplication.run(XdclassApplication.class, args);
- }
- }
2.使用注解@Schedule声明这是一个定时任务,Springboot启动会扫描到该注解并标记为定时任务
- @Component
- public class TestTask {
- @Scheduled(cron="*/1 * * * * *")
- public void sum2(){
- System.out.println("cron 每秒 当前时间:"+new Date());
- }
- }
3.使用@Asyn声明这是一个异步任务的方法,如果在类上使用该注解,则该类下所有方法都是异步任务的方法
- @Component
- @Async
- public class AsyncTask {
- public void task1() throws InterruptedException{
- long begin = System.currentTimeMillis();
- Thread.sleep(1000L);
- long end = System.currentTimeMillis();
- System.out.println("任务1耗时="+(end-begin));
- }
- public void task2() throws InterruptedException{
- long begin = System.currentTimeMillis();
- Thread.sleep(2000L);
- long end = System.currentTimeMillis();
- System.out.println("任务2耗时="+(end-begin));
- }
- public void task3() throws InterruptedException{
- long begin = System.currentTimeMillis();
- Thread.sleep(3000L);
- long end = System.currentTimeMillis();
- System.out.println("任务3耗时="+(end-begin));
- }
- //获取异步结果
- 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");
- }
- }
测试
- @RestController
- @RequestMapping("/api/v1")
- public class UserController {
- @Autowired
- private AsyncTask task;
- @GetMapping("async_task")
- public JsonData exeTask() throws InterruptedException{
- long begin = System.currentTimeMillis();
- // task.task1();
- // task.task2();
- // task.task3();
- Future<String> task4 = task.task4();
- Future<String> task5 = task.task5();
- Future<String> task6 = task.task6();
- //这里死循环让主线程挂起,目的是为了计算其他异步任务的执行任务的耗时
- 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);
- }
- }
【SpringBoot】SpringBoot2.x整合定时任务和异步任务处理的更多相关文章
- SpringBoot2.x整合定时任务和异步任务处理
SpringBoot2.x整合定时任务和异步任务处理 一.项目环境 springboot2.x本身已经集成了定时任务模块和异步任务,可以直接使用 二.springboot常用定时任务配置 1.在启动类 ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解
笔记 1.SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- SpringBoot整合定时任务和异步任务处理 3节课
1.SpringBoot定时任务schedule讲解 定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- SpringBoot整合定时任务和异步任务处理
SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题, ...
- 【SpringBoot】整合定时任务和异步任务
========================10.SpringBoot整合定时任务和异步任务处理 =============================== 1.SpringBoot定时任务s ...
- SpringBoot整合全局异常处理&SpringBoot整合定时任务Task&SpringBoot整合异步任务
============整合全局异常=========== 1.整合web访问的全局异常 如果不做全局异常处理直接访问如果报错,页面会报错500错误,对于界面的显示非常不友好,因此需要做处理. 全局异 ...
- 【SpringBoot】息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ
========================13.消息队列介绍和SpringBoot2.x整合RockketMQ.ActiveMQ ======================= 1.JMS介绍和 ...
- 【SpringBoot】SpringBoot2.x整合Shiro(一)
一:什么是ACL和RBAC: ACL: Access Control List 访问控制列表 以前盛行的一种权限设计,它的核心在于用户直接和权限挂钩 优点:简单易用,开发便捷 缺点:用户和权限直接挂钩 ...
- SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)
一.使用场景 定时任务在开发中还是比较常见的,比如:定时发送邮件,定时发送信息,定时更新资源,定时更新数据等等... 二.准备工作 在Spring Boot程序中不需要引入其他Maven依赖 (因为s ...
随机推荐
- 微信小程序和H5之间相互跳转
1.微信小程序跳转小程序 wx.navigateToMiniProgram <script src='https://res.wx.qq.com/open/js/jweixin-1.3.0.js ...
- 上线 Python 应用仅需一条命令的开源框架:Zappa(详细教程)
本文面向有 Python Web 基础的小伙伴 作者:HelloGitHub-吱吱 这里是 HelloGitHub 推出的<讲解开源项目>系列,今天要向小伙伴们介绍一个 Python 无服 ...
- 如何安装jenkins并简单的使用
如何安装jenkins并使用 一.jenkins 简介: Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,功能包括 : 1.持续的软件版本发布/测试项目: 2.监控外部调用 ...
- XUPT-D
/* 泰泰学长又来玩数字了,泰泰学长想让你帮他求1-n的和,但是这次的求和可不是简单的1+2+...+n. 这次的求和是这样的,如果加到一个数字是2的指数倍,那就不加,反而减掉这个数. ...
- 《进击吧!Blazor!》系列入门教程 第一章 7.图表
<进击吧!Blazor!>是本人与张善友老师合作的Blazor零基础入门教程视频,此教程能让一个从未接触过Blazor的程序员掌握开发Blazor应用的能力. 视频地址:https://s ...
- POJ_2752 Seek the Name, Seek the Fame 【KMP】
一.题目 POJ2752 二.分析 比较明显的KMP运用. 但是这题不是只找一个,仔细看题后可以发现相当于是在找到最大的满足条件的后缀后,再在这个后缀里面找满足条件的后缀. 可以不断的运用KMP得出答 ...
- Codeforces 976C Nested Segments
题面: 传送门 C. Nested Segments Input file: standard input Output file: standard output Time limit: 2 secon ...
- 2019 GDUT Rating Contest I : Problem B. Teamwork
题面: 传送门 B. Teamwork Input file: standard input Output file: standard output Time limit: 1 second Memor ...
- 正则表达式-Python实现
1.概述: Regular Expression.缩写regex,regexp,R等: 正则表达式是文本处理极为重要的工具.用它可以对字符串按照某种规则进行检索,替换. Shell编程和高级编程语言中 ...
- IPFS挖矿赚钱吗?IPFS挖矿是真的吗?
IPFS一出现就获得了极高的关注度,「让人类信息永存」的口号也让其蒙上了一层神秘的面纱.今天我就来给大家自剖析,一探IPFS技术的真相. IPFS是一个去中心化存储网络,而Filecoin是IPFS激 ...