多年前使用过quartz,今天又需要再用,而且是在spring boot框架下。很神奇,spring也是十年前用过的。

  这里仅记录下完成的最快速和简单的操作,高级的使用以后有空弄明白了再写:

1、增加maven引用

        <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>

2、编写任务代码:按自己要求编写类即可,每个任务做好标注@Scheduled(...)

package com.my.task;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import java.text.SimpleDateFormat;
import java.util.Date; @Component
@Configurable
@EnableScheduling
public class HappyBirthday { /**
* 使用spring 内置slf4j日志对象
*/
private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass()); /**
* 30秒间隔启动一次任务
*/
@Scheduled(fixedRate = 1000 * 30)
public void reportCurrentTime(){
logger.info ("Scheduling Tasks Examples: The time is now " + dateFormat ().format (new Date ()));
} /**
* 每分钟启动一次任务
*/
@Scheduled(cron = "0 */1 * * * * ")
public void reportCurrentByCron(){
logger.info ("Scheduling Tasks Examples By Cron: The time is now " + dateFormat ().format (new Date()));
} private SimpleDateFormat dateFormat(){
return new SimpleDateFormat ("HH:mm:ss");
}
}

3、done

quartz与spring boot-最简模式的更多相关文章

  1. Intellij IDEA Spring Boot 项目Debug模式启动缓慢问题

    问题 Intellij IDEA Spring Boot 项目Debug模式启动缓慢 环境 os: windows10 idea :2018.1 解决方法 去除所有断点就正常了,很诡异,原因未知.

  2. Quartz与Spring Boot集成使用

    上次自己搭建Quartz已经是几年前的事了,这次项目中需要定时任务,需要支持集群部署,想到比较轻量级的定时任务框架就是Quartz,于是来一波. 版本说明 通过搜索引擎很容易找到其官网,来到Docum ...

  3. 【Quartz】Spring Boot使用properties文件配置Quartz

    (1)在resource目录下新建quartz.properties文件 #============================================================== ...

  4. Spring - Spring Boot - Thymeleaf - textual 模式

    概述 thymeleaf 的 text 模式简单使用 过程会比较啰嗦, 需要结论的同学, 可以直接到底部去寻找 背景 想尝试做一个简单的 模板工具 目的 自动生成一些简单的 重复文本 思路 尽量简单 ...

  5. Spring/Spring boot正确集成Quartz及解决@Autowired失效问题

    周五检查以前Spring boot集成Quartz项目的时候,发现配置错误,因此通过阅读源码的方式,探索Spring正确集成Quartz的方式. 问题发现 检查去年的项目代码,发现关于QuartzJo ...

  6. Spring Boot 定时任务 Quartz 使用教程

    Quartz是一个完全由java编写的开源作业调度框架,他使用非常简单.本章主要讲解 Quartz在Spring Boot 中的使用. 快速集成 Quartz 介绍 Quartz 几个主要技术点 Qu ...

  7. 2019-04-05 Spring Boot学习记录

    1. 使用步骤 ① 在pom.xml 增加父级依赖(spring-boot-starter-parent) ② 增加项目起步依赖,如spring-boot-starter-web ③ 配置JDK版本插 ...

  8. Spring Boot文档

    本文来自于springboot官方文档 地址:https://docs.spring.io/spring-boot/docs/current/reference/html/ Spring Boot参考 ...

  9. 20191127 Spring Boot官方文档学习(4.18-4.24)

    4.18.JTA的分布式事务 通过使用Atomikos或Bitronix嵌入式事务管理器,Spring Boot支持跨多个XA资源的分布式JTA事务.部署到合适的Java EE应用程序服务器时,还支持 ...

  10. 在Spring Boot启动后执行指定代码

    在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据等等. 在Spring中可以通过ApplicationListener来实现相关的功能,不过在配合Spring Bo ...

随机推荐

  1. 英语口语练习系列-C34-儿童-谈论物品和人-武陵春

    词汇-儿童 child a child favorite game toy regulation breadwinner dominant selfish ancestor custom belief ...

  2. Kotlin基础(五)Kotlin的类型系统

    Kotlin的类型系统 一.可空类型 //s为null的话编译器会报错,没问号不能为空 fun strLen(s : String)=s.length //如果允许s为空可: fun strLen2( ...

  3. 阿里云EDAS在本地CentOS7.5 系统搭建测试环境,部署配置中心以及部署多个war包

    参考阿里云的EDAS开发文档: 使用 Ali-Tomcat 开发应用 我们自己在内网搭建CentOS7 的测试环境,需要的资源如下: Ali-Tomcat Pandora 容器 EDAS 配置中心安装 ...

  4. error LNK2001: unresolved external symbol _main

    想运行一个网上下载的opengl离屏渲染(渲染到纹理FBO)的程序,然后一直报错 error LNK2001: unresolved external symbol _main 解决了一下午终于弄明白 ...

  5. BZOJ2596 : [Wc2007]疯狂赛车

    根据光路最快原理以及斯涅尔定律,可以得到从定点$P$进入某条直线的最佳入射角. 求出每个端点到每条线段的最佳点,建图求最短路即可. 时间复杂度$O(n^2\log n)$. #include<c ...

  6. Java 构造器 遇到多个构造器时要考虑用构建器

    静态工厂和构造器有个共同的局限性:它们都不能很好地扩展到大量的可选参数. 当一个类中有若干个必选属性和多个可选属性时,采用重叠构造器模式.JavaBeans模式或者Builder模式,但各有优劣. 当 ...

  7. 大神们都在用的两个国外的免费离线下载:Rain&amp; LoadBT

    大神们都在用的两个国外的免费离线下载:Rain& LoadBT 最近QQ离线和迅雷离线也都挂了,115还死贵,所以分享两个国外免费的网站.希望可以为大家提供一些帮助,同时也借此机会同大家相互交 ...

  8. Vue 2.3、2.4 知识点小结

    2.3 style 多重值: <div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }">< ...

  9. 基于Token的身份认证 与 基于服务器的身份认证

    基于Token的身份认证 与 基于服务器的身份认证 基于服务器的身份认证 在讨论基于Token的身份认证是如何工作的以及它的好处之前,我们先来看一下以前我们是怎么做的: HTTP协议是无状态的,也就是 ...

  10. Idea checkstyle插件的使用

    File->Setting 选择Plugins,查询是否已经安装了checkstyle,如果没有安装,可以点击下面的“Browse repositories...”按钮 查询到checkstyl ...