Java SpringBoot Scheduled定时任务
package task.demo.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import task.demo.service.AsyncService; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; @RestController
public class AsyncController {
@Autowired
AsyncService asyncService; @RequestMapping("/hello")
public Object hello() {
//@EnableAsync //注解方式开启异步支持
String date1 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(date1);
asyncService.hello();
String date2 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println(date2);
return "success";
} /**
* 定时任务
* @return
*/
@RequestMapping("/scheduledHello")
public Object scheduledHello() {
//@EnableScheduling //注解方式开启定时任务支持
asyncService.scheduledHello();
return "scheduledHello";
}
}
package task.demo.service; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; @Service
public class AsyncService { /**
* 异步任务
*/
@Async
public void hello() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("开始执行");
} /**
* 定时任务 每两秒执行一次
* cron表达式在线生成 http://cron.qqe2.com/
* 秒 分 时 日 月 周
*/
@Scheduled(cron = "0/2 * * * * *")
public void scheduledHello() {
String date1 = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("定时任务执行了 " + date1);
}
}
Java SpringBoot Scheduled定时任务的更多相关文章
- SpringBoot执行定时任务@Scheduled
SpringBoot执行定时任务@Scheduled 在做项目时,需要一个定时任务来接收数据存入数据库,后端再写一个接口来提供该该数据的最新的那一条. 数据保持最新:设计字段sign的值(0,1)来设 ...
- SpringBoot学习18:springboot使用Scheduled 定时任务器
Scheduled 定时任务器:是 Spring3.0 以后自带的一个定时任务器. 1.在pom.xml文件中添加Scheduled依赖 <!-- 添加spring定时任务 Scheduled ...
- SpringBoot: 18.使用Scheduled 定时任务器(转)
Scheduled 定时任务器:是 Spring3.0 以后自带的一个定时任务器. 1.在pom.xml文件中添加Scheduled依赖 <!-- 添加spring定时任务 Scheduled ...
- springboot整合@Scheduled定时任务的使用
1.启动类里面添加注解@EnableScheduling ,例如: @SpringBootApplication@EnableScheduling@MapperScan("com.examp ...
- SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)
一.使用场景 定时任务在开发中还是比较常见的,比如:定时发送邮件,定时发送信息,定时更新资源,定时更新数据等等... 二.准备工作 在Spring Boot程序中不需要引入其他Maven依赖 (因为s ...
- Scheduled定时任务器在Springboot中的使用
Scheduled定时任务器是Spring3.0以后自带的一个定时任务器. 使用方式: 1.添加依赖 <!-- 添加 Scheduled 坐标 --> <dependency> ...
- springboot scheduled并发配置
本文介绍如何使用springboot的sheduled实现任务的定时调度,并将调度的任务实现为并发的方式. 1.定时调度配置scheduled 1)注册定时任务 package com.xiaoju. ...
- SpringBoot - 添加定时任务
SpringBoot 添加定时任务 EXample1: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spri ...
- springboot @scheduled 并发
本文介绍如何使用springboot的sheduled实现任务的定时调度,并将调度的任务实现为并发的方式. 1.定时调度配置scheduled 1)注册定时任务 package com.xiaoju. ...
随机推荐
- 【洛谷 P2051】 [AHOI2009]中国象棋(DP)
题目链接 首先想到状压dp,但是\(n,m\)高达100,怎么压? 容易发现,每行每列最多两个象棋,否则就直接gg了. 一个巧妙的设置状态的方式是,只需要记录到当前行有多少列是放了1个炮和2个炮. 然 ...
- 7.nth-of-type | nth-child?【CSS】
举例说明: <ul> <p>111</p> <span>222</span> <li>1</li> <li& ...
- DQL 查询表中的数据
DQL 查询表中的数据:查询语句(最复杂的语句)不会对数据库中的数据进行修改,只是一种显示数据的方式 语法格式: select 字段列表 from 表名列表 where 条件列表 group by 分 ...
- [极客-Linux] 04 Linux命令
创建用户: useradd 装文件: CentOS: (软件管家yum) rpm -i xxx.rpm Ubuntu: (apt-get) dpkg -i xxx.deb nohup: no ...
- Graphviz install the Windows for Scyther
1.在Pycharm 中使用Scyther工具的时候需要导入 graphviz 直接在 Interpreter 上安装的售后会报错,如果在 IDE上无法支架安装的库可以试图在控制台上安装,控制台上无法 ...
- SoC的设计变的如此复杂和高成本
当一些硬件IP变成了标准的螺丝钉和螺母的时候,硬件设计的未来就没有了吗? 由于太过于复杂,而整体的毛利率又不高,无法迅速迭代,产生边际效应,也无法迅速扩张. 就成了一个传统行业,从业者也逐渐被时代遗忘 ...
- PHP实现DES/ECB/PKCS5Padding加密兼容Java SHA1PRNG算法
在使用php调用java接口时,遇到了两边加密结果不一致的问题.经过沟通发现接口方使用了SHA1PRNG算法,对原密码计算后做为Des的加密Key. 因此在php中也需要先对原密码做相应计算才能保持结 ...
- PHP 判断终端是手机还是电脑访问网站代码
用thinkphp做底层框架,判断客户是用pc访问还是手机访问的. <?php $platform = platform();//检测访问平台 //print_r($_SERVER);DIE; ...
- Eclipse中修改了项目,导入Tomcat中时,括号显示原来项目的名字
Eclipse中Tomcat导入项目并且修改了项目名字,把项目添加到Tomcat上,发现在项目后面带了个括号里面显示原来项目的名字,并且在访问的时候也只能用原来的项目名访问,怎么办呢? 1.打开你的项 ...
- SIGAI机器学习第十八集 线性模型2
之前讲过SVM,是通过最大化间隔导出的一套方法,现在从另外一个角度来定义SVM,来介绍整个线性SVM的家族. 大纲: 线性支持向量机简介L2正则化L1-loss SVC原问题L2正则化L2-loss ...