码迷,mamicode.com
首页 > 其他好文 > 详细

Quartz Scheduler(2.2.1) - hello world

时间:2016-01-06 13:40:13      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

简单示例

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

 

Quartz Scheduler(2.2.1) - hello world

标签:

原文地址:http://www.cnblogs.com/huey/p/5105003.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!