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. ...
随机推荐
- JS权威指南读书笔记(四)
第十章 正则表达式 1 正则表达式直接量定义为包含在一对斜杠(/)之间的字符 a /s$/ == new RegExp("s$") 2 直接量字符:所有字母和数字都是按照字 ...
- ES6之promise原理
我在这里介绍了promise的原理: https://juejin.im/post/5cc54877f265da03b8585902 我在这里 仅仅张贴 我自己实现的简易promise——DiProm ...
- vue标签内循环数据逗号分隔
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- awvs 中文手册详细版(含10.5及12版本)
目录: 0×00.什么是Acunetix Web Vulnarability Scanner ( What is AWVS?) 0×01.AWVS安装过程.主要文件介绍.界面简介.主要操作区域简介(I ...
- JQuery EasyUI treegrid展开与折叠,以及数据加载两次的问题
问题:做项目的时候遇到代码生成的页面,只默认展开了一级节点,每次操作之后刷新还要手动一级一级展开,太麻烦了 官方API:http://www.jeasyui.net/plugins/186.html ...
- 利用Python openpyxl操作Excel
from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = ...
- 安装k8s,使用root帐号的初始化脚本
现在稳定性差不多了.可以总结一下了. 真正使用时,有几个地方,还是确认一下,再正式运行吧. #!/bin/bash # Version V0. ---: ;fi K8S_VERSION="1 ...
- 二叉搜索树(python)
# -*- coding: utf-8 -*- class BSTNode(object): def __init__(self, key, value, left=None, right=None) ...
- 《你说对就队》第九次团队作业:Beta冲刺与验收准备
<你说对就队>第九次团队作业:Beta冲刺与验收准备 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 团队名称 <你说对就队> ...
- 《The One!团队》第八次作业:ALPHA冲刺(二)
项目 内容 作业所属课程 所属课程 作业要求 作业要求 团队名称 < The One !> 作业学习目标 (1)掌握软件测试基础技术.(2)学习迭代式增量软件开发过程(Scrum) 第二天 ...