定时任务,异步任务

一、定时任务

1、步骤:

1:在启动类上写@EnableScheduling注解

2:在要定时任务的类上写@component

3:在要定时执行的方法上写@Scheduled(fixedRate=毫秒数)。

2、示例

主类

@SpringBootApplication
@EnableScheduling //开启定时任务
public class MainApplication { public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}

定时任务类

import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class Jobs { //表示方法执行完成后5秒
@Scheduled(fixedDelay=5000)
public void fixedDelayJob() throws InterruptedException{
System.out.println("fixedDelay 每隔5秒"+new Date());
} //表示每隔3秒
@Scheduled(fixedRate=3000)
public void fixedRateJob(){ System.out.println("fixedRate 每隔3秒"+new Date());
} //表示每天8时30分0秒执行
@Scheduled(cron="0 0,30 0,8 ? * ? ")
public void cronJob(){ System.out.println(new Date()+" ...>>cron...."); } }

效果:

3.总结

1.fixedDelay和fixedRate,单位是毫秒,这里这里就是5秒和3秒,它们的区别就是:

,fixedRate就是每多次分钟一次,不论你业务执行花费了多少时间。我都是1分钟执行1次,而fixedDelay是当任务执行完毕后1分钟在执行。所以根据实际业务不同,我们会选择不同的方式。

2.cron表达式:比如你要设置每天什么时候执行,就可以用它

cron表达式,有专门的语法,而且感觉有点绕人,不过简单来说,大家记住一些常用的用法即可,特殊的语法可以单独去查。
     cron一共有7位,但是最后一位是年,可以留空,所以我们可以写6位:

* 第一位,表示秒,取值0-59
* 第二位,表示分,取值0-59
* 第三位,表示小时,取值0-23
* 第四位,日期天/日,取值1-31
* 第五位,日期月份,取值1-12
* 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思
另外:1表示星期天,2表示星期一。
* 第7为,年份,可以留空,取值1970-2099

cron中,还有一些特殊的符号,含义如下:

(*)星号:可以理解为每的意思,每秒,每分,每天,每月,每年...
(?)问号:问号只能出现在日期和星期这两个位置。
(-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12
(,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期一,星期二,星期四
(/)斜杠:如:x/y,x是开始值,y是步长,比如在第一位(秒) 0/15就是,从0秒开始,每15秒,最后就是0,15,30,45,60 另:*/y,等同于0/y

下面列举几个例子供大家来验证:

0 0 3 * * ?     每天3点执行
0 5 3 * * ? 每天3点5分执行
0 5 3 ? * * 每天3点5分执行,与上面作用相同
0 5/10 3 * * ? 每天3点的 5分,15分,25分,35分,45分,55分这几个时间点执行
0 10 3 ? * 1 每周星期天,3点10分 执行,注:1表示星期天
0 10 3 ? * 1#3 每个月的第三个星期,星期天 执行,#号只能出现在星期的位置

二、异步任务

1、步骤

1: 启动类里面使用@EnableAsync注解开启功能,自动扫描

2:在要异步任务的类上写@component

3:在定义异步任务类写@Async(写在类上代表整个类都是异步,在方法加上代表该类异步执行)

2、示例

主类

@SpringBootApplication
@EnableAsync //开启异步任务
public class MainApplication { public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}

异步类

import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component; /**
* 功能描述:异步任务业务类
*/
@Component
@Async
public class AsyncTask { //获取异步结果
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");
} }

controller类

import java.util.concurrent.Future;
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;
import com.jincou.task.AsyncTask;
import com.jincou.util.JsonData; @RestController
@RequestMapping("/api/v1")
public class UserController { @Autowired
private AsyncTask task; @GetMapping("async_task")
public JsonData exeTask() throws InterruptedException{ long begin = System.currentTimeMillis(); Future<String> task4 = task.task4();
Future<String> task5 = task.task5();
Future<String> task6 = task.task6(); //如果都执行往就可以跳出循环,isDone方法如果此任务完成,true
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);
}
}

结果:

页面:

后台:

3、总结

从上面示例我们可以看出:如果同步方法,那我们需要6秒,而异步执行,我们只需要3秒左右,这就是异步的作用。

1)要把异步任务封装到类里面,不能直接写到Controller
     2)增加Future<String> 返回结果 AsyncResult<String>("task执行完成");
     3)如果需要拿到结果 需要判断全部的 task.isDone()

想太多,做太少,中间的落差就是烦恼。想没有烦恼,要么别想,要么多做。上尉【10】

springBoot(9)---定时任务,异步任务的更多相关文章

  1. SpringBoot整合定时任务异步任务

    1.定时任务 1.开启定时任务 @SpringBootApplication //开启定时任务 @EnableScheduling public class SpringBootDemoApplica ...

  2. SpringBoot整合定时任务和异步任务处理 3节课

    1.SpringBoot定时任务schedule讲解   定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类        ...

  3. SpringBoot整合定时任务和异步任务处理

    SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题, ...

  4. 玩转SpringBoot之定时任务详解

    序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...

  5. SpringBoot 配置定时任务

    SpringBoot启用定时任务,其内部集成了成熟的框架,因此我们可以很简单的使用它. 开启定时任务 @SpringBootApplication //设置扫描的组件的包 @ComponentScan ...

  6. SpringBoot - 添加定时任务

    SpringBoot 添加定时任务 EXample1: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spri ...

  7. springboot之定时任务

    定时线程 说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务. 回顾一下定时线程池. public static ScheduledExecutorService newScheduledT ...

  8. 十三、springboot集成定时任务(Scheduling Tasks)

    定时任务(Scheduling Tasks) 在springboot创建定时任务比较简单,只需2步: 1.在程序的入口加上@EnableScheduling注解. 2.在定时方法上加@Schedule ...

  9. SpringBoot创建定时任务

    之前总结过spring+quartz实现定时任务的整合http://www.cnblogs.com/gdpuzxs/p/6663725.html,而springboot创建定时任务则是相当简单. (1 ...

随机推荐

  1. C#的split分割的举例

    下面列举了split分割字符串的几种示例: string te = ";"; string re = "a;b"; string se = "a&qu ...

  2. Lucene学习笔记:基础

    Lucence是Apache的一个全文检索引擎工具包.可以将采集的数据存储到索引库中,然后在根据查询条件从索引库中取出结果.索引库可以存在内存中或者存在硬盘上. 本文主要是参考了这篇博客进行学习的,原 ...

  3. Effective C++ 笔记:条款 34 实现继承和接口继承

    Differentiate between inheritance of interface and inheritance of implementation. 行为含义 声明一个pure virt ...

  4. 学习Acegi应用到实际项目中(8)- 扩展UserDetailsService接口

    一个能为DaoAuthenticationProvider提供存取认证库的的类,它必须要实现UserDetailsService接口: public UserDetails loadUserByUse ...

  5. JAVA执行远端服务器的脚本

    JAVA执行远端服务器的脚本 问题描述 实现思路 技术要点 代码实现 问题描述 工作中遇到这样一个问题,我们的应用为了实现高可用会采取双机部署,拓扑图大致如下: 这种方案可以简单的保证高可用,即便应用 ...

  6. ionic 侧栏菜单用法

    第一步: 引入js和css文件我这里是直接引入的cdn,ionic是基于angular的,bundle.min.js把常用angular的js已经压缩到一起,可以直接引入.bundle.min.js, ...

  7. 浅谈如何检查Linux中开放端口列表

    给大家分享一篇关于如何检查Linux中的开放端口列表的详细介绍,首先如果你想检查远程Linux系统上的端口是否打开请点击链接浏览.如果你想检查多个远程Linux系统上的端口是否打开请点击链接浏览.如果 ...

  8. 网络操作系统 第六章 Window Server 2008 活动目录

    本章小结 在企业网络中,活动目录是必不可少的,它将网络中各种对象组织起来进行管理,方便了网络对象的查找,增强了网络的安全性,为用户的物流管理提供了极大的便利,本章首先介绍了活动目录的基本概念,然后通过 ...

  9. NLP文本相似度

    NLP文本相似度 相似度 相似度度量:计算个体间相似程度 相似度值越小,距离越大,相似度值越大,距离越小 最常用--余弦相似度:​ 一个向量空间中两个向量夹角的余弦值作为衡量两个个体之间差异的大小 余 ...

  10. PowerShell实现简单的搜索地区功能

    Add-Type -AssemblyName Microsoft.VisualBasic $VBI = [Microsoft.VisualBasic.Interaction] function Sea ...