与JFinal的集成
JFinal有个插件可以使用以集成。
在 public void configPlugin(Plugins me)中加载插件
QuartzPlugin quartzPlugin = new QuartzPlugin("job.properties"); me.add(quartzPlugin);配置文件如下
#Job EveryDay Day : 00:10:00 every day a.job=cn.server.service.ds.JobByDay a.cron=0 10 0 * * ? a.enable=true #Job EveryMonth : 03:00:00 1th every month b.job=cn.server.service.ds.JobByMonth b.cron=0 0 3 1 * ? b.enable=true #Job Org Data : 02:00:00 every day c.job=cn.server.service.ds.OrgDataImportService c.cron=0 30 5 * * ? c.enable=true #Job clean sms code fro login Day : 00:10:00 every day d.job=cn.server.service.ds.JobClear d.cron=0 10 0 * * ? d.enable=truejob就是一个实现了Job接口的类,用于写你的实际任务。
另外一个实时的Jfinal插件,原文http://my.oschina.net/superbigfu/blog/206630
QuartzPlugin
import java.util.Properties;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import xidian.wwf.ivc.quartz.QuartzFactory;
import xidian.wwf.ivc.utils.PropertiesUtil;
import com.jfinal.plugin.IPlugin;
/** * Quartz插件 * @author WWF */
public class QuartzPlugin implements IPlugin{
/**默认配置文件**/
private String config = "quartz.properties";
public QuartzPlugin(){
}
public QuartzPlugin(String config){
this.config = config;
}
@Override
public boolean start() {
try {
//加载配置文件
Properties props = PropertiesUtil.loadPropertyFile(config);
//实例化
QuartzFactory.sf = new StdSchedulerFactory(props);
//获取Scheduler
Scheduler sched = QuartzFactory.sf.getScheduler();
sched.start();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean stop() {
try {
QuartzFactory.sf.getScheduler().shutdown();
QuartzFactory.sf = null;
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
QuartzFactory是我自己写的一个开始job任务的管理类,如下
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import xidian.wwf.ivc.utils.TimeUtil;
/** * QuartzFactory * @author WWF */
public class QuartzFactory {
public static SchedulerFactory sf;
/** * 定时开始任务 * @param startTime * @param id * @param name * @param group * @param jobClass */
public static void startJobOnce(String startTime, int id,String name,String group,Class<? extends Job> jobClass){
try {
Scheduler sched = sf.getScheduler();
// define the job and tie it to our HelloJob class
JobDetail job = newJob(jobClass)
.withIdentity("job_"+name+"_"+id, "group_"+group+"_"+id)
.requestRecovery()
.build();
job.getJobDataMap().put(group+"_"+name, id);
// 定时执行
SimpleTrigger trigger = (SimpleTrigger) newTrigger()
.withIdentity("trigger_"+name+"_"+id, "group_"+group+"_"+id)
.startAt(TimeUtil.StringToDate2(startTime))
.build();
sched.scheduleJob(job, trigger);
sched.start();
} catch (Exception e) {
e.printStackTrace();
}
}
/** * 停止任务 * @param name * @param group * @param id */
public static void stopJob(String name,String group,int id){
try {
if (sf!=null) {
Scheduler scheduler = sf.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey("trigger_"+name+"_"+id,"group_"+ group+"_"+id);
Trigger trigger = scheduler.getTrigger(triggerKey);
if (trigger!=null) {
scheduler.pauseTrigger(triggerKey);
scheduler.unscheduleJob(triggerKey);
scheduler.deleteJob(trigger.getJobKey());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
因为有时候服务器维护,会关闭tomcat,所以我把调度任务存入到数据库中,可以中断回复,下面是我的quartz.properties配置文件和数据库连接ConnectionProvider
quartz.properties
#==================================================
# 配置实例名和id
#==================================================
org.quartz.scheduler.instanceName = myScheduler
org.quartz.scheduler.instanceId: my
org.quartz.scheduler.skipUpdateCheck: true
#==================================================
# 配置线程池
#==================================================
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 5
org.quartz.threadPool.threadPriority: 5
#==================================================
# 配置JobStore
#==================================================
org.quartz.jobStore.misfireThreshold: 600000
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties=false
org.quartz.jobStore.dataSource=my
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.isClustered=true
#==================================================
# 配置数据库
#==================================================
org.quartz.dataSource.my.connectionProvider.class = QuartzConnectionProvider
QuartzConnectionProvider
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.quartz.utils.ConnectionProvider;
import xidian.wwf.ivc.utils.PropertiesUtil;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.plugin.druid.DruidPlugin;
/** * 自定义QuartzConnectionProvider * @author WWF */
public class QuartzConnectionProvider implements ConnectionProvider{
private static final String DB_CONFIG="databaseConfig.properties";
private DruidPlugin druidPlugin;
@Override
public Connection getConnection() throws SQLException {
return druidPlugin.getDataSource().getConnection();
}
@Override
public void initialize() throws SQLException {
Properties properties = PropertiesUtil.loadPropertyFile(DB_CONFIG);
druidPlugin = new DruidPlugin(
properties.getProperty("jdbcUrl"),
properties.getProperty("user"),
properties.getProperty("password"),
properties.getProperty("jdbcDriverClass"));
// StatFilter提供JDBC层的统计信息
druidPlugin.addFilter(new StatFilter());
// WallFilter的功能是防御SQL注入攻击
WallFilter wallFilter = new WallFilter();
wallFilter.setDbType("mysql");
druidPlugin.addFilter(wallFilter);
druidPlugin.start();
}
@Override
public void shutdown() throws SQLException {
druidPlugin.stop();
}
}
QuartzConnectionProvider中使用了DruidPlugin,动态加载数据库配置文件,数据库更换只需要修改数据库配置文件即可
最后,新建一个TestJob
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import xidian.wwf.ivc.utils.TimeUtil;
public class TestJob implements Job{
public TestJob(){
}
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("开始时间="+TimeUtil.getTimeAll());
try {
//JobDataMap dataMap= arg0.getJobDetail().getJobDataMap();
System.out.println("哇哈哈");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("结束时间="+TimeUtil.getTimeAll());
}
}
在afterJFinalStart中新建测试任务
//测试quartz
String startTime = "2014-03-09 09:54:00";
QuartzFactory.startJobOnce(startTime, 5, "test", "testgroup", TestJob.class);
到点了就执行TestJob了。大家还可以根据需要,在QuartzFactory中添加间隔定时调度等,因为我项目就一个定点调度,所以基本都是执行一次即可。
与Spring的整合
Maven来管理项目,需要的Jar包我给大家贴出来。
quartz-1.8.5.jar
commons-logging.jar
spring-core-3.0.5.RELEASE.jar
spring-beans-3.0.5.RELEASE.jar
spring-context-3.0.5.RELEASE.jar
spring-context-support-3.0.5.RELEASE.jar
spring-asm-3.0.5.RELEASE.jar
spring-expression-3.0.5.RELEASE.jar
spring.transaction-3.0.5.RELEASE.jar
spring-web-3.0.5.RELEASE.jar
Maven的pom.xml的配置:
首先我们来写一个被调度的类: