Quartz 之 HelloWorld
Posted satisfysmy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz 之 HelloWorld相关的知识,希望对你有一定的参考价值。
package com.satisfy; 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 { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("current times is"+sdf.format(date)); //编写业务逻辑 System.out.println("Hello World!"); } } package com.satisfy; 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实例,将该实例与HelloJob Class绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class) .withIdentity("myJob").build(); System.out.println("jobDetail‘s name"+jobDetail.getKey().getName()); System.out.println("jobDetail‘s group"+jobDetail.getKey().getGroup()); System.out.println("jobDetail‘s jobClass "+jobDetail.getKey().getClass()); // 创建一个Trigger 实例,定义改Job 立即执行,并且每割2 秒重复执行一次,直到永远 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(); Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("current times is"+sdf.format(date)); scheduler.scheduleJob(jobDetail, trigger); } }
以上是关于Quartz 之 HelloWorld的主要内容,如果未能解决你的问题,请参考以下文章