Quartz快速上手

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz快速上手相关的知识,希望对你有一定的参考价值。

快速上手你需要干啥:

        下载Quartz

        安装Quartz

        根据你的需要来配置Quartz

        开始一个示例应用

 

下载和安装

 

The Quartz JAR Files

        The main Quartz library is named quartz-xxx.jar (where xxx is a version number). In order to use any of Quartz’s features, this jar must be located on your application’s classpath.

 

        Once you’ve downloaded Quartz, unzip it somewhere, grab the quartz-xxx.jar and put it where you want it.

 

        However, if you want to make Quartz available to many applications then simply make sure it’s on the classpath of your appserver.

 

The Properties File

        Quartz uses a properties file called (kudos on the originality) quartz.properties.

        I keep all of my configuration files (including quartz.properties) in a project under the root of my application.

 

配置

 

        This is the big bit! Quartz is a very configurable application.

        The best way to configure Quartz is to edit a quartz.properties file, and place it in your application’s classpath (see Installation section above).

 

        一个基本的配置文件长这样:

 

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

 

The scheduler created by this configuration has the following characteristics:

 

        org.quartz.scheduler.instanceName - This scheduler’s name will be “MyScheduler”.

 

        org.quartz.threadPool.threadCount - There are 3 threads in the thread pool, which means that a maximum of 3 jobs can be run simultaneously.

 

        org.quartz.jobStore.class - All of Quartz’s data, such as details of jobs and triggers, is held in memory (rather than in a database). Even if you have a database and want to use it with Quartz, I suggest you get Quartz working with the RamJobStore before you open up a whole new dimension by working with a database.

 

开始一个简单的应用

 

        The following code obtains an instance of the scheduler, starts it, then shuts it down:

 

QuartzTest.java

 

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;

public class QuartzTest {

    public static void main(String[] args) {

        try {
            // Grab the Scheduler instance from the Factory
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

            // and start it off
            scheduler.start();

            scheduler.shutdown();

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
    }
}

 

 

        Once you obtain a scheduler using StdSchedulerFactory.getDefaultScheduler(), your application will not terminate until you call scheduler.shutdown(), because there will be active threads.

 

 

        To do something interesting, you need code between the start() and shutdown() calls.

 

// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
    .withIdentity("job1", "group1")
    .build();

// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .startNow()
    .withSchedule(simpleSchedule()
    .withIntervalInSeconds(40)
    .repeatForever())            
    .build();

// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);

 

        (you will also need to allow some time for the job to be triggered and executed before calling shutdown() - for a simple example such as this, you might just want to add a Thread.sleep(60000) call).

 

        绑定到Hello.class上是要干啥啊?

        触发器要执行的动作是啥啊?

 

        现在你可以滚一边自己去玩了.......

以上是关于Quartz快速上手的主要内容,如果未能解决你的问题,请参考以下文章

Quartz.NET快速上手第一课(官网文档翻译)

快速上手React:

SpringBoot-ElasticJob封装快速上手使用(分布式定时器)

新浪SAE快速上手教程

《Python编程快速上手》6.7实践项目代码

Quartz深入浅出