需要的jar包为:quartz-1.6.5.jar、commons-logging-1.1.3.jar、commons-collections-3.1.jar
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.SchedulerException;
public class QuartzTest {
public static void main(String[] args) throws SchedulerException, ParseException {
SimpleDateFormat DateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date d = new Date();
String returnstr = DateFormat.format(d);
TestJob job = new TestJob();
String job_name ="11";
System.out.println(returnstr+ "【系统启动】");
QuartzManager.addJob(job_name,job,"0/2 * 5-8 * * ?"); //每天5点至8点每隔2秒执行一次
}
}
import java.text.ParseException;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzManager {
private static SchedulerFactory sf = new StdSchedulerFactory();
private static String JOB_GROUP_NAME = "group1";
private static String TRIGGER_GROUP_NAME = "trigger1";
public static void addJob(String jobName, Job job, String time)
throws SchedulerException, ParseException {
Scheduler sched = sf.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME,
job.getClass());
CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);
trigger.setCronExpression(time);
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown())
sched.start();
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class TestJob implements Job {
SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
String returnstr = DateFormat.format(d);
public void execute(JobExecutionContext arg0) throws JobExecutionException {
//需要执行业务内容
System.out.println(returnstr+"★★★★★★★★★★★");
}
}