import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job{
public void execute(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("--------------------------------");
//打印当前的执行时间
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("Current Exec Time is:"+format.format(date));
//编写具体的业务逻辑
System.out.println("Hello Word!");
System.out.println("--------------------------------");
}
}
package com.quartz;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class HelloScheduler {
public static void main(String[] args) throws SchedulerException {
// 创建一个jobDetail实例,将该实例与Hello class 绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build();
// 创建一个Trigger实例,定义该job立即执行,并隔着两秒种重复执行
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1").startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever()).build();
//创建Scheduler实例
SchedulerFactory sfact = new StdSchedulerFactory();
Scheduler scheduler = sfact.getScheduler();
scheduler.start();
System.out.println("********************************");
//打印当前的执行时间
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("Current Exec Time is:"+format.format(date));
scheduler.scheduleJob(jobDetail, trigger);
System.out.println("********************************");
}
}