java timer cron_Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)

本文介绍四种Java定时任务实现方法:Timer和TimerTask、Spring、QuartZ及LinuxCron。详细对比了各方法的特点和适用场景,并提供了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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注解运行定时任务

这样的方式很easy,却能使用cron完毕和QuartZ一样的功能,值得推荐一下。

ApplicationContext.xml:

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

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

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

实现类:

@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"?>

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">

5

实现类

package xx.schedule;

@Component

public class StartThrift {

/**

* 调度入口

*/

public void execute() {

// to do something

}

}

QuartZ No Spring

使用的QuartZ jar是1.8.5,例如以下:

org.quartz-scheduler

quartz

1.8.5

调用类实现代码例如以下:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值