• Quartz任务调度框架的简单应用


    首先创建Maven项目并添加依赖

     1 <!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
     2         <dependency>
     3             <groupId>org.quartz-scheduler</groupId>
     4             <artifactId>quartz</artifactId>
     5             <version>2.2.3</version>
     6         </dependency>
     7         <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
     8         <dependency>
     9             <groupId>org.slf4j</groupId>
    10             <artifactId>slf4j-nop</artifactId>
    11             <version>1.7.7</version>
    12 
    13         </dependency>

    创建一个需要执行的任务取名为HelloJob.java

    public class HelloJob implements Job {
        /*用来传值
            private String message;
        private Float FloatJobValue;
        private Double DoubleTriggerValue;
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public Float getFloatJobValue() {
            return FloatJobValue;
        }
    
        public void setFloatJobValue(Float FloatJobValue) {
            this.FloatJobValue = FloatJobValue;
        }
    
        public Double getDoubleTriggerValue() {
            return DoubleTriggerValue;
        }
    
        public void setDoubleTriggerValue(Double DoubleTriggerValue) {
            this.DoubleTriggerValue = DoubleTriggerValue;
        }*/
    
        public void execute(JobExecutionContext context) throws JobExecutionException {
            // TODO Auto-generated method stub
            // 打印当前的执行时间
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Date date = new Date();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("Current Exec Time Is:" + sf.format(date));
            // 编写具体的业务逻辑
            //
             System.out.println("Hello World");
            //
            /*
             * JobKey key = context.getJobDetail().getKey();
             * System.out.println("My name and group are:" + key.getName() + ":" +
             * key.getGroup()); TriggerKey trkey = context.getTrigger().getKey();
             * System.out.println("My name and group are:" + trkey.getName() + ":" +
             * trkey.getGroup()); //JobDataMap dataMap =
             * context.getMergedJobDataMap(); //JobDataMap tdataMap =
             * context.getTrigger().getJobDataMap(); String Msg =
             * dataMap.getString("message"); Float jobFloatValue =
             * dataMap.getFloat("FloatJobValue"); //String triggerMsg =
             * dataMap.getString("message"); Double triggerDoubleValue =
             * dataMap.getDouble("DoubleTriggerValue");
             * //System.out.println("JobMsg is:"+jobMsg);
             * System.out.println("jobFloatValue is:"+FloatJobValue);
             * System.out.println("Msg is:"+message);
             * System.out.println("triggerDoubleValue is:"+DoubleTriggerValue);
             */
            //
            /*Trigger currentTrigger = context.getTrigger();
            System.out.println("Start Time Is:" + sf.format(currentTrigger.getStartTime()));
            System.out.println("End Time Is:" + sf.format(currentTrigger.getEndTime()));
            JobKey jobKey = currentTrigger.getJobKey();
            System.out.println("JobKey info---jobName:" + jobKey.getName() + "jobGroup:" + jobKey.getGroup());*/
            
        }
    
    }            

    HelloScheduler.java

     1 public class HelloScheduler {
     2     public static void main(String[] args) throws SchedulerException, InterruptedException {
     3         // 打印当前的执行时间
     4         Date date = new Date();
     5         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     6         System.out.println("Current Time Is:" + sf.format(date));
     7         // 创建一个JobDetail实例,将该实例与HelloJob Class绑定
     8         JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
     9                 .withIdentity("myJob")
    10                 /*.usingJobData("message", "hello myJob1")
    11                 .usingJobData("FloatJobValue", 3.14F)*/
    12                 .build();
    13         //获取距离当前时间4秒后的时间
    14         date.setTime(date.getTime()+4000L);
    15         //获取距离当前时间6秒后的时间
    16         Date endDate = new Date();
    17         endDate.setTime(endDate.getTime() + 6000);
    18         /*System.out.println("jobDetail's name:" + jobDetail.getKey().getName());
    19         System.out.println("jobDetail's group:" + jobDetail.getKey().getGroup());
    20         System.out.println("jobDetail's jobClass:" + jobDetail.getJobClass().getName());*/
    21         // 创建一个trigger实例,定义该Job立即执行,并且每隔两秒钟重复执行一次,直到永远
    22         //距离当前时间4秒钟后首次执行,之后每隔两秒钟
    23         //SimpleTrigger
    24         /*SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder.newTrigger()
    25                 .usingJobData("message", "hello myTrigger1")
    26                 .usingJobData("DoubleTriggerValue",2.0D)
    27                 .withIdentity("myTrigger", "group1")
    28                 //设置开始时间结束时间,优先于执行次数
    29                 .startAt(date)
    30                 .endAt(endDate)
    31                 //每两秒钟执行一次
    32                 .withSchedule(
    33                         SimpleScheduleBuilder
    34                             .simpleSchedule()
    35                             .withIntervalInSeconds(2)
    36                             .withRepeatCount(3)
    37                                     .repeatForever())
    38                 .build();*/
    39         
    40         // CronTrigger
    41         CronTrigger trigger = (CronTrigger) TriggerBuilder
    42                 .newTrigger()
    43                 .withIdentity("myTrigger", "group1")
    44                 // 设置开始时间结束时间,优先于执行次数
    45                 //.startAt(date).endAt(endDate)
    46                 // 每两秒钟执行一次
    47                 .withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?"))
    48                 .build();
    49         //1.2017年内每天10点15分触发一次
    50         // 0 15 10 ? * * 2017
    51         //2.每天的14点整至14点59分55秒,以及18点整至18点59分55秒,每5秒钟触发一次
    52         //0/5 * 14,18 * * ?
    53         //3.每月周一至周五的10点15分触发一次
    54         //0 15 10 ? * 2-6
    55         //4.每月最后一天的10点15分触发一次
    56         //0 15 10 L * ?
    57         //5.每月第三个周五的10点15分触发一次
    58         //0 15 10 ? * 6#3
    59         // 创建scheduler实例
    60         SchedulerFactory sfact = new StdSchedulerFactory();
    61         Scheduler scheduler = sfact.getScheduler();
    62         scheduler.start();
    63         System.out.println("Scheduled time is :"+sf.format(scheduler.scheduleJob(jobDetail, trigger)));
    64         //scheduler执行两秒后挂起
    65         /*Thread.sleep(2000L);
    66         scheduler.shutdown(false);
    67         System.out.println("scheduler is shut down?" + scheduler.isShutdown());*/
    68 /*        scheduler.standby();
    69         Thread.sleep(3000L);
    70         scheduler.start();*/
    71     }
    72 }

    配置文件quartz.properties

    org.quartz.scheduler.instanceName: DefaultQuartzScheduler
    org.quartz.scheduler.rmi.export: false
    org.quartz.scheduler.rmi.proxy: false
    org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
    
    org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
    org.quartz.threadPool.threadCount: 10
    org.quartz.threadPool.threadPriority: 5 
    #0-9
    org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
    
    org.quartz.jobStore.misfireThreshold: 60000
    
    org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
  • 相关阅读:
    FTP 协议和 HTTP 协议的比较
    HttpURLConnection的post请求,什么时候发出,writeData存在什么地方
    装饰器
    函数参数以及名称空间作用域
    函数的调用
    函数的返回值
    定义函数的三种方式
    函数
    day05
    day04
  • 原文地址:https://www.cnblogs.com/wwwjie/p/8277180.html
Copyright © 2020-2023  润新知