java 定时任务 (timer和timetask,quartz,spring,LinuxCron)
来源:互联网 发布:中文版国际象棋软件 编辑:程序博客网 时间:2024/05/21 14:08
</pre>在Java中,实现定时任务有多种方式,,Timer和TimerTask、Spring、QuartZ、Linux Cron。<p></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">以上4种实现定时任务的方式,Timer是最简单的,不需要任何框架,仅仅JDK就可以,缺点是仅仅是个时间间隔的定时器,调度简单;Spring和QuartZ都支持cron,功能都很强大,Spring的优点是稍微简单一点,QuartZ的优点是没有Spring也可使用;Linux Cron是个操作系统级别的定时任务,适用于所有操作系统支持的语言,缺点是精度只能到达分钟级别。</p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px">本文直介绍<span style="font-family:Arial; font-size:14px; line-height:26px">Timer和TimerTask、<span style="font-family:Arial; font-size:14px; line-height:26px">QuartZ</span></span></p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"></p><h1 style="margin:0px; padding:0px; font-family:Arial; line-height:26px">Timer和TimerTask</h1><div></div><pre name="code" class="java">package timertask;import java.util.Date;import java.util.Timer;import java.util.TimerTask;public class TimerTest extends TimerTask {@Overridepublic void run() {System.out.println(i++);}private Timer timer;private int i=0;public static void main(String[] args) {TimerTest tt=new TimerTest();tt.timer=new Timer();tt.timer.schedule(tt, new Date(),1000);//立刻开始执行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) {// TODO Auto-generated catch blocke.printStackTrace();}//结束任务执行,程序终止 tt.timer.cancel(); //结束任务执行,程序并不终止,因为线程是JVM级别的 //timerTest.cancel(); System.out.println("你好");}}
quartz 的实现。(测试时,需要quartz,slf4j,log4j的架包)
定时任务执行的对象 要继承与jobpackage timertask.quartz;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import org.quartz.Calendar;import org.quartz.CronExpression;import org.quartz.CronTrigger;import org.quartz.Job;import org.quartz.JobBuilder;import org.quartz.JobDataMap;import org.quartz.JobDetail;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.Trigger;import org.quartz.impl.StdSchedulerFactory;import org.quartz.impl.triggers.CronTriggerImpl;import com.sun.org.apache.bcel.internal.generic.NEW;public class AutoSendo implements Job{@Overridepublic void execute(JobExecutionContext jobexecutioncontext)throws JobExecutionException {//定时任务执行的动作。输出数据String string=(String) jobexecutioncontext.getJobDetail().getJobDataMap().get("key");System.out.println(string);//System.out.println("fdsfsdfsdfsd");}}
2,执行定时任务
package timertask.quartz;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import org.bouncycastle.jce.provider.JDKDSASigner.noneDSA;import org.quartz.CronExpression;import org.quartz.CronScheduleBuilder;import org.quartz.CronTrigger;import org.quartz.Job;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.JobKey;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;import org.quartz.impl.triggers.CronTriggerImpl;public class QuartzSchedulerUtil {public static void launchTask() throws Exception{SchedulerFactory factory =new StdSchedulerFactory();//一种,cronTrigger获得方式一CronTriggerImpl cronTrigger=new CronTriggerImpl();SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//第二种cronTrigger的获得方式//cronTrigger=TriggerBuilder.newTrigger().withIdentity("sss").withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build();cronTrigger.setCronExpression("0/30 * * * * ? ");cronTrigger.setName("sssss");cronTrigger.setStartTime(new Date());Class<Job> job1=(Class<Job>) Class.forName("timertask.quartz.AutoSendo");System.out.println(job1.newInstance());JobBuilder jb= JobBuilder.newJob(job1).withIdentity("sssssss");jb.usingJobData("key", "value");JobDetail job=jb.build();Scheduler scheduler=factory.getScheduler();Date date =scheduler.scheduleJob(job, cronTrigger);System.out.println(sdf.format(date)); scheduler.start();//立即发送方法scheduler.triggerJob(job.getKey());;//停止定时任务scheduler.unscheduleJob(cronTrigger.getKey());}public static void main(String[] args) throws Exception {QuartzSchedulerUtil.launchTask();}}
0 0
- java 定时任务 (timer和timetask,quartz,spring,LinuxCron)
- Java定时任务(Timer、Quartz、Spring、LinuxCron)
- Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)
- Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)
- Java之旅--定时任务(Timer、Quartz、Spring、LinuxCron)
- JAVA定时任务Timer、Spring Task、Quartz
- 定时任务实现Timer, TimeTask, ScheduledExecutorService及Spring定时器
- 定时任务实现Timer, TimeTask, ScheduledExecutorService及Spring定时器
- 定时任务实现Timer, TimeTask, ScheduledExecutorService及Spring定时器
- Android 中Timer和TimeTask完成定时任务
- Java执行定时任务(Timer、Quartz)
- java定时任务Timer,Quartz
- spring timetask 定时任务调度
- Spring+Quartz && Java.util.Timer 实现定时任务
- spring Quartz与Timer定时任务
- 定时任务之Timer,Quartz,Spring task
- java定时任务调度工具Timer和Quartz
- java Timer和TimeTask简介
- 安全相关的PHP函数
- 无法通过8192在表空间中扩展
- c++打印字符串的地址
- 多态性(二)——动态多态性之虚函数
- Android_Spinner的使用(第二季)
- java 定时任务 (timer和timetask,quartz,spring,LinuxCron)
- block测试题目验证结果
- lemon oa前端页面——由user-base-list谈项目组织
- Mysql常用函数
- 网络设备配合SecureCRT使用解决命令行超长串行问题的典型配置
- ACM-开灯问题
- LDA PLSA 比较总结
- SQL中使用WITH CTE
- 一点小笔记