SimpleJob.java
package com.test.job;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class SimpleJob implements Job{
public void execute(JobExecutionContext context)
throws JobExecutionException {
// your business logic
// ...
System.out.println("simpleJob running ....");
}
}
TestSimpleJob.java
package com.test.job;
import java.text.ParseException;
import org.quartz.CronExpression;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class TestSimpleJob {
public static void main(String[] args){
try {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
String jobName = "simpleJob";
String groupName = "DEFAULT";
//删除作业
if(scheduler.getJobDetail(jobName,groupName) != null){
scheduler.deleteJob(jobName, groupName);
}
//作业的详细信息
//任务名称,任务组名称,任务实现类
JobDetail jobDetail = new JobDetail(jobName,groupName,SimpleJob.class);
//创建简单触发器
SimpleTrigger simpleTrigger = new SimpleTrigger("simpleTrigger",null);
simpleTrigger.setRepeatCount(100); // 调用100次
simpleTrigger.setRepeatInterval(5*1000); //每5秒钟调用一次
//注册作业
scheduler.scheduleJob(jobDetail, simpleTrigger);
CronTrigger cronTrigger = new CronTrigger("cronTrigger",null);
CronExpression cronExpression = null;
try {
cronExpression = new CronExpression("0 0 12 * * ?"); //每天12点运行
cronTrigger.setCronExpression(cronExpression);
//注册作业
scheduler.scheduleJob(jobDetail, cronTrigger);
if(! scheduler.isShutdown()){
scheduler.start();
}
} catch (ParseException e) {
e.printStackTrace();
}
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}