Java中的作业调度——quartz 博客分类: Java Java作业调度第三方框架
程序员文章站
2024-03-23 11:01:16
...
1.quartz简介
关于quartz的介绍,请链接如下地址:http://baike.baidu.com/link?url=Gl0_SCWPJ8owyjM2ORBWd0En0Y2ppGZuGTfwAJFb5dA4OyrlEVmYAR6yG4QtG8O1x9j1U6JMJPXvSbRBU1Bn0RcZOctgFn_hdIG_-HtGYm7。
此处不再累述!
2. 实例
功能:每隔五秒打印出信息
代码:
package com.zh.quartz; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class Quartz implements Job{ @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { // TODO Auto-generated method stub System.out.println("Hello quartz" + "--->" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } }
package com.zh.quartz; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleTrigger; import org.quartz.impl.StdSchedulerFactory; public class QuartzMain { public static void main(String[] args){ QuartzMain quartz_main = new QuartzMain(); try{ quartz_main.startSchedule(); }catch(Exception e){ e.printStackTrace(); } } @SuppressWarnings("static-access") protected void startSchedule() throws SchedulerException{ //调度器 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //具体执行类 JobDetail jobDetail = new JobDetail("firstJob", Scheduler.DEFAULT_GROUP,Quartz.class); //触发器定义每五秒一次 SimpleTrigger simpleTrigger = new SimpleTrigger("trigger",scheduler.DEFAULT_GROUP,10,5000); scheduler.scheduleJob(jobDetail,simpleTrigger); scheduler.start(); } }