• Quartz -第一篇-入门


    特点:分布式+集群
    设计模式:
      工厂模式
      builder模式
      组件模式
      链式写法
    核心概念:调度器,任务,触发器
     
    使用:
      依赖:
      
             <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz</artifactId>
                <version>2.2.3</version>
            </dependency>

    创建一个实例实现Job接口,实现其中的execute方法,将你要做的任务放到此方法

    package quartz.job;
    
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    import javax.xml.crypto.Data;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * ClassName: HelloJob<br/>
     * Description: <br/>
     * date: 2019/1/22 11:19 AM<br/>
     *
     * @author chengluchao
     * @since JDK 1.8
     */
    
    public class HelloJob implements Job {
    
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("Current Exec Time Is:" + simpleDateFormat.format(date));
            System.out.println("Hello word!");
        }
    }

    编写调用类

    package quartz.job;
    
    import org.quartz.JobBuilder;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerFactory;
    import org.quartz.SimpleScheduleBuilder;
    import org.quartz.Trigger;
    import org.quartz.TriggerBuilder;
    import org.quartz.impl.StdSchedulerFactory;
    
    /**
     * ClassName: HelloScheduler<br/>
     * Description: <br/>
     * date: 2019/1/22 11:23 AM<br/>
     *
     * @author chengluchao
     * @since JDK 1.8
     */
    
    public class HelloScheduler {
        public static void main(String[] args) throws Exception {
            //创建一个JobDetail实例,将HelloJob.class绑定
            JobDetail jobDetail = JobBuilder
                    .newJob(HelloJob.class)
                    .withIdentity("myJob", "group1")
                    .build();
            //创建一个Trigger实例,定义该job立即执行,并且每隔两秒钟重复执行一次,直到永远
            Trigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("myTrigger", "group1")
                    .startNow()//立即执行
                    .withSchedule(
                            SimpleScheduleBuilder
                                    .simpleSchedule()
                                    .withIntervalInSeconds(2)//两秒一次
                                    .repeatForever())//直到永远
                    .build();
            //创建Schduler实例
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.start();
            scheduler.scheduleJob(jobDetail, trigger);
        }
    }

    14:56:17.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.myJob', class="quartz".job.HelloJob
    14:56:17.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    14:56:17.296 [DefaultQuartzScheduler_Worker-10] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.myJob
    Current Exec Time Is:2019-01-22 14:56:17
    Hello word!
    14:56:19.295 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.myJob', class="quartz".job.HelloJob
    14:56:19.295 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    14:56:19.295 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.myJob
    Current Exec Time Is:2019-01-22 14:56:19
    Hello word!
    14:56:21.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.myJob', class="quartz".job.HelloJob
    14:56:21.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
    14:56:21.296 [DefaultQuartzScheduler_Worker-2] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.myJob
    Current Exec Time Is:2019-01-22 14:56:21
    Hello word!

    补充:

    jobDetail的重要属性:
      name
      group 默认"DEFAULT"
      jobClass
      jobDataMap:
          在任务调度的时候,jobDataMap可以进行存储一些信息;
     
    打印jobDetail的信息
            System.out.println("jobDetail's name : " + jobDetail.getKey().getName());
            System.out.println("jobDetail's gruop : " + jobDetail.getKey().getGroup());
            System.out.println("jobDetail's jobClass : " + jobDetail.getKey().getClass());

    jobDataMap的使用:

    public class HelloScheduler {
        public static void main(String[] args) throws Exception {
            //创建一个JobDetail实例,将HelloJob.class绑定
            JobDetail jobDetail = JobBuilder
                    .newJob(HelloJob.class)
                    .withIdentity("myJob")
                    .usingJobData("message","hello myJob1")
                    .usingJobData("floatJobvalue",3.14f)
                    .build();
    
            //创建一个Trigger实例,定义该job立即执行,并且每隔两秒钟重复执行一次,直到永远
            Trigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("myTrigger", "group1")
                    .usingJobData("message","hello myTrigger1")
                    .usingJobData("doubleJobvalue",885.02)
                    .startNow()//立即执行
                    .withSchedule(
                            SimpleScheduleBuilder
                                    .simpleSchedule()
                                    .withIntervalInSeconds(2)//两秒一次
                                    .repeatForever())//直到永远
                    .build();
            //创建Schduler实例
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = schedulerFactory.getScheduler();
            scheduler.start();
            scheduler.scheduleJob(jobDetail, trigger);
        }
    }
    public class HelloJob implements Job {
    
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("Current Exec Time Is:" + simpleDateFormat.format(date));
            System.out.println("Hello word!");
    
    
            JobKey key = jobExecutionContext.getJobDetail().getKey();
            System.out.println("JobDetailKey : " + key.getName() + "JobDetailValue : " + key.getGroup());
            TriggerKey triggerKey1 = jobExecutionContext.getTrigger().getKey();
            System.out.println("triggerKey1 : " + triggerKey1.getName() + "triggervalue1 : " + triggerKey1.getGroup());
    
    
            JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
            JobDataMap tdataMap = jobExecutionContext.getTrigger().getJobDataMap();
    
            System.out.println(dataMap.get("message"));
            System.out.println(tdataMap.get("message"));
    
    
            JobDataMap dataMap2 = jobExecutionContext.getMergedJobDataMap();
            //Trigger会覆盖JobDetail的信息
            System.out.println(dataMap2.get("message"));
            System.out.println(dataMap2.get("message"));
    
    
        }

    获取jobDataMap的值的方式2:

     在HelloJob类中定义属性,属性名和map中的key一样,加上get/set方法

    在HelloJob的类中就可以直接使用

    Trigger的方法:
    .startAt(date)//首次执行的时间
    .endAt(endDate)//最后一次执行的时间
    .startNow()//立即执行
  • 相关阅读:
    证明一下拉普拉斯的《概率分析论》观点
    Android实现小圆点显示未读功能
    命名 —— 函数的命名
    node.js 之爬虫
    ubuntu安装 tensorflow GPU
    古文(诗词文)—— 结构模式与复用
    Win10安装Ubuntu16.04 双系统
    python使用wget下载网络文件
    文字检测与识别资源
    10大深度学习架构:计算机视觉优秀从业者必备
  • 原文地址:https://www.cnblogs.com/chenglc/p/10303960.html
Copyright © 2020-2023  润新知