Quartz入门01

首先给一个简单的例子。

1、首先是任务类
任务类是Quartz每次触发时,触发的任务对象,我们要定期完成的业务逻辑,就是在这个对象中完成的。

package com.neohope.quartz.test;

import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * Created by Hansen
 */
public class Job001 implements Job {
    private static Logger logger = LoggerFactory.getLogger(Job001.class);

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("Job001 Executing : " + new Date());

        //获取Job的参数
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        //String jobDesc = jobDetail.getDescription();

        JobDataMap dataMap = jobDetail.getJobDataMap();
        String message = dataMap.getString("JOB_MSG");
        logger.info("Job001 Message is : " + message);

    }
}

2、然后是测试类
测试类初始化了Scheduler,并且设定任务每2秒执行一次,执行5次

package com.neohope.quartz.test;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Hansen
 */
public class Test001 {

    public void run() throws Exception {
        Logger logger = LoggerFactory.getLogger(Test001.class);
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler scheduler = sf.getScheduler();

        JobDetail job = JobBuilder.newJob(Job001.class).withIdentity("job001", "jgroup001").build();
        job.getJobDataMap().put("JOB_MSG","Hi Job001");

        ScheduleBuilder schedulerBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).withRepeatCount(5);
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger001", "tgroup001").withSchedule(schedulerBuilder).build();
        scheduler.scheduleJob(job, trigger);

        scheduler.start();
        logger.info("scheduler started");
        try {
            System.in.read();
        } catch (Exception e) {
        }

        scheduler.shutdown(true);
        logger.info("scheduler ended");
    }

    public static void main(String[] args) throws Exception {
        System.setProperty("org.quartz.properties","quartzS.properties");

        Test001 test = new Test001();
        test.run();
    }
}

3、配置文件
为了使用方便,我将配置文件名称修改了一下quartzS.properties

#============================================================================
# Configure Main Scheduler Properties  
#============================================================================
org.quartz.scheduler.instanceName: NeoScheduler
org.quartz.scheduler.instanceId: AUTO
org.quartz.scheduler.skipUpdateCheck: true

#============================================================================
# Configure ThreadPool  
#============================================================================
org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount: 1
org.quartz.threadPool.threadPriority: 5

#============================================================================
# Configure JobStore  
#============================================================================
org.quartz.jobStore.misfireThreshold: 60000
org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore

#org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX
#org.quartz.jobStore.driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
#org.quartz.jobStore.useProperties: false
#org.quartz.jobStore.dataSource: myDS
#org.quartz.jobStore.tablePrefix: QRTZ_
#org.quartz.jobStore.isClustered: false

#============================================================================
# Configure Datasources  
#============================================================================
#org.quartz.dataSource.myDS.driver: org.postgresql.Driver
#org.quartz.dataSource.myDS.URL: jdbc:postgresql://localhost/dev
#org.quartz.dataSource.myDS.user: jhouse
#org.quartz.dataSource.myDS.password: 
#org.quartz.dataSource.myDS.maxConnections: 5

4、大家运行一下看看吧

Comments are closed.