首先参考文章
http://sulaohuai.blog.51cto.com/10674946/1702625
里面的示例写的不是太全,自己揣测大致补全了代码,亲测可用。
1、Main方法的测试类
package com.ims.scheduler;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.quartz.*;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws SchedulerException, InterruptedException {
// Job的配置信息,可以从数据库或配置文件中获取
List<JobParameter> list = new ArrayList<JobParameter>();
String jobGroup = "JobGroup1";
for (int i = 1; i < 4; i++) {
JobParameter param = new JobParameter();
param.setJobName("Job" + i);
param.setJobGroup(jobGroup);
param.setCronExpression("0/5 * * * * ?");
param.setDescription("Execute job " + i + " every 5 seconds ...");
list.add(param);
}
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
try {
Scheduler scheduler = (Scheduler) context.getBean("schedulerFactoryBean");