标签:style blog http java color strong
Hello Quartz / 本文通过一个简单的例子让大家快速了解Quartz,上手,并了解Quartz内的一些关键对象 如 Scheduler、Job、Trigger、JobExecutionContext等对象package org.quartz.examples.examples01; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; /** * hello world Quartz * @author weeks * */ public class HelloJob implements Job { private static Logger _log = LoggerFactory.getLogger(HelloJob .class ); /** * Job,Job需要一个公有的构造函数,否则Factory无法构建 */ public HelloJob() { } /** * 实现execute方法 */ public void execute(JobExecutionContext context) throws JobExecutionException { _log.info( "Hello World! - " + new Date()); } }
package org.quartz.examples.examples01; import static org.quartz.DateBuilder.evenMinuteDate ; import static org.quartz.JobBuilder.newJob ; import static org.quartz.TriggerBuilder.newTrigger ; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Date; /** * 此Demo将演示如何启动和关闭Quartz调度器,以及如何运作 * @author weeks * */ public class SimpleExample { public void run() throws Exception { Logger log = LoggerFactory.getLogger(SimpleExample.class); log.info("------- Initializing ----------------------"); // 1、工厂模式 构建Scheduler的Factory,其中STD为Quartz默认的Factory,开发者亦可自行实现自己的Factory;Job、Trigger等组件 SchedulerFactory sf = new StdSchedulerFactory(); // 2、通过SchedulerFactory获得Scheduler对象 Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete -----------"); // 3、org.quartz.DateBuilder.evenMinuteDate <下一分钟> -- 通过DateBuilder构建Date Date runTime = evenMinuteDate(new Date()); log.info("------- Scheduling Job -------------------"); // 4、org.quartz.JobBuilder.newJob --通过JobBuilder构建Job JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build(); // 5、通过TriggerBuilder进行构建 Trigger trigger = newTrigger().withIdentity("trigger1" , "group1").startAt(runTime ).build(); // 6、工厂模式,组装各个组件<JOB,Trigger> sched.scheduleJob(job, trigger); // [group1.job1] will run at: log.info(job.getKey() + " will run at: " + runTime); // 7、start sched.start(); log.info("------- Started Scheduler -----------------"); log.info("------- Waiting 65 seconds... -------------"); try { // wait 65 seconds to show job Thread.sleep(65L * 1000L); // executing... } catch (Exception e) { // } // shut down the scheduler log.info("------- Shutting Down ---------------------"); // 8、通过Scheduler销毁内置的Trigger和Job sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); } public static void main(String[] args) throws Exception { SimpleExample example = new SimpleExample(); example.run(); } }
private transient Scheduler scheduler ; private Trigger trigger; private JobDetail jobDetail; private JobDataMap jobDataMap; private transient Job job; private Calendar calendar; private boolean recovering = false; private int numRefires = 0; private Date fireTime; private Date scheduledFireTime; private Date prevFireTime; private Date nextFireTime; private long jobRunTime = -1; private Object result; private HashMap<Object, Object> data=new HashMap<Object, Object>();
标签:style blog http java color strong
原文地址:http://blog.csdn.net/chenweitang123/article/details/37837557