Java定时任务调度工具Timer Quartz
-----------------------------------Timer
PS:Timer是 jdk中的;Quartz是第三方的
PS:也即是返回执行任务时候的时间
---------------------------------------------------一个综合的例子
import java.text.SimpleDateFormat; import java.util.TimerTask; public class DancingRobot extends TimerTask{ @Override public void run() { // 获取最近一次任务执行的时间,将其格式化 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("最近执行任务的时间:"+sdf.format(scheduledExecutionTime()));//获取最近一次任务执行的时间,将其格式化 System.out.println("开心的跳舞"); } }
import java.util.Timer; import java.util.TimerTask; public class WaterRobot extends TimerTask{ private Timer timer; public WaterRobot(Timer timer) { this.timer = timer; } //最大容量为5L private Integer bucketCapacity = 0; @Override public void run() { // 灌水直到桶水满为止 if(bucketCapacity<5){ System.out.println("机器人加1L水"); bucketCapacity++; }else{ System.out.println("返回取消的任务数 before:"+timer.purge()); cancel();//停止当前的机器人的执行 System.out.println("机器人停止加水"); System.out.println("返回取消的任务数 after:"+timer.purge()); System.out.println("当前水容量:"+bucketCapacity+"L"); try { Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); } timer.cancel();//停止所有的机器人操作 } } }
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Timer; public class Main { public static void main(String[] args) { Timer timer = new Timer(); //获取当前时间 Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间:"+sdf.format(calendar.getTime())); DancingRobot dr = new DancingRobot(); WaterRobot wr = new WaterRobot(timer); timer.schedule(dr, calendar.getTime(), 2000); timer.scheduleAtFixedRate(wr, calendar.getTime(), 1000); } /* 当前时间:2018-06-08 18:11:24 最近执行任务的时间:2018-06-08 18:11:25 开心的跳舞 机器人加1L水 机器人加1L水 机器人加1L水 最近执行任务的时间:2018-06-08 18:11:27 开心的跳舞 机器人加1L水 机器人加1L水 最近执行任务的时间:2018-06-08 18:11:29 开心的跳舞 返回取消的任务数 before:0 机器人停止加水 返回取消的任务数 after:1 当前水容量:5L*/ }
------------------------------------Quartz
-------------------------------------Hello world-----------------------
package com.beet.test.HelloQuartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloJob implements Job{ public void execute(JobExecutionContext arg0) throws JobExecutionException { //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前execution时间为:"+sdf.format(date)); // 编写具体的业务逻辑 System.out.println("hello world"); } }
package com.beet.test.HelloQuartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) throws SchedulerException { //创建一个jobdetail实例,将该实例与hellojob class绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build(); //创建一个Trigger的实例,定义改job立即执行,并且每隔2秒钟重复一次,直到永远 SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").startNow().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build(); //创建schedule实例 StdSchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); scheduler.start(); //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间为:"+sdf.format(date)); scheduler.scheduleJob(jobDetail, trigger); } }
当前时间为:2018-06-14 09:57:36
当前execution时间为:2018-06-14 09:57:36
hello world
当前execution时间为:2018-06-14 09:57:38
hello world
当前execution时间为:2018-06-14 09:57:40
hello world
当前execution时间为:2018-06-14 09:57:42
hello world
当前execution时间为:2018-06-14 09:57:44
public class HelloScheduler { public static void main(String[] args) throws SchedulerException { //创建一个jobdetail实例,将该实例与hellojob class绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build(); System.out.println("jobDetail的名字:"+jobDetail.getKey().getName()); System.out.println("jobDetail的group:"+jobDetail.getKey().getGroup()); System.out.println("jobDetail的jobclass:"+jobDetail.getJobClass().getName()); //创建一个Trigger的实例,定义改job立即执行,并且每隔2秒钟重复一次,直到永远 SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").startNow().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build(); //创建schedule实例 StdSchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); scheduler.start(); //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间为:"+sdf.format(date)); scheduler.scheduleJob(jobDetail, trigger); /*jobDetail的名字:myJob jobDetail的group:group1 jobDetail的jobclass:com.beet.test.HelloQuartz.HelloJob 当前时间为:2018-06-14 10:10:25 当前execution时间为:2018-06-14 10:10:26 hello world*/ } }
---------------------------------------------------------使用JobDataMap传参数
package com.beet.test.HelloQuartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.TriggerKey; public class HelloJob implements Job{ public void execute(JobExecutionContext context) throws JobExecutionException { //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 编写具体的业务逻辑 //System.out.println("hello world"); JobKey key = context.getJobDetail().getKey(); TriggerKey trKey = context.getTrigger().getKey(); JobDataMap dataMap = context.getJobDetail().getJobDataMap(); JobDataMap tDataMap = context.getTrigger().getJobDataMap(); //从中获取Map //String JobMsg = dataMap.getString("message"); // Float jobFloatValue = dataMap.getFloat("FloatJobValue"); String triggerMsg = tDataMap.getString("message"); Float triggerDoubleValue = tDataMap.getFloat("DoubleTriggerValue"); System.out.println("当前execution时间为:"+sdf.format(date)); System.out.println("我的job name和group是:"+key.getName()+":"+key.getGroup()); System.out.println("我的trigger name和group是:"+trKey.getName()+":"+trKey.getGroup()); //System.out.println("job message是:"+JobMsg); //System.out.println("FloatJobVAlue是:"+jobFloatValue); System.out.println("trigger message:"+triggerMsg); System.out.println("FloatJobVAlue是:"+triggerDoubleValue); } }
package com.beet.test.HelloQuartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) throws SchedulerException { //创建一个jobdetail实例,将该实例与hellojob class绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").usingJobData("message", "hello myJob1").usingJobData("FloatJobValue", 3.14f).build(); System.out.println("jobDetail的名字:"+jobDetail.getKey().getName()); System.out.println("jobDetail的group:"+jobDetail.getKey().getGroup()); System.out.println("jobDetail的jobclass:"+jobDetail.getJobClass().getName()); //创建一个Trigger的实例,定义改job立即执行,并且每隔2秒钟重复一次,直到永远 SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").usingJobData("message", "hello myTrigger").usingJobData("DoubleTriggerValue", 2.0D).startNow().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build(); //创建schedule实例 StdSchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); scheduler.start(); //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间为:"+sdf.format(date)); scheduler.scheduleJob(jobDetail, trigger); /*jobDetail的名字:myJob jobDetail的group:group1 jobDetail的jobclass:com.beet.test.HelloQuartz.HelloJob 当前时间为:2018-06-14 10:10:25 当前execution时间为:2018-06-14 10:10:26 hello world*/ } }
package com.beet.test.HelloQuartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) throws SchedulerException { //创建一个jobdetail实例,将该实例与hellojob class绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build(); /* System.out.println("jobDetail的名字:"+jobDetail.getKey().getName()); System.out.println("jobDetail的group:"+jobDetail.getKey().getGroup()); System.out.println("jobDetail的jobclass:"+jobDetail.getJobClass().getName()); */ //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间为:"+sdf.format(date)); date.setTime(date.getTime()+3000);//开始3s后执行 Date endDate = new Date(); endDate.setTime(endDate.getTime()+6000); //创建一个Trigger的实例,定义改job立即执行,并且每隔2秒钟重复一次,直到永远 SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").startAt(date).endAt(endDate).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build(); //创建schedule实例 StdSchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); scheduler.start(); scheduler.scheduleJob(jobDetail, trigger); /*jobDetail的名字:myJob jobDetail的group:group1 jobDetail的jobclass:com.beet.test.HelloQuartz.HelloJob 当前时间为:2018-06-14 10:10:25 当前execution时间为:2018-06-14 10:10:26 hello world*/ } }
package com.beet.test.HelloQuartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.Trigger; import org.quartz.TriggerKey; public class HelloJob implements Job{ public void execute(JobExecutionContext context) throws JobExecutionException { //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前execution时间为:"+sdf.format(date)); // 编写具体的业务逻辑 // System.out.println("hello world"); Trigger trigger = context.getTrigger(); System.out.println("start time is "+trigger.getStartTime()); System.out.println("end time is "+trigger.getEndTime()); } }
当前时间为:2018-06-14 14:14:32
当前execution时间为:2018-06-14 14:14:35
start time is Thu Jun 14 14:14:35 CST 2018
end time is Thu Jun 14 14:14:38 CST 2018
当前execution时间为:2018-06-14 14:14:37
start time is Thu Jun 14 14:14:35 CST 2018
end time is Thu Jun 14 14:14:38 CST 2018
package com.beet.test.HelloQuartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) throws SchedulerException { //创建一个jobdetail实例,将该实例与hellojob class绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build(); /* System.out.println("jobDetail的名字:"+jobDetail.getKey().getName()); System.out.println("jobDetail的group:"+jobDetail.getKey().getGroup()); System.out.println("jobDetail的jobclass:"+jobDetail.getJobClass().getName()); */ //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间为:"+sdf.format(date)); date.setTime(date.getTime()+3000);//开始3s后执行 Date endDate = new Date(); endDate.setTime(endDate.getTime()+6000); //创建一个Trigger的实例,定义改job立即执行,并且每隔2秒钟重复一次,直到永远 被endDate 覆盖掉了执行计划 SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").startAt(date).endAt(endDate).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build(); //创建schedule实例 定时 定频率 StdSchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); scheduler.start(); scheduler.scheduleJob(jobDetail, trigger); /*jobDetail的名字:myJob jobDetail的group:group1 jobDetail的jobclass:com.beet.test.HelloQuartz.HelloJob 当前时间为:2018-06-14 10:10:25 当前execution时间为:2018-06-14 10:10:26 hello world*/ } }
PS:
,表示或的关系
-,至 5-7 五到七
*,每秒钟
/, 每分钟没小时 0/5 没隔5分钟(..)出发一次
?,不关心
#,代表第.. 6#3 每月第三周的周五(6代表周五)
L,代表最后
package com.beet.test.HelloQuartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) throws SchedulerException { //创建一个jobdetail实例,将该实例与hellojob class绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build(); /* System.out.println("jobDetail的名字:"+jobDetail.getKey().getName()); System.out.println("jobDetail的group:"+jobDetail.getKey().getGroup()); System.out.println("jobDetail的jobclass:"+jobDetail.getJobClass().getName()); */ //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间为:"+sdf.format(date)); date.setTime(date.getTime()+3000);//开始3s后执行 Date endDate = new Date(); endDate.setTime(endDate.getTime()+6000); //创建一个Trigger的实例,定义改job立即执行,并且每隔2秒钟重复一次,直到永远 CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ? *")).build(); //创建schedule实例 StdSchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); scheduler.start(); scheduler.scheduleJob(jobDetail, trigger); } }
package com.beet.test.HelloQuartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.Trigger; import org.quartz.TriggerKey; public class HelloJob implements Job{ public void execute(JobExecutionContext context) throws JobExecutionException { //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前execution时间为:"+sdf.format(date)); // 编写具体的业务逻辑 System.out.println("hello world"); /*Trigger trigger = context.getTrigger(); System.out.println("start time is "+trigger.getStartTime()); System.out.println("end time is "+trigger.getEndTime()); */ } }
PS: 这两种方式推荐使用std的方式。
package com.beet.test.HelloQuartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.CronScheduleBuilder; import org.quartz.CronTrigger; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; public class HelloScheduler { public static void main(String[] args) throws SchedulerException, InterruptedException { //创建一个jobdetail实例,将该实例与hellojob class绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build(); /* System.out.println("jobDetail的名字:"+jobDetail.getKey().getName()); System.out.println("jobDetail的group:"+jobDetail.getKey().getGroup()); System.out.println("jobDetail的jobclass:"+jobDetail.getJobClass().getName()); */ //打印当前的执行时间 Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间为:"+sdf.format(date)); date.setTime(date.getTime()+3000);//开始3s后执行 Date endDate = new Date(); endDate.setTime(endDate.getTime()+6000); //创建一个Trigger的实例,定义改job立即执行,并且每隔2秒钟重复一次,直到永远 CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ? *")).build(); //创建schedule实例 StdSchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); scheduler.start(); Date scheduleDate = scheduler.scheduleJob(jobDetail, trigger); System.out.println("开始执行时间为:"+sdf.format(scheduleDate)); //scheudle执行2s挂起 Thread.sleep(2000); scheduler.standby();//挂起 //挂起3s重新启动 Thread.sleep(3000); scheduler.start(); } }
posted on 2018-06-08 11:48 biyangqiang 阅读(300) 评论(0) 编辑 收藏 举报