1.测试用例
package com.ninemax.application.quartz.loader;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import org.quartz.JobDetail;
import com.ninemax.application.quartz.model.MyJob;
/**
* 利用JAVA反射调用定时任务
*
* @author Darker
*
*/
@SuppressWarnings("all")
public class ClassLoaderTest {
public static void main(String[] args) {
// 读取指定JAR包
File jarFile= new File("E:/Worksapces2015/Vip/WebRoot/WEB-INF/lib/quartz-1.8.3.jar");
// 读取指定的类
File classFile= new File("E:/Worksapces2015/Vip/src/com/ninemax/application/quartz/model/MyJob.class");
try {
// 加载指定JAR包
URLClassLoader loader=new URLClassLoader(new URL[]{jarFile.toURL()});
// 加载指定的类
URLClassLoader classLoader= new URLClassLoader(new URL[]{classFile.toURL()});
// 加载指定JAR包中的类
Class SchedulerFactoryClazz=loader.loadClass("org.quartz.impl.StdSchedulerFactory");
Class CronTriggerClazz=loader.loadClass("org.quartz.CronTrigger");
Class JobDetailClazz=loader.loadClass("org.quartz.JobDetail");
Class SchedulerClazz=loader.loadClass("org.quartz.Scheduler");
Class TriggerClazz=loader.loadClass("org.quartz.Trigger");
// 加载MyJob中的类
Class MyJobClazz=classLoader.loadClass("com.ninemax.application.quartz.model.MyJob");
// 调度工厂SchedulerFactory初始化
Object schedulerFactory = SchedulerFactoryClazz.newInstance();
// 调度器Scheduler初始化 11
//Object scheduler=SchedulerClazz.newInstance();
// 通过调度工厂获取调度器scheduler
Object scheduler=schedulerFactory.getClass().getDeclaredMethod("getScheduler", null);
// 初始化JobDetail实例
Object jobDetail=JobDetailClazz.newInstance();
Constructor jobDetailMethod=jobDetail.getClass().getConstructor(String.class,String.class,Class.class);
// 构造方法jobDetail填写参数
jobDetailMethod.newInstance("job1","group1",MyJobClazz);
//jobDetailMethod.invoke();
// 初始化实例
Object cronTrigger=CronTriggerClazz.newInstance();
// cronTrigger构造方法
Constructor cronTriggerMethod=cronTrigger.getClass().getConstructor(String.class,String.class);
// 构造方法cronTrigger填写参数
cronTriggerMethod.newInstance("cronTrigger","triggerGroup");
// 获取CronExpression方法
Method cronExpression = cronTrigger.getClass().getDeclaredMethod("setCronExpression", String.class);
// 设置cronExpression参数
cronExpression.invoke(cronTrigger, "0 30 18 * * ? *");
// 获取scheduleJob方法
Method scheduleJob=scheduler.getClass().getDeclaredMethod("scheduleJob", JobDetailClazz,TriggerClazz);
// 设置参数
scheduleJob.invoke(scheduler, jobDetail,cronTrigger);
// 获取start方法
Method start=scheduler.getClass().getDeclaredMethod("start", null);
// 启动调度
start.invoke(scheduler, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.domain
package com.ninemax.application.quartz.model;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("Quartz测试时间:" + new Date());
}
}
3.quartz-1.8.3.jar下载地址:
http://mvnrepository.com/artifact/org.quartz-scheduler/quartz
来源:oschina
链接:https://my.oschina.net/u/2400848/blog/678447