java任务调度框架Quartz案例教程

pom.xml引入quartz依赖:

<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>`

org.quartz-scheduler
quartz-jobs
2.2.1

Quartz是一个可配置化的应用程序。配置Quartz的方式是使用quartz.properties配置文件。
quartz.properties
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
编写第一个quartz示例程序
第一步:编写一个job类,需要实现org.quartz.Job接口
这里编写一个样例,该任务只做一件事,就是打印任务执行时间以及汇报任务已经执行。Hello.Java代码如下:

package org.byron4j.quartz;

import org.byron4j.utils.DateUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**

  • 实现org.quartz.Job接口,声明该类是一个可执行任务类
  • @author Administrator

*/
public class HelloJob implements Job {

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    System.out.println("现在是北京时间:" + DateUtil.getCurrDateTime() + " - helloJob任务执行");
}

}

第二步:编写日期时间工具类,提供获取不同格式时间的公共方法
DateUtil.java代码如下:

package org.byron4j.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
private static SimpleDateFormat officerSdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

public static String getCurrDateTime(){
    return officerSdf.format(new Date());
}

}

第三步:使用job、trigger、schedule调用定时任务
在该实例中我们使用了静态引入,引入了3个静态方法

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

你也可以分别使用替代,视个人习惯选择:

JobBuilder.newJob(…)
SimpleScheduleBuilder.simpleSchedule(…)
TriggerBuilder.newTrigger(…)

QuartzTest.java代码如下:

package org.byron4j.quartz;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzTest {

public static void main(String[] args) {

    try {

        //从调度程序工厂获取一个调度程序的实例
        Scheduler  scheduler  = StdSchedulerFactory.getDefaultScheduler();

        //显示调度程序的名称(这里会展示我们在quartz.properties文件中的名称)
        System.out.println("scheduleName = " + scheduler.getSchedulerName());


        /** 重要:
         *  定义一个job,并绑定到我们自定义的HelloJob的class对象
         *  这里并不会马上创建一个HelloJob实例,实例创建是在scheduler安排任务触发执行时创建的
         *  这种机制也为后面使用Spring集成提供了便利
         */
          JobDetail job = newJob(HelloJob.class)
              .withIdentity("job1", "group1")
              .build();

          // 声明一个触发器,现在就执行(schedule.start()方法开始调用的时候执行);并且每间隔2秒就执行一次
          Trigger trigger = newTrigger()
              .withIdentity("trigger1", "group1")
              .startNow()
                    .withSchedule(simpleSchedule()
                      .withIntervalInSeconds(2)
                      .repeatForever())            
              .build();

          // 告诉quartz使用定义的触发器trigger安排执行任务job
          scheduler.scheduleJob(job, trigger);

        //启动任务调度程序,内部机制是线程的启动
        scheduler.start();

        //关闭任务调度程序,如果不关闭,调度程序schedule会一直运行着
        //scheduler.shutdown();

    } catch (SchedulerException e) {

        e.printStackTrace();
    }
}

}

第四步:执行调用你的定时任务
运行QuartzTest.java的main方法,我们可以看到控制台输出,每隔2秒就执行了我们预先安排的打印时间和工作内容的任务:

scheduleName = MyScheduler
现在是北京时间:2016-11-05 13:08:30 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:32 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:34 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:36 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:38 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:40 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:42 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:44 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:46 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:48 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:50 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:52 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:54 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:56 - helloJob任务执行
现在是北京时间:2016-11-05 13:08:58 - helloJob任务执行
现在是北京时间:2016-11-05 13:09:00 - helloJob任务执行

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值