【SpringBoot】整合定时任务和异步任务
========================10、SpringBoot整合定时任务和异步任务处理 ===============================
1、SpringBoot定时任务schedule讲解
简介:讲解什么是定时任务和常见定时任务区别
1、常见定时任务 Java自带的java.util.Timer类
timer:配置比较麻烦,时间延后问题
timertask:不推荐
2、Quartz框架
配置更简单
xml或者注解
3、SpringBoot使用注解方式开启定时任务
1)启动类里面 @EnableScheduling开启定时任务,自动扫描
2)定时任务业务类 加注解 @Component被容器扫描
3)定时执行的方法加上注解 @Scheduled(fixedRate=2000) 定期执行一次
2、SpringBoot常用定时任务配置实战
简介:SpringBoot常用定时任务表达式配置和在线生成器
package net.xdclass.base_project.task; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* 功能描述:定时任务业务类
*
* <p> 创建时间:Apr 30, 2018 10:21:48 AM </p>
*
*@作者 小D课堂 小D
*/
@Component
public class TestTask { @Scheduled(fixedRate=2000) //两秒执行一次
public void sum(){
System.out.println("当前时间:"+new Date());
} }
Task
控制台
1、cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒
1)crontab 工具 https://tool.lu/crontab/
2、fixedRate: 定时多久执行一次(上一次开始执行时间点后xx秒再次执行;)
3、fixedDelay: 上一次执行结束时间点后xx秒再次执行
4、fixedDelayString: 字符串形式,可以通过配置文件指定
流程:新建任务类注册到context,通过@Scheduled注解自动执行
package net.xdclass.base_project.task; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* 功能描述:定时任务业务类
*
* <p> 创建时间:Apr 30, 2018 10:21:48 AM </p>
*
*@作者 小D课堂 小D
*/
@Component
public class TestTask { @Scheduled(fixedRateString="2000")//两秒执行一次
//@Scheduled(cron="*/2 * * * * *")
public void sum() throws InterruptedException{ Thread.sleep(4000L);
System.out.println("结束 当前时间:"+new Date()); } //@Scheduled(cron="*/1 * * * * *")
public void sum2(){
System.out.println("cron 每秒 当前时间:"+new Date());
} }
task
3、SpringBoot2.x异步任务实战(核心知识)
简介:讲解什么是异步任务,和使用SpringBoot2.x开发异步任务实战
1、什么是异步任务和使用场景:适用于处理log、发送邮件、短信……等
下单接口->查库存 100
余额校验 150
风控用户100
....
2、启动类里面使用@EnableAsync注解开启功能,自动扫描
3、定义异步任务类并使用@Component标记组件被容器扫描,异步方法加上@Async(可以注解到类)
流程:新建任务类注册到context, controller成员属性注入任务类, 成员方法中执行任务类方法
注意点:
1)要把异步任务封装到类里面,不能直接写到Controller
2)增加Future<String> 返回结果 AsyncResult<String>("task执行完成");
3)如果需要拿到结果 需要判断全部的 task.isDone()
4、通过注入方式,注入到controller里面,如果测试前后区别则改为同步则把Async注释掉
package net.xdclass.base_project.task; import java.util.concurrent.Future; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component; /**
* 功能描述:异步任务业务类
*
* <p> 创建时间:Apr 30, 2018 11:25:15 PM </p>
*
*@作者 小D课堂 小D
*/
@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");
} }
AsynTask
package net.xdclass.base_project.controller; import java.util.concurrent.Future; import net.xdclass.base_project.domain.JsonData;
import net.xdclass.base_project.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("/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);
} }
controller
异步执行,拿回结果,
【SpringBoot】整合定时任务和异步任务的更多相关文章
- SpringBoot整合定时任务和异步任务处理 3节课
1.SpringBoot定时任务schedule讲解 定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- SpringBoot整合定时任务和异步任务处理
SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题, ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解
笔记 1.SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 ...
- 【SpringBoot】SpringBoot2.x整合定时任务和异步任务处理
SpringBoot2.x整合定时任务和异步任务处理 一.项目环境 springboot2.x本身已经集成了定时任务模块和异步任务,可以直接使用 二.springboot常用定时任务配置 1.在启动类 ...
- SpringBoot2.x整合定时任务和异步任务处理
SpringBoot2.x整合定时任务和异步任务处理 一.项目环境 springboot2.x本身已经集成了定时任务模块和异步任务,可以直接使用 二.springboot常用定时任务配置 1.在启动类 ...
- SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)
一.使用场景 定时任务在开发中还是比较常见的,比如:定时发送邮件,定时发送信息,定时更新资源,定时更新数据等等... 二.准备工作 在Spring Boot程序中不需要引入其他Maven依赖 (因为s ...
- SpringBoot整合全局异常处理&SpringBoot整合定时任务Task&SpringBoot整合异步任务
============整合全局异常=========== 1.整合web访问的全局异常 如果不做全局异常处理直接访问如果报错,页面会报错500错误,对于界面的显示非常不友好,因此需要做处理. 全局异 ...
- SpringBoot整合定时任务异步任务
1.定时任务 1.开启定时任务 @SpringBootApplication //开启定时任务 @EnableScheduling public class SpringBootDemoApplica ...
- SpringBoot整合定时任务task
@SpringBootApplication //扫描 mybatis mapper 包路径 @MapperScan(basePackages = "com.imooc.mapper&quo ...
随机推荐
- VirtualBox for mac
一. VirtualBox 1.下载 官网: https://www.virtualbox.org/wiki/Downloads 2.安装 傻瓜式安装即可 二.centOS7 1.下载 centOS: ...
- AutoFac在MVC中的使用
在asp.net mvc控制器中使用Autofac来解析依赖 如下Controller中使用构造函数依赖注入接口IUserService: public IUserService _IUserServ ...
- 去除pt种里tracker的方法
1.下载BEncode Editor,打开 2.把下载的种子拖进去,可以看到这样的: tracker就是箭头指的地方,这个包含了个人帐号独一无二的私钥,如果泄露给别人的话别人下载就是走的你的帐号,轻则 ...
- 二叉树放置照相机 Binary Tree Cameras
2019-03-27 15:39:37 问题描述: 问题求解: 很有意思的问题,问题描述简单,求解过程也可以非常的简洁,是个难得的好题. 求解的过程是自底向上进行分析,对于叶子节点,如果在叶子上放置照 ...
- 《SQL 基础教程》第三章:聚合和排序
这一章节主要讲了三方面的内容: 数据的汇总操作a. 聚合函数b.分组操作 给汇总操作指定条件 对汇总结果进行排序 COUNT()等聚合函数 定义: 输入多行,输出一行的函数称为聚合函数 功能: 用于对 ...
- ActiveRecord Nested Atrributes 关联记录,对嵌套属性进行CURD
设置了Nested attributes后,你可以通过父记录来更新/新建/删除关联记录. 使用: #accepts_nested_attributes_for class method. 例如: cl ...
- mysql查询今日、本周、本月记录
SELECT * FROM table_name WHERE to_days(createtime) = to_days(now()); SELECT * FROM table_name WHERE ...
- mybatis_generator合并xml和Java
之前写了合并xml的插件,今天改了改mybatis-generator源码,合并java和xml都改进去了. 先上图吧. 左边是一开始生成的,中间去掉author加了password字段和方法,右边重 ...
- zzw原创_非root用户启动apache的问题解决(非root用户启动apache的1024以下端口)
场景:普通用户编译的apache,要在该用户下启动1024端口以下的apache端口 1.假设普通用户为sims20,用该用户编译 安装了一个apache,安装路径为/opt/aspire/produ ...
- Oracle判断周末
有些业务场景下会有择出周末的需求,具体判断语句如下: 1.SELECT TO_CHAR(TO_DATE(DATA_DATE,'YYYY-MM-DD),'D') FROM DUAL; 如果DATA_DA ...