jfinal2.2定时任务

时间:2021-06-30 13:11:42

引言:只从jfinal2.0后,部分功能代码有所变动,导致jfianl2.2无法使用之前的定时任务。


实现如下:

1、去quartz官网下载最新的定时任务jar包(http://www.quartz-scheduler.org/)


2、自己实现QuartzPlugin.java文件,代码如下


import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;


import com.jfinal.plugin.IPlugin;


/**
 * 实现作业的调度
 */
public class QuartzPlugin implements IPlugin {


private Logger logger = Logger.getLogger(QuartzPlugin.class);
private SchedulerFactory sf = null;
private Scheduler sched = null;
private String config = null;
private Properties properties;


public QuartzPlugin(String config) {
this.config = config;
}


@SuppressWarnings({ "rawtypes", "unchecked" })
public boolean start() {
sf = new StdSchedulerFactory();
try {
sched = sf.getScheduler();
} catch (SchedulerException e) {
new RuntimeException(e);
}
loadProperties();
Enumeration enums = properties.keys();
while (enums.hasMoreElements()) {
String key = enums.nextElement() + "";
if (!key.endsWith("job")) {
continue;
}
String cronKey = key.substring(0, key.indexOf("job")) + "cron";
String enable = key.substring(0, key.indexOf("job")) + "enable";
if (isDisableJob(enable)) {
continue;
}
String jobClassName = properties.get(key) + "";
String jobCronExp = properties.getProperty(cronKey) + "";
Class clazz;
try {
clazz = Class.forName(jobClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
JobDetail job = newJob(clazz).withIdentity(jobClassName,
jobClassName).build();
CronTrigger trigger = newTrigger()
.withIdentity(jobClassName, jobClassName)
.withSchedule(cronSchedule(jobCronExp)).build();
Date ft = null;
try {
ft = sched.scheduleJob(job, trigger);
sched.start();
} catch (SchedulerException ee) {
new RuntimeException(ee);
}
logger.info(job.getKey() + " has been scheduled to run at: " + ft
+ " and repeat based on expression: "
+ trigger.getCronExpression());
}
return true;
}


private boolean isDisableJob(String enable) {
return Boolean.valueOf(properties.get(enable) + "") == false;
}


private void loadProperties() {
properties = new Properties();
InputStream is = QuartzPlugin.class.getClassLoader()
.getResourceAsStream(config);
try {
properties.load(is);
} catch (IOException e) {
throw new RuntimeException(e);
}
}


public boolean stop() {
try {
sched.shutdown();
} catch (SchedulerException e) {
logger.error("shutdown error", e);
return false;
}
return true;
}
}


3、创建定时任务


import java.io.File;
import java.util.List;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.jfinal.kit.PathKit;
import com.fs.eams.model.InventoryTaskResultRecord;
import com.fs.eams.utils.DateUtils;

public class InventoryQuartzTask implements Job {


public void execute(JobExecutionContext arg0) throws JobExecutionException {
// 内容
}
}


4、properties配置文件内容(名称自定义,我这叫:inventoryQuartzTask.properties)


inventoryQuartzTask.job=com.fs.eams.common.quartzTask.InventoryQuartzTask
inventoryQuartzTask.cron= 0 0 2 * * ?
inventoryQuartzTask.enable=true


# 每隔5秒执行一次:*/5 * * * * ?
#
# 每隔1分钟执行一次:0 */1 * * * ?
#
# 每天23点执行一次:0 0 23 * * ?
#
# 每天凌晨1点执行一次:0 0 1 * * ?
#
# 每月1号凌晨1点执行一次:0 0 1 1 * ?
#
# 每月最后一天23点执行一次:0 0 23 L * ?
#
# 每周星期天凌晨1点实行一次:0 0 1 ? * L
#
# 在26分、29分、33分执行一次:0 26,29,33 * * * ?
#
# 每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?


5、jfinal定时任务配置


public void configPlugin(Plugins me) {
/*
* 定时任务
*/
QuartzPlugin inventoryQuartzTask = new QuartzPlugin("inventoryQuartzTask.properties");
me.add(inventoryQuartzTask);
}