SpringBoot几种定时任务的实现方式 和多线程执行任务
定时任务实现的几种方式:
- Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务。使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时间运行。一般用的较少。
 - ScheduledExecutorService:也jdk自带的一个类;是基于线程池设计的定时任务类,每个调度任务都会分配到线程池中的一个线程去执行,也就是说,任务是并发执行,互不影响。
 - Spring Task:Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多。
 - Quartz:这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂。
 
下面说的是 spring自带的定时任务。
SpringBoot使用注解方式开启定时任务添加注解方式
开发中定时任务,要和别的业务类分开写。这样处理起来方便。
1)启动类里面 @EnableScheduling开启定时任务,自动扫描
			2)定时任务业务类 加注解 @Component被容器扫描
			3)定时执行的方法加上注解 @Scheduled(fixedRate=2000) 定期执行一次
@SpringBootApplication //一个注解顶下面3个
@EnableScheduling //开启定时任务
@EnableAsync //开启异步任务
public class XdclassApplication { public static void main(String[] args) {
SpringApplication.run(XdclassApplication.class, args);
}
} //定时任务类
@Component
public class AsyncTask {
@Scheduled(fixedRate=2000)
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 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");
}
SpringBoot2.x异步任务实战(核心知识) 不一定是要和上面的定时任务一起使用还有很多场景的。需要异步处理的任务,最好单独的封装在一个类中。这样好处理。
	简介:讲解什么是异步任务,和使用SpringBoot2.x开发异步任务实战
		1、什么是异步任务和使用场景:适用于处理log、发送邮件、短信……等
			下单接口->查库存 100
					余额校验 150
					风控用户100
2、启动类里面使用@EnableAsync注解开启功能,自动扫描
3、定义异步任务类并使用@Component标记组件被容器扫描,异步方法加上@Async
			注意点:
				1)要把异步任务封装到类里面,不能直接写到Controller
				2)增加Future<String> 返回结果 AsyncResult<String>("task执行完成");  
				3)如果需要拿到结果 需要判断全部的 task.isDone()
		4、通过注入方式,注入到controller里面,如果测试前后区别则改为同步则把Async注释掉
@Component
@Async
public class AsyncTask {
@Scheduled(fixedRate=2000)
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 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");
}
}
@GetMapping("async_task")
public JsonData exeTask() throws InterruptedException{
   long begin = System.currentTimeMillis();
   Future<String> task4 = task.task4();
   Future<String> task5 = task.task5();
   for(;;){
      if (task4.isDone() && task5.isDone() ) {
         break;
      }
   }
   long end = System.currentTimeMillis();
   long total = end-begin;
   System.out.println("执行总耗时="+total);
   return JsonData.buildSuccess(total);
}
												
											SpringBoot几种定时任务的实现方式 和多线程执行任务的更多相关文章
- SpringBoot几种定时任务的实现方式
		
定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行, ...
 - 【SpringBoot】几种定时任务的实现方式
		
SpringBoot 几种定时任务的实现方式 Wan QingHua 架构之路 定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java ...
 - SpringBoot三种配置Dubbo的方式
		
*必须首先导入dubbo-starter (1).使用SpringBoot配置文件(application.properties或application.yml) dubbo.application. ...
 - SpringBoot两种读取配置文件的方式
		
方式一 @Value("${custom.group}") private String customGroup; 方式二 @Autowired private Environme ...
 - springBoot第二种配置文件yaml书写方式及读取数据、整合myBatis和整合junit
		
一.yaml文件格式:key-value形式:可以表示对象 集合 1.语法:key:value 冒号后面必须跟一个空格再写value值 key1: key2: key3:value 2.属性取值:a. ...
 - SpringBoot的四种定时任务
		
定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务. 使用这种方式可以让你的程序按照某一个频度执行 ...
 - Spring boot 集成三种定时任务方式
		
三种定时任务方式分别为 org.springframework.scheduling.annotation.Scheduled java.util.concurrent.ScheduledExecut ...
 - SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)
		
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10659045.html,否则将追究法律责任!!! 一.在JAVA开发领域,目前可以通过以下 ...
 - SpringBoot中的定时任务与Quartz的整合
		
SpringBoot集成Quartz 定时任务Quartz : 就是在指定的时间执行一次或者循环执行,在项目的开发中有时候会需要的, 还是很有用的. SpringBoot内置的定时 添加依赖 < ...
 
随机推荐
- 19.SSM整合_配置式开发
			
1.定义实体类Student 2.定义Student表 3.定义index页面 4.定义处理器 5.定义Service 6.定义Dao接口 7.定义Dao的Mapper配置文件 8.定义MyBatis ...
 - set 集合的函数调用
			
方法 意义 S.add(e) 在集合中添加一个新的元素e:如果元素已经存在,则不添加 S.remove(e) 从集合中删除一个元素,如果元素不存在于集合中,则会产生一个KeyError错误 S.dis ...
 - Kubernetes的yaml文件中command的使用
			
前面说了init容器initContainers,这主要是为应用容器做前期准备工作的,一般都会用到shell脚本,这就会用到command,这里写写command的用法. command就是将命令在创 ...
 - vim技巧总结
			
自动补齐CTRL+N/CTRL+P vim 自动补全 颜色设置 hi Pmenu ctermfg=black ctermbg=gray guibg=#444444 hi PmenuSel ctermf ...
 - 使用Mysql-magic获取Mysql账户密码
			
版权声明:本文为博主原创文章,欢迎转载,转载请注明原文超链接https://www.cnblogs.com/zerotrust/p/10846530.html 本文仅限于技术讨论与分享,严禁用于非法用 ...
 - Centos7下搭建WebGoat 8和DVWA环境
			
搭建WebGoat 安装前置条件说明 我们这里选择WebGoat的jar版本,由于WebGoat 8的jar文件已自带了tomcat和数据库,所以不需要再另外安装tomcat和mysql这种东西,只需 ...
 - 构建之法个人作业5——alpha2项目测试
			
[相关信息] Q A 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/2019autumnsystemanalysisanddesign/ 这个作业要求在 ...
 - C/C++代码规范
			
零.前言 笔者最近在看开源代码,看到代码格式各自参差不齐,感觉像是各家各有所长.因此打算写一篇关于C/C++代码规范文章,请各位参考,并践踏批评. 一.文件排版 1. 包含头文件 • 先系统头文件,后 ...
 - Nginx的TCP/UDP调度器
			
安装nginx [root@proxy ~]# yum –y install gcc pcre-devel openssl-devel //安装依赖包 [root@proxy ~]# .tar.gz ...
 - struts2的Action中使用spring的@Transactional注解事务出错
			
1.在Struts2使用的是spring管理对象. 使用spring的注解式事务配置, 在action的方法中直接使用事务遇到的问题. public class testAction extends ...