|
本帖最后由 Jacktang 于 2018-7-7 15:49 编辑
很多时候,我们会遇到让系统自动执行某段代码去执行业务相关逻辑。如生日短信通知,我们需要一个定时任务,获取到生日当天的所有用户,然后执行短信推送等业务逻辑,Jfinal是如何实现定时任务调度呢?
第一步:在Jfinal的过滤器中的configPlugin方法中加入自己实现的jfinal插件接口类配置。
public void configPlugin(Plugins me){
QuartzPlugin plugin = new QuartzPlugin("job.properties");
me.add(plugin);
//...数据源等其他配置
其中QuartzPlugin类的代码如下:
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.log4j.Logger;
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 final Logger logger = Logger.getLogger(QuartzPlugin.class);
private static final String JOB = "job";
private String config = "job.properties";
private Properties properties;
public QuartzPlugin(String config) {
this.config = config;
}
public QuartzPlugin() {
}
private SchedulerFactory schedulerFactory;
private Scheduler scheduler;
@SuppressWarnings("unchecked")
public boolean start() {
try {
loadProperties();
} catch (IOException e) {
logger.error(e.getMessage());
return false;
}
if (properties == null) {
return false;
}
schedulerFactory = new StdSchedulerFactory();
try {
scheduler = schedulerFactory.getScheduler();
} catch (SchedulerException e) {
logger.error(e.getMessage());
return false;
}
if (scheduler == null) {
logger.error("scheduler is null");
return false;
}
Enumeration |
|