zoukankan      html  css  js  c++  java
  • Quartz Scheduler(2.2.1)

    简单示例

    1. maven 依赖

    <dependencies>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz-jobs</artifactId>
            <version>2.2.1</version>
        </dependency>
        
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.6</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    2. quarzt.properties(可选)

    org.quartz.scheduler.instanceName = MyScheduler
    org.quartz.threadPool.threadCount = 3
    org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

    3. Job

    package com.huey.hello.quartz;
    
    import java.util.Date;
    
    import org.quartz.Job;
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    
    import com.huey.hello.quartz.utils.DateUtils;
    
    public class HelloJob implements Job {
    
        /**
         * 实 现 org.quartz.Job 接口,并实现 execute 方法,在此方法执行业务逻辑 
         */
        public void execute(JobExecutionContext context) throws JobExecutionException {
            Date fireTime = context.getFireTime();        
            System.out.println("Hello Quartz Scheduler! " + DateUtils.dateToStr(fireTime));                
        }
        
    }

    4. Simple Code

    package com.huey.hello.quartz;
    
    import org.quartz.CronExpression;
    import org.quartz.CronScheduleBuilder;
    import org.quartz.JobBuilder;
    import org.quartz.JobDetail;
    import org.quartz.Scheduler;
    import org.quartz.SchedulerFactory;
    import org.quartz.Trigger;
    import org.quartz.TriggerBuilder;
    import org.quartz.impl.StdSchedulerFactory;
    
    public class MainApp {
    
        public static void main(String[] args) throws Exception {
            
            // 创建 SchedulerFactory 并获取 Scheduler 对象实例
            SchedulerFactory schedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = schedulerFactory.getScheduler();
            
            // 通过 JobBuilder 创建 JobDetail 对象实例 
            JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
                                    .withIdentity("helloJob", Scheduler.DEFAULT_GROUP)
                                    .build(); 
            
            // 通过 TriggerBuilder 创建 Trigger 对象实例,设置每 5 秒调度一次任务
            Trigger trigger = TriggerBuilder.newTrigger()
                                    .withIdentity("helloTrigger", Scheduler.DEFAULT_GROUP)
                                    .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0/5 * * * * ?")))
                                    .build();
            
            // 排定任务
            scheduler.scheduleJob(jobDetail, trigger);
            
            // 启动调度器 
            scheduler.start();
            // 
            Thread.sleep(20L * 1000L);
            // 关闭调度器
            scheduler.shutdown(true);
            
        }
        
    }

    Key Interface

    Scheduler - the main API for interacting with the Scheduler.

    Job - an interface to be implemented by components that you want the Scheduler to execute.

    JobDetail - used to define instances of Jobs.

    Trigger - a component that defines the schedule upon which a given Job will be executed.

    JobBuilder - used to define/build JobDetail instances, which define instances of Jobs.

    TriggerBuilder - used to define/build Trigger instances

    SimpleTrigger - it is handy if you need 'one-shot' execution (just single execution of a job at a given moment in time), or if you need to fire a job at a given time, and have it repeat N times, with a delay of T between executions.

    CronTrigger - it is useful if you wish to have triggering based on calendar-like schedules such as "every Friday, at noon" or "at 10:15 on the 10th day of every month."

  • 相关阅读:
    WordPress The Plus Addons for Elementor插件身份验证绕过漏洞复现分析
    ThinkPHP 5日志文件包含trick
    JavaScript对称数字金字塔(机考)
    css绘制三角箭头
    element-ui table 多列数据动态排序(前后台交互)
    Animate.css
    Normalize.css
    CMake笔记
    时间对齐——用 FFT 加速互相关
    g2o 代码学习—— exp map and log map for SE(3), SIM(3)
  • 原文地址:https://www.cnblogs.com/huey/p/5105003.html
Copyright © 2011-2022 走看看