Quartz Scheduler(2.2.1) - hello world

简单示例

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."

时间: 2024-11-10 13:38:15

Quartz Scheduler(2.2.1) - hello world的相关文章

Table of Contents - Quartz Scheduler

Getting Started Hello World Integration with Spring Quartz Scheduler Developer Guide Usage of JobDataMap Usage of Calendars Usage of SimpleTrigger Usage of CronTriggers Working with TriggerListeners and JobListeners Working with SchedulerListeners Wo

Quartz Scheduler(2.2.1) - Working with JobStores

About Job Stores JobStores are responsible for keeping track of all the work data you give to the scheduler: jobs, triggers, calendars, and so forth. Selecting the appropriate JobStore for your Quartz scheduler instance is an important step. The choi

Spring Boot集成Spring Scheduler和Quartz Scheduler

本文介绍了Spring Boot集成Spring Scheduler和Quartz Scheduler的基础知识,利用ShedLock解决Spring Scheduler多实例运行冲突,介绍了Quartz ScheduleBuilder.Calendar,介绍了动态创建Quartz Job的方法. GitHub源码 Spring Scheduler Spring Framework提供了简单.易用的Job调度框架Spring Scheduler. 示例 在Spring Boot中,只需两步即可启

Quartz Scheduler(2.2.1) - Usage of SimpleTrigger

SimpleTrigger should meet your scheduling needs if you need to have a job execute exactly once at a specific moment in time, or at a specific moment in time followed by repeats at a specific interval. For example, if you want the trigger to fire at e

Quartz Scheduler(2.2.1) - Usage of Calendars

Quartz Calendar objects (not java.util.Calendar objects) can be associated with triggers at the time the trigger is defined and stored in the scheduler. Calendars are useful for excluding blocks of time from the trigger's firing schedule. For instanc

Quartz Scheduler(2.2.1) - Usage of CronTriggers

Cron is a UNIX tool that has been around for a long time, so its scheduling capabilities are powerful and proven. The CronTrigger class is based on the scheduling capabilities of cron. CronTrigger uses “cron expressions”, which are able to create fir

Quartz使用(2) - Quartz核心接口Scheduler、Job

quartz的核心接口如下: 接口 含义 Scheduler scheduler的主要API接口 Job 任务实现接口,期望调度器能够执行 JobDetail 用于定义Job实例 Trigger 调度器基于特定时间来执行指定任务的组件 JobBuilder 用于定义.创建JobDetail实例 TriggerBuilder 用于定义.创建Trigger实例 1. Scheduler 一个调度器的生命周期为通过SchedulerFactory创建,直到执行其shutdown()方法.当Schedu

Spring 3 + Quartz 1.8.6 Scheduler Example--reference

In this tutorial, we will show you how to integrate Spring with Quartz scheduler framework. Spring comes with many handy classes to support Quartz, and decouple your class to Quartz APIs. Tools Used : Spring 3.1.2.RELEASE Quartz 1.8.6 Eclipse 4.2 Mav

Quartz Enterprise Job Scheduler 1.x Tutorial---转载

Lesson 10: Configuration, Resource Usage and SchedulerFactory http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/TutorialLesson10 The architecture of Quartz is modular, and therefore to get it running several components need to be "sn