• quartz定时任务_演示版01


    预置:

    quartz共4个设计模式:Builder模式,Factory模式,组件模式,链式模式

    体系结果:核心3个类JobDetail , trigger , scheduler

    重要组成部分:Job接口、JobBuilder、JobDetail、JobStore

                             TriggerBuilder、Trigger

                                             Scheduler

                             接听器:JobListener、TriggerListener、SchedulerListener

                             ThreadPool (线程池,解决并发问题)

                              Calendar (排除日程表中某些时间点)

    =====================================================================

    1.新建maven项目

    2.在http://www.mvnrepository.com/中找到quartz的jar包依赖代码引入pom.xml文件

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

    3.实现job接口的execute方法:

    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 {
    //实现job接口execute方法具体内容
    public void execute(JobExecutionContext arg0) throws JobExecutionException{
    //打印当前执行时间 格式:2017-01-01 00:00:00
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("current exec time is :"+sdf.format(date));

    //编写具体业务逻辑
    System.out.println("helloJob类 实现了 Job接口的execute方法");
    }
    }

    4.main方法中实现任务的创建运行

    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;

    /*
    * 20180103 ver1.0
    */
    public class helloSchedule {

    public static void main(String[] args) throws SchedulerException {
    // TODO Auto-generated method stub

    // 创建一个JobDetail实例 将HelloJob类添加到JobDetail中
    JobDetail jobDetail = JobBuilder.newJob(helloJob.class)
    .withIdentity("myJob", "jobDetailGroup")
    .build();
    //创建一个Trigger实例,定义该job立即执行,每隔两秒钟重复执行一次,一直不停
    Trigger trigger = TriggerBuilder.newTrigger()
    .withIdentity("myTrigger","triggerGroup")
    .startNow().withSchedule(
    SimpleScheduleBuilder.simpleSchedule()
    .withIntervalInSeconds(2)
    .repeatForever())
    .build();
    //创建Schduler实例
    SchedulerFactory sfFactory = new StdSchedulerFactory();
    Scheduler scheduler = sfFactory.getScheduler();
    scheduler.start();

    //打印当前时间
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("current scheduler time is :"+sdf.format(date));
    scheduler.scheduleJob(jobDetail, trigger);

    }

    }

    5.运行结果:

    current scheduler time is :2018-01-03 15:08:54
    current exec time is :2018-01-03 15:08:54
    helloJob类 实现了 Job接口的execute方法
    current exec time is :2018-01-03 15:08:56
    helloJob类 实现了 Job接口的execute方法
    current exec time is :2018-01-03 15:08:58
    helloJob类 实现了 Job接口的execute方法
    current exec time is :2018-01-03 15:09:00
    helloJob类 实现了 Job接口的execute方法
    current exec time is :2018-01-03 15:09:02
    helloJob类 实现了 Job接口的execute方法
    current exec time is :2018-01-03 15:09:04
    helloJob类 实现了 Job接口的execute方法
    current exec time is :2018-01-03 15:09:06
    helloJob类 实现了 Job接口的execute方法

  • 相关阅读:
    Codeforces Round #644 (Div. 3) A~G
    西安邮电大学第五届ACM-ICPC校赛(同步赛) B(拓扑排序)
    Codeforces Round #642 (Div. 3)A~D
    Codeforces Round #641 (Div. 2)A~D
    Codeforces Round #634 (Div. 3)A~E
    Educational Codeforces Round 85 (Rated for Div. 2)ABCD
    Codeforces Round #631 (Div. 2) ABD
    Codeforces Round #629 (Div. 3) E
    Educational Codeforces Round 84 (Rated for Div. 2) E
    yp训练赛3/21
  • 原文地址:https://www.cnblogs.com/imlvbu/p/8184405.html
Copyright © 2020-2023  润新知