#定时任务
#指定定时任务类
quartz.job=com.app.task.SyncJDTokenJob
#cron定时任务时间
quartz.cron=0 0 6 * * ?
package com.app.task;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import com.jfinal.plugin.IPlugin;
public class QuartzPlugin implements IPlugin {
private static final String QUARTZ = "quartz.";
private static final String JOB = "job";
private static final String CRON = "cron";
private Properties properties;
private String config = "task.properties";//不能用"quartz.properties"命名,与SchedulerFactory有冲突
private SchedulerFactory schedulerFactory;
private Scheduler scheduler;
public QuartzPlugin(){
}
public QuartzPlugin(String config){
this.config = config;
}
//加载properties文件
public void loadProperties(){
properties = new Properties();
try {
InputStream is = this.getClass().getClassLoader().getResourceAsStream(config);
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean start() {
schedulerFactory = new StdSchedulerFactory();
try {
//初始化调度器
scheduler = schedulerFactory.getScheduler();
} catch (SchedulerException e) {
e.printStackTrace();
}
loadProperties();
Enumeration<Object> enums = properties.keys();
while (enums.hasMoreElements()) {
String key = enums.nextElement()+"";
if(key.indexOf(QUARTZ)!= 0)continue;
if(key.indexOf(JOB)!= QUARTZ.length())continue;
//获取任务类和corn表达式
String jobClassName = properties.getProperty(QUARTZ+JOB)+"";
String cronExp = properties.getProperty(QUARTZ+CRON)+"";
try {
Class<? extends Job> clazz = Class.forName(jobClassName).asSubclass(Job.class);
//构造任务
JobDetail jobDetail = JobBuilder.newJob(clazz)
.withIdentity(jobClassName, jobClassName)
.build();
//构造任务触发器
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(jobClassName, jobClassName)
.withSchedule(CronScheduleBuilder.cronSchedule(cronExp))
.build();
//把任务和任务触发器添加到调度器中
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
@Override
public boolean stop() {
try {
scheduler.shutdown();
} catch (SchedulerException e) {
e.printStackTrace();
}
return true;
}
}
package com.app.task;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.app.service.JDAcessTokenService;
import com.app.util.sys.SYS_TABLE;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
public class SyncJDTokenJob implements Job {
private JDAcessTokenService tokenService = new JDAcessTokenService();
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
//需要执行的任务代码
}
}
/**
* 注册插件
* **/
@Override
public void configPlugin(Plugins plugins)
{
//定时任务quartz插件
plugins.add(new QuartzPlugin());
}