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定时任务的更多相关文章

  1. SpringBoot执行定时任务@Scheduled

    SpringBoot执行定时任务@Scheduled 在做项目时,需要一个定时任务来接收数据存入数据库,后端再写一个接口来提供该该数据的最新的那一条. 数据保持最新:设计字段sign的值(0,1)来设 ...

  2. SpringBoot学习18:springboot使用Scheduled 定时任务器

    Scheduled 定时任务器:是 Spring3.0 以后自带的一个定时任务器. 1.在pom.xml文件中添加Scheduled依赖 <!-- 添加spring定时任务 Scheduled ...

  3. SpringBoot: 18.使用Scheduled 定时任务器(转)

    Scheduled 定时任务器:是 Spring3.0 以后自带的一个定时任务器. 1.在pom.xml文件中添加Scheduled依赖 <!-- 添加spring定时任务 Scheduled ...

  4. springboot整合@Scheduled定时任务的使用

    1.启动类里面添加注解@EnableScheduling ,例如: @SpringBootApplication@EnableScheduling@MapperScan("com.examp ...

  5. SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)

    一.使用场景 定时任务在开发中还是比较常见的,比如:定时发送邮件,定时发送信息,定时更新资源,定时更新数据等等... 二.准备工作 在Spring Boot程序中不需要引入其他Maven依赖 (因为s ...

  6. Scheduled定时任务器在Springboot中的使用

    Scheduled定时任务器是Spring3.0以后自带的一个定时任务器. 使用方式: 1.添加依赖 <!-- 添加 Scheduled 坐标 --> <dependency> ...

  7. springboot scheduled并发配置

    本文介绍如何使用springboot的sheduled实现任务的定时调度,并将调度的任务实现为并发的方式. 1.定时调度配置scheduled 1)注册定时任务 package com.xiaoju. ...

  8. SpringBoot - 添加定时任务

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

  9. springboot @scheduled 并发

    本文介绍如何使用springboot的sheduled实现任务的定时调度,并将调度的任务实现为并发的方式. 1.定时调度配置scheduled 1)注册定时任务 package com.xiaoju. ...

随机推荐

  1. 多个echarts图自适应屏幕大小

    当一个echarts图时,可以这样做 //下面my_charts是html中echarts的ID var myChart= echarts.init(document.getElementById(& ...

  2. SAP官网发布的react教程

    大家学习React的时候,用的是什么教程呢?Jerry当时用的阮一峰博客上的入门教程,因为React使用的JSX语法并不是所有的浏览器都支持,所以还得使用browser.js在浏览器端将JSX转换成J ...

  3. SpringBoot学习之@Configuration注解和@Bean注解

    @Configuration 1.@Configuration注解底层是含有@Component ,所以@Configuration 具有和 @Component 的作用. 2.@Configurat ...

  4. 【Redis】基本数据类型及命令操作(超详细)

    一.String 1.1 概述 1.2 相关命令列表 1.3 命令示例 二.List 2.1 概述: 2.2 相关命令列表: 2.3 命令示例: 2.4 链表结构的小技巧: 三.Hashes 3.1 ...

  5. [极客-Linux] 04 Linux命令

    创建用户: useradd 装文件: CentOS:   (软件管家yum) rpm -i xxx.rpm Ubuntu:   (apt-get) dpkg -i xxx.deb nohup: no ...

  6. Linux kernel buffer ring

    参考:What are the concepts of “kernel ring buffer”, “user level”, “log level”? Ring Buffer 原始问题 个人补充:r ...

  7. TODO的作用及如何使用

    https://blog.csdn.net/jerry11112/article/details/82966142 文章标题:[C#]TODO的作用 可以方便后续找到要做的功能点.

  8. Django之路——7 django与ajax

    Ajax简介 AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语言与服务器进行异步交互,传输的数 ...

  9. Bias vs. Variance(3)---用learning curves来判断bias/variance problem

    画learning curves可以用来检查我们的学习算法运行是否正常或者用来改进我们的算法,我们经常使用learning cruves来判断我们的算法是否存在bias problem/varianc ...

  10. Dynamics 365 目录

    Dynamics 365 目录 1.Dynamics 365 on-premises 安装 2.Dynamics 365 安装过程中的问题 2.1Dynamics 365 安装问题——无法访MSCRM ...