SpringBoot整合定时任务task

@SpringBootApplication
//扫描 mybatis mapper 包路径
@MapperScan(basePackages = "com.imooc.mapper")
//扫描 所有需要的包, 包含一些自用的工具类包 所在的路径
@ComponentScan(basePackages= {"com.imooc", "org.n3r.idworker"})
//开启定时任务
@EnableScheduling
public class ImoocApplication { public static void main(String[] args) {
SpringApplication.run(ImoocApplication.class, args);
}
}
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class TestTask { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); // 定义每过3秒执行任务
@Scheduled(fixedRate = 3000)
public void reportCurrentTime() {
System.out.println("现在时间:" + dateFormat.format(new Date()));
}
}
cron表达式定时任务; ps: springboot不支持年
@Scheduled(cron = "4-40 * * * * ?")

@SpringBootApplication
//扫描 mybatis mapper 包路径
@MapperScan(basePackages = "com.imooc.mapper")
//扫描 所有需要的包, 包含一些自用的工具类包 所在的路径
@ComponentScan(basePackages= {"com.imooc", "org.n3r.idworker"})
//开启定时任务
@EnableScheduling
//开启异步调用方法
@EnableAsync
public class ImoocApplication { public static void main(String[] args) {
SpringApplication.run(ImoocApplication.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
public class AsyncTask { @Async
public Future<Boolean> doTask11() throws Exception {
long start = System.currentTimeMillis();
Thread.sleep(1000);
long end = System.currentTimeMillis();
System.out.println("任务1耗时:" + (end - start) + "毫秒");
return new AsyncResult<>(true);
} @Async
public Future<Boolean> doTask22() throws Exception {
long start = System.currentTimeMillis();
Thread.sleep(700);
long end = System.currentTimeMillis();
System.out.println("任务2耗时:" + (end - start) + "毫秒");
return new AsyncResult<>(true);
} @Async
public Future<Boolean> doTask33() throws Exception {
long start = System.currentTimeMillis();
Thread.sleep(600);
long end = System.currentTimeMillis();
System.out.println("任务3耗时:" + (end - start) + "毫秒");
return new AsyncResult<>(true);
}
}
import java.util.concurrent.Future; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("tasks")
public class DoTask { @Autowired
private AsyncTask asyncTask; @RequestMapping("test1")
public String test1() throws Exception { long start = System.currentTimeMillis(); Future<Boolean> a = asyncTask.doTask11();
Future<Boolean> b = asyncTask.doTask22();
Future<Boolean> c = asyncTask.doTask33(); while (!a.isDone() || !b.isDone() || !c.isDone()) {
if (a.isDone() && b.isDone() && c.isDone()) {
break;
}
} long end = System.currentTimeMillis(); String times = "任务全部完成,总耗时:" + (end - start) + "毫秒";
System.out.println(times); return times;
}
}

线程池,activeMQ
SpringBoot整合定时任务task的更多相关文章
- SpringBoot整合全局异常处理&SpringBoot整合定时任务Task&SpringBoot整合异步任务
============整合全局异常=========== 1.整合web访问的全局异常 如果不做全局异常处理直接访问如果报错,页面会报错500错误,对于界面的显示非常不友好,因此需要做处理. 全局异 ...
- (入门SpringBoot)SpringBoot结合定时任务task(十)
SpringBoot整合定时任务task 使用注解EnableScheduling在启动类上. 定义@Component作为组件被容器扫描. 表达式生成地址:http://cron.qqe2.com ...
- SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)
一.使用场景 定时任务在开发中还是比较常见的,比如:定时发送邮件,定时发送信息,定时更新资源,定时更新数据等等... 二.准备工作 在Spring Boot程序中不需要引入其他Maven依赖 (因为s ...
- Spring Boot入门系列(八)整合定时任务Task,一秒搞定定时任务
前面介绍了Spring Boot 中的整合Redis缓存已经如何实现数据缓存功能.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/categ ...
- 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整合定时任务
定时任务一般是项目中都需要用到的,可以用于定时处理一些特殊的任务. 在SpirngBoot中使用定时任务变的特别简单,不需要再像SpringMVC一样写很多的配置,只需要在启动类上增加一个@Enabl ...
- SpringBoot整合定时任务异步任务
1.定时任务 1.开启定时任务 @SpringBootApplication //开启定时任务 @EnableScheduling public class SpringBootDemoApplica ...
随机推荐
- 最小二乘法 及 梯度下降法 分别对存在多重共线性数据集 进行线性回归 (Python版)
网上对于线性回归的讲解已经很多,这里不再对此概念进行重复,本博客是作者在听吴恩达ML课程时候偶然突发想法,做了两个小实验,第一个实验是采用最小二乘法对数据进行拟合, 第二个实验是采用梯度下降方法对数据 ...
- Linux kernel 不输出log信息
/********************************************************************************* * Linux kernel 不输 ...
- 开源库dlib的安装与编译-CMake
前言 最近项目涉及到关于face alignment的实现,了解到目前主要的算法有ERT.SDM.LBF等,其中由于dlib开源库实现了ERT算法,效果也很不错,故开始研究dlib的使用.而使用的第一 ...
- WPF 中对启动参数的处理
/// <summary> /// Interaction logic for App.xaml /// </summary> public partial ...
- log4net保存到数据库系列五、新增数据库字段
园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志一.WebConfig中配置log4net 一.WebConfig中配置log4ne ...
- 用复制mysql/data 文件夹 下面的数据库的形式来复制数据库出现的问题
用复制mysql/data 文件夹 下面的数据库的形式来复制数据库出现的问题 提示找不到表,我去看了一下,丢失了很多个表: 问题: 直接拷贝data下的数据库数据进行备份 , 发现部分数据表不见了. ...
- asm数据文件迁移(asm–>asm)
rman迁移操作 [oracle@localhost oradata]$ rman target / Recovery Manager: Release 10.2.0.3.0 - Producti ...
- git revert回退时提示One or more files are in a conflicted state
解决代码冲突 如果commit时出现“You have to update your work copy first.”红色警告,说明版本库中的此文件已经被其他人修改了. 请先点“ok”按钮退出.执行 ...
- JZ2440 裸机驱动 第8章 NAND Flash控制器
本章目标 了解NAND Flash 芯片的接口 掌握通过NAND Flash控制器访问NAND Flash的方法 8.1 NAND Flash介绍和NAND Flash控制器使用 NAND ...
- ASP.NET中将数据作为XML数据发送 使用 Request.InputStream 接收
将数据作为XML数据发送,例如:public void PostXml(string url, string xml){byte[] bytes = Encoding.UTF8.GetBytes(xm ...