1、添加包引用:/jfinal_demo/WebContent/WEB-INF/lib/quartz-all-1.6.1.jar

注意版本号,这个版本是现在项目中使用的,已经有更高版本了,但这版比较稳定。

2、添加Job的实现类:

package cn.jfinal.job;

import java.text.SimpleDateFormat;
import java.util.Date; import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; public class RedPacketValidate implements Job{
private static final Logger log = Logger.getLogger(RedPacketValidate.class); @Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
validate(); } private void validate() {
// TODO Auto-generated method stub
SimpleDateFormat FORMAT = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
String msgString="当前时间:"+FORMAT.format(new Date());
log.info("红包定时器开始扫描……"+msgString);
System.out.println(msgString);
} }

  

三、添加调度类:

有了干活的,得通知他什么时候干活,怎么干。

SimpleScheduler  简单调度,以后会有复杂调度类,敬请期待。

package cn.jfinal.job;

import java.util.Date;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory; public class SimpleScheduler { private static final Logger log = Logger.getLogger(SimpleScheduler.class); @Test
public void testScheduler()
{
SimpleScheduler simple = new SimpleScheduler();
try
{
// Create a Scheduler and schedule the Job
Scheduler scheduler = simple.createScheduler();
simple.scheduleJob(scheduler); // Start the Scheduler running
scheduler.start(); log.info( "Scheduler started at " + new Date()); } catch (SchedulerException ex) {
log.error(ex);
} } public Scheduler createScheduler() throws SchedulerException
{ //创建调度器
return StdSchedulerFactory.getDefaultScheduler();
} //Create and Schedule a RedPacketValidate with the Scheduler public void scheduleJob(Scheduler scheduler) throws SchedulerException
{ // Create a JobDetail for the Job
JobDetail jobDetail = new JobDetail("RedPacketValidate",Scheduler.DEFAULT_GROUP,RedPacketValidate.class); // Configure the directory to scan
//jobDetail.getJobDataMap().put("SCAN_DIR","D:\\Tomcat\\conf"); //set the JobDataMap that is associated with the Job. // Create a trigger that fires every 10 seconds, forever
Trigger trigger = TriggerUtils.makeSecondlyTrigger(10);//每10秒触发一次 trigger.setName("scanTrigger"); // Start the trigger firing from now
trigger.setStartTime(new Date());//设置第一次触发时间 // Associate the trigger with the job in the scheduler
scheduler.scheduleJob(jobDetail, trigger);
} }

  

四、在web中使用:

SimpleScheduler simple = new SimpleScheduler();
try {
// Create a Scheduler and schedule the Job
Scheduler scheduler = simple.createScheduler();
simple.scheduleJob(scheduler); // Start the Scheduler running
scheduler.start(); log.info("Scheduler started at " + new Date()); } catch (SchedulerException ex) {
log.error(ex);
}

  

五、注意引用job用到的jar包:

Jfinal中定时器的初步探索(一)-LMLPHP

05-11 11:36