项目需要用一些少量的后台调度任务,上周末再完全用jfinal重现项目的时候把quartz替换成了cron4j。虽然以前没用过cron4j但是感觉自己需要的简单功能用这个轻巧的jar包就足够了。刚开始是在一个servlet 监听器里面启动调度程序。后来想完全可以依赖于Jfinal的插件机制做一点点小扩展。献丑直接上代码。
另外cron4j替换quartz会不会出什么问题我也不清楚。求指教。
如果需要的话再弄个QuartzPlugIn
package com.jfinal.plugin.cron;import it.sauronsoftware.cron4j.Scheduler;import com.jfinal.plugin.IPlugin;public class Cron4jPlugIn implements IPlugin { private final Scheduler scheduler = new Scheduler(); @Override public boolean start() { scheduler.start(); return true; } @Override public boolean stop() { scheduler.stop(); return true; } public void addTask(String cronExpress, Runnable task) { scheduler.schedule(cronExpress, task); }}
/** * 配置插件 */ public void configPlugin(Plugins me) { C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password")); me.add(c3p0Plugin); ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin); me.add(arp); arp.addMapping("mobilebind", MobileBind.class); arp.addMapping("sendrecord", SendRecord.class); arp.setShowSql(true); SqlReporter.setLogger(true); Cron4jPlugIn cron4jPlugIn = new Cron4jPlugIn(); if (getPropertyToBoolean("SendStudentInfo.enable") == true) { cron4jPlugIn.addTask(getProperty("SendStudentInfo.cron"), new SendStudentInfoTask()); } if (getPropertyToBoolean("SmsUpReceive.enable") == true) { cron4jPlugIn.addTask(getProperty("SmsUpReceive.cron"), new SmsUpReceiveTask()); } me.add(cron4jPlugIn); }
a_little_config.txt
SmsUpReceive.cron=* * * * *SmsUpReceive.enable=trueSendStudentInfo.cron=0 * * * *SendStudentInfo.enable=true