import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloQuartzJob implements Job {
private static int count = 0;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDetail jobDetail = context.getJobDetail();
String value = jobDetail.getJobDataMap().getString("color");
System.out.println("value = " + value);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date());
System.out.println((count++) + ", " + date + " hello world " + jobDetail.getKey());
}
}
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class CronScheduleBuilderTest {
public static void main(String[] args) {
// 描述Job实现类及其他的一些静态信息
JobDetail jobDetail = JobBuilder.newJob(HelloQuartzJob.class)
.withIdentity("testjob_1", "group_1").build();
// 描述触发Job执行的时间触发规则
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("trigger_1", "group_1")
.startNow()
// 每5秒执行一次
.withSchedule(CronScheduleBuilder.cronSchedule(" */5 * * * * ? "))
.build();
SchedulerFactory sf = new StdSchedulerFactory();
try {
// 运行容器,使用SchedulerFactory创建Scheduler实例
Scheduler scheduler = sf.getScheduler();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
参考博客:
http://blog.csdn.net/a67474506/article/details/38316273