在Java中,实现定时任务有多种方式,本文介绍4种,Timer和TimerTask、Spring、QuartZ、Linux Cron。

以上4种实现定时任务的方式,Timer是最简单的,不需要任何框架,仅仅JDK就可以,缺点是仅仅是个时间间隔的定时器,调度简单;Spring和QuartZ都支持cron,功能都很强大,Spring的优点是稍微简单一点,QuartZ的优点是没有Spring也可使用;Linux Cron是个操作系统级别的定时任务,适用于所有操作系统支持的语言,缺点是精度只能到达分钟级别。

Timer和TimerTask

关于Timer定时器的实现原理,如果我们看过JDK源码,就会发现,是使用的Object.wait(timeout),来进行的线程阻塞,timeout是根据下次执行实际和当前实际之差来计算。实际上,这可以归结为一个多线程协作(协作都是在互斥下的协作)问题。

在java.util.concurrent中,有个ScheduledThreadPoolExecutor,也可以完全实现定时任务的功能。

而其他的框架,无非是功能的增强,特性更多,更好用,都是在基础的java之上的包装。

代码示例如下:

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class TimerTest extends TimerTask
{
private Timer timer;
public static void main(String[] args)
{
TimerTest timerTest= new TimerTest();
timerTest.timer = new Timer();
//立刻开始执行timerTest任务,只执行一次
timerTest.timer.schedule(timerTest,new Date());
//立刻开始执行timerTest任务,执行完本次任务后,隔2秒再执行一次
//timerTest.timer.schedule(timerTest,new Date(),2000);
//一秒钟后开始执行timerTest任务,只执行一次
//timerTest.timer.schedule(timerTest,1000);
//一秒钟后开始执行timerTest任务,执行完本次任务后,隔2秒再执行一次
//timerTest.timer.schedule(timerTest,1000,2000);
//立刻开始执行timerTest任务,每隔2秒执行一次
//timerTest.timer.scheduleAtFixedRate(timerTest,new Date(),2000);
//一秒钟后开始执行timerTest任务,每隔2秒执行一次
//timerTest.timer.scheduleAtFixedRate(timerTest,1000,2000); try
{
Thread.sleep(10000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
//结束任务执行,程序终止
timerTest.timer.cancel();
//结束任务执行,程序并不终止,因为线程是JVM级别的
//timerTest.cancel();
}
@Override
public void run()
{
System.out.println("Task is running!");
}
}

使用spring @Scheduled注解执行定时任务

这种方式非常简单,却能使用cron完成和QuartZ一样的功能,值得推荐一下。

ApplicationContext.xml:

beans根节点增加内容:xmlns:task="http://www.springframework.org/schema/task"

beans根节点中,xsi:schemaLocation属性下增加:

http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd

task:annotation-driven/

实现类:

@Component  //import org.springframework.stereotype.Component;
public class MyTestServiceImpl implements IMyTestService {
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
@Override
public void myTest(){
System.out.println("进入测试");
}
}

注意几点:

spring的@Scheduled注解  需要写在实现上;

定时器的任务方法不能有返回值;

实现类上要有组件的注解@Component,@Service,@Repository

QuartZ

QuartZ With Spring

applicationContext-schedule.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 启动的Trigger列表 -->
<ref local="startThriftTrigger" />
</list>
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.threadPool.threadCount">5</prop>
</props>
</property>
<!-- 启动时延期3秒开始任务 -->
<property name="startupDelay" value="3" />
<property name="autoStartup" value="${scheduler.autoStartup}" />
</bean> <!-- 启动Thrift,立即启动 -->
<bean id="startThriftTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="startDelay" value="0" />
<property name="repeatInterval" value="1000" />
<property name="repeatCount" value="0" />
<property name="jobDetail" ref="startThriftTask" />
</bean>
<bean id="startThriftTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="startThrift" />
<property name="targetMethod" value="execute" />
<!-- 同一任务在前一次执行未完成而Trigger时间又到时是否并发开始新的执行, 默认为true. -->
<property name="concurrent" value="true" />
</bean>
</beans>

实现类

package xx.schedule;

@Component
public class StartThrift {
/**
* 调度入口
*/
public void execute() {
// to do something
}
}

QuartZ No Spring

使用的QuartZ jar是1.8.5,如下:

<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.5</version>
</dependency>

调用类实现代码如下:

import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory; public class InvokeStatSchedule {
public void start() throws SchedulerException
{
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler(); //InvokeStatJob是实现了org.quartz.Job的类
JobDetail jobDetail = new JobDetail("jobDetail", "jobDetailGroup", InvokeStatJob.class);
CronTrigger cronTrigger = new CronTrigger("cronTrigger", "triggerGroup");
try {
CronExpression cexp = new CronExpression("0 0 * * * ?");
cronTrigger.setCronExpression(cexp);
} catch (Exception e) {
e.printStackTrace();
}
scheduler.scheduleJob(jobDetail, cronTrigger);
scheduler.start();
}
}

定时任务类代码如下:

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; public class InvokeStatJob implements Job { @Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
//...要定时操作的内容
}
}

Linux Cron

这其实也是一种非常普遍的实现定时任务的方式,实际是操作系统的定时任务。Linux Cron只能到达分钟级,到不了秒级别。

一般我们是设置定时执行一个sh脚本,在脚本里面写一些控制代码,例如,有如下的脚本:

3,33 * * * * /usr/local/log_parser/run_log_parser.sh &

run_log_parser.sh的内容大致如下:

#!/bin/sh

log_parser_dir=/usr/local/log_parser
tmp_file=/usr/local/run_parser_tmp.txt
parser_log=/usr/local/access_parser.log
tmpDir=/data/applogs/access_logs_bp date >> "$parser_log" if [! -f "$tmp_file"]; then
echo '访问日志解析正在进行,尚未完成' >> "$parser_log"
echo '' >> "$parser_log"
else
echo '开始解析访问日志' >> "$parser_log"
touch "$tmp_file"
cd "$log_parser_dir"
python access_log_parser.py >> "$parser_log"
rm "$tmp_file"
echo '解析访问日志完成' >> "$parser_log"
echo '' >> "$parser_log"
cd "$tmpDir"
gzip gzip WEB0*
mv *.gz gz/
echo '压缩备份日志及移动到压缩目录成功' >> "$parser_log"
fi from: https://www.kancloud.cn/digest/java-travel/159427

Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)的更多相关文章

  1. SpringBoot之旅 -- 定时任务两种(Spring Schedule 与 Quartz 整合 )实现

    相关文章 Spring Boot 相关文章目录 前言 最近在项目中使用到定时任务,之前一直都是使用Quartz 来实现,最近看Spring 基础发现其实Spring 提供 Spring Schedul ...

  2. Java定时任务调度工具Timer Quartz

      -----------------------------------Timer PS:Timer是 jdk中的:Quartz是第三方的 PS:也即是返回执行任务时候的时间 ----------- ...

  3. java定时任务实现的几种方式(Timer、Spring Task、Quartz)

    Timer JDK自带的Timer类,允许调度一个TimerTask任务. Demo: /** * Timer测试类 */ public class TimerDemo { public static ...

  4. atititt.java定时任务框架选型Spring Quartz 注解总结

    atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz  (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...

  5. spring定时任务之quartz

    在Spring中,使用JDK的Timer类库来做任务调度功能不是很方便,关键它不可以象cron服务那样可以指定具体年.月.日.时和分的时间.你只能将时间通过换算成微秒后传给它.如任务是每天执行一次,则 ...

  6. Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置

    Spring整合quartz2.2.3总结,quartz动态定时任务,Quartz定时任务集群配置 >>>>>>>>>>>>&g ...

  7. java中实现定时任务 task 或quartz

    转载大神的 https://www.cnblogs.com/hafiz/p/6159106.html https://www.cnblogs.com/luchangyou/p/6856725.html ...

  8. [JAVA]定时任务之-Quartz使用篇

    Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十个,百个, ...

  9. Java基础--定时任务Timer

    Java基础--定时任务Timer 一.Timer介绍 java.util.Timer java.util.TimerTask Timer是一个定时器类,通过该类可以为指定的定时任务进行配置.Time ...

随机推荐

  1. 【LOJ】#2511. 「BJOI2018」双人猜数游戏

    题解 设\(f[p][a][b]\)表示询问了\(p\)次,答案是\(a,b\)是否会被猜出来 然后判断如果\(p = 1\) 第一个问的\(Alice\),那么\([s,\sqrt{nm}]\)约数 ...

  2. streaming优化:并行接收数据

    val numStreams = 5 val kafkaStreams = (1 to numStreams).map { i => KafkaUtils.createStream(...) } ...

  3. 使用python爬取整本《盗墓笔记》

    一.前言 <盗墓笔记>是一本经典的盗墓题材小说,故事情节引人入胜.本文将使用python2.7通过小说网站http://www.daomubiji.com/来爬取整本盗墓笔记并保存,在这一 ...

  4. 记一次重装系统(.net开发环境重装问题汇总)

    起因: 有一天,我突然感觉到电脑的运行明显变慢,慢的可怕,启动任务资源管理器一看,不看不知道,一看吓一跳,CPU使用率,物理内存皆100%,当时的第一印象,是电脑中病毒了吧!!,进入进程一看,有几个名 ...

  5. 【二分答案+2-SAT】Now or later UVALive - 3211

    题目链接:https://cn.vjudge.net/contest/209473#problem/J 题目大意: 有n架飞机,每架飞机有两个可降落时间点a,b(a<b)(即一架飞机可以选择在时 ...

  6. FHQ Treap及其可持久化与朝鲜树式重构

    FHQ Treap,又称无旋treap,一种不基于旋转机制的平衡树,可支持所有有旋treap.splay等能支持的操作(只有在LCT中会比splay复杂度多一个log).最重要的是,它是OI中唯一一种 ...

  7. BZOJ1170 : [Balkan2007]Cipher

    首先对于每个位置,求出它开始长度为y的横行的hash值,然后对于hash值再求一次竖列的hash值,排序后求出众数即可. 时间复杂度$O(n^2\log n)$. #include<cstdio ...

  8. Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题

    B. Young Photographer 题目连接: http://codeforces.com/contest/14/problem/B Description Among other thing ...

  9. OpenJ_POJ C16G Challenge Your Template 迪杰斯特拉

    Challenge Your Template 题目连接: http://acm.hust.edu.cn/vjudge/contest/122701#problem/G Description ACM ...

  10. ueditor上传图片设置的简单实例

    0.前言:我用过ckeditor,kingeditor还是感觉ueditor最好用,功能强大,经常更新.之前因为升级了struts2到2.5的了,原本的kingeditor已经不能共存,于是找到了ud ...