java中实现定时任务 task 或quartz
转载大神的
https://www.cnblogs.com/hafiz/p/6159106.html
https://www.cnblogs.com/luchangyou/p/6856725.html
使用Spring Task轻松完成定时任务
一、背景
最近项目中需要使用到定时任务进行库存占用释放的需求,就总结了如何使用Spring Task进行简单配置完成该需求,本文介绍Spring3.0以后自定义开发的定时任务工具,
spring task,我们可以将它比作一个轻量级的Quartz,使用简单方便,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式,下面我会分别介绍这两种方式。
二、定时任务开发步骤
开发环境
Spring 4.2.6.RELEASE
Maven 3.3.9
Jdk 1.7
Idea 15.04
【1】.基于配置文件
1.编写普通java class
package com.hafiz.www.cron; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* Desc:第一个基于SpringTask的调度任务
* Created by hafiz.zhang on 2016/12/11.
*/
public class FirstCron {
private static final Logger logger = LoggerFactory.getLogger(FirstCron.class); public void cron() {
logger.info("定时任务进行中.......");
// do something else
}
}
2.在spring配置文件头中添加命名空间及描述(下面加粗处)并配置定时任务
1 <beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:task="http://www.springframework.org/schema/task"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
6 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
7
8 <bean id="firstCron" class="com.hafiz.www.cron.FirstCron"/>
9 <task:scheduled-tasks>
10 <task:scheduled ref="firstCron" method="cron" cron="0/5 * * * * ?"/>
11 </task:scheduled-tasks>
12 </beans>
我们设置每5秒钟运行一次。关于Spring Task 的 cron表达式,请参见另一篇博客:摆脱Spring 定时任务的@Scheduled cron表达式的困扰
【2】基于注解
我们可以使用@Scheduled注解进行开发,首先我们看下,该注解的源码
1 package org.springframework.scheduling.annotation;
2
3 import java.lang.annotation.Documented;
4 import java.lang.annotation.ElementType;
5 import java.lang.annotation.Repeatable;
6 import java.lang.annotation.Retention;
7 import java.lang.annotation.RetentionPolicy;
8 import java.lang.annotation.Target;
9 import org.springframework.scheduling.annotation.Schedules;
10
11 @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
12 @Retention(RetentionPolicy.RUNTIME)
13 @Documented
14 @Repeatable(Schedules.class)
15 public @interface Scheduled {
16 String cron() default "";
17
18 String zone() default "";
19
20 long fixedDelay() default -1L;
21
22 String fixedDelayString() default "";
23
24 long fixedRate() default -1L;
25
26 String fixedRateString() default "";
27
28 long initialDelay() default -1L;
29
30 String initialDelayString() default "";
31 }
可以看出该注解有五个方法或者叫参数,分别表示的意思是:
cron:指定cron表达式
zone:官方文档解释:A time zone for which the cron expression will be resolved。指定cron表达式运行的时区
fixedDelay:官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
fixedRate:官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
initialDelay:官方文档解释:Number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.任务第一次被调用前的延时,单位毫秒
1.编写注解的定时任务类
1 package com.hafiz.www.cron;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.scheduling.annotation.Scheduled;
6
7 /**
8 * Desc:第一个基于SpringTask的调度任务
9 * Created by hafiz.zhang on 2016/12/11.
10 */
11 public class FirstCron {
12 private static final Logger logger = LoggerFactory.getLogger(FirstCron.class);
13
14 @Scheduled(cron = "0/5 * * * * ?")
15 public void cron() {
16 logger.info("定时任务进行中.......");
17 // do something else
18 }
19 }
2.在spring配置文件头中添加命名空间及描述(下面加粗处)并开启定时任务注解驱动
1 <beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:task="http://www.springframework.org/schema/task"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
6 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
7
8 <bean id="firstCron" class="com.hafiz.www.cron.FirstCron"/>
9 <task:scheduler id="qbScheduler" pool-size="10"/>
10 <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
11 <!--简单来说,我们只需要<task:annotation-driven/>这一句即可,这些参数不是必须的 -->
12 </beans>
以上我们就完成了基于注解的定时任务的开发,是不是很简单?
运行结果:

三、总结
其实有些知识我们表面上看起来很难,但是当我们实际操作的时候,发现挺简单的,只要遇到问题我们勤思考多思考,就一定会有解决办法。关于定时任务,还有一种基于Spring Quartz的实现,以后有需要,我们再进行介绍。欢迎留言交流.......
java中实现定时任务 task 或quartz的更多相关文章
- 转:java中的定时任务
引自:http://www.cnblogs.com/wenbronk/p/6433178.html java中的定时任务, 使用java实现有3种方式: 1, 使用普通thread实现 @Test p ...
- Java中的定时任务
现代的应用程序早已不是以前的那些由简单的增删改查拼凑而成的程序了,高复杂性早已是标配,而任务的定时调度与执行也是对程序的基本要求了. 很多业务需求的实现都离不开定时任务,例如,每月一号,移动将清空你上 ...
- Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)
在Java中,实现定时任务有多种方式,本文介绍4种,Timer和TimerTask.Spring.QuartZ.Linux Cron. 以上4种实现定时任务的方式,Timer是最简单的,不需要任何框架 ...
- Java 中的定时任务(一)
定时任务简单来说就是在指定时间,指定的频率来执行一个方法,而在 Java 中我们又该如何实现呢? 想来主要有 3 种方式,最原始的方式肯定是开启一个线程,让它睡一会跑一次睡一会跑一次这也就达到了定频率 ...
- java中的定时任务小示例
package package_1; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; ...
- java中实现定时功能
网上资料: 我们可以使用Timer和TimerTask类在java中实现定时任务,详细说明如下: 1.基础知识java.util.Timer一种线程设施,用于安排以后在后台线程中执行的任务.可安排任务 ...
- 如何用 Java 实现 Web 应用中的定时任务?
定时任务,是指定一个未来的时间范围执行一定任务的功能.在当前WEB应用中,多数应用都具备任务调度功能,针对不同的语音,不同的操作系统, 都有其自己的语法及解决方案,windows操作系统把它叫做任务计 ...
- 如何用 Java 实现 Web 应用中的定时任务
定时任务,是指定一个未来的时间范围执行一定任务的功能.在当前WEB应用中,多数应用都具备任务调度功能,针对不同的语音,不同的操作系统, 都有其自己的语法及解决方案,windows操作系统把它叫做任务计 ...
- java中基于TaskEngine类封装实现定时任务
主要包括如下几个类: 文章标题:java中基于TaskEngine类封装实现定时任务 文章地址: http://blog.csdn.net/5iasp/article/details/10950529 ...
随机推荐
- [原创]java导出excel
一.需求背景 在项目开发中,经常会遇到导出Excel报表文件的情况,因为很多情况下,我们需要打印Excel报表,虽然在网页上也可以生成报表,但是打印网上里的报表是无法处理排版问题的,所以最好的方式,还 ...
- 机器学习 Regularization and model selection
Regularization and model selection 假设我们为了一个学习问题尝试从几个模型中选择一个合适的模型.例如,我们可能用一个多项式回归模型hθ(x)=g(θ0+θ1x+θ2x ...
- asterisk ss7 ${CALLERID(rdnis)}变量为空问题
asterisk 1.8.16+chan_ss7 version 2.1.1b ${CALLERID(rdnis)}变量取不到信息问题,解决 编辑 funcs/func_callerid.c chan ...
- Spring笔记05(Spring JDBC三种数据源和ORM框架的映射)
1.ORM框架的映射 01.JDBC连接数据库以前的方式代码,并给对象赋值 @Test /** * 以前的方式jdbc */ public void TestJdbc(){ /** * 连接数据库的四 ...
- MySQL记录_20160919
1.首先先看下什么是MySQL. MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 最流行的关系型数据库管理系统,其开放源码这一特点 ...
- ACM学习历程—HDU 1272 小希的迷宫(并查集)
Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...
- 设计四个线程,其中两个线程每次对j加1,另外两个线程每次对j减1
public class ManyThreads2 { private int j = 0; public synchronized void inc() { j++; System.out.prin ...
- 红黑树的C语言实现
rbtree.h #ifndef _RED_BLACK_TREE_H_ #define _RED_BLACK_TREE_H_ #define RED 0 // 红色节点 #define BLACK 1 ...
- DS:template
ylbtech-DS: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech.cnbl ...
- JavaScript总结(1)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...