quartz-scheduler的集群化配置

由于集群只能工作在JDBC-Jobstore(JobStoreTX或者JobStoreCMT)方式下。需要先准备好quartz的数据库环境。

1.在 quartz 的官网上(http://quartz-scheduler.org/ )下载quartz框架的最新版本(目前是1.8.6),下载quartz-1.8.6.tar.gz文件。

2.解压压缩文件quartz-1.8.6.tar.gz,在quartz-1.8.6\docs\dbTables\ 找到文件tables_oracle.sql(以oracle为例),在数据库中执行一遍,就可以看到quartz 框架

集群管理所需的表。

3.配置quartz.properties的quartz属性文件。

#============================================================================

# Configure Main Scheduler Properties

#============================================================================

org.quartz.scheduler.instanceName = Scheduler

org.quartz.scheduler.instanceId = AUTO

#============================================================================

# Configure ThreadPool

#============================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool

org.quartz.threadPool.threadCount = 10

org.quartz.threadPool.threadPriority = 5

#==============================================================

#Configure JobStore 集群配置

#==============================================================

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX

org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.OracleDelegate

org.quartz.jobStore.tablePrefix = QRTZ_

org.quartz.jobStore.dataSource = myDS

 

org.quartz.jobStore.isClustered = true

org.quartz.jobStore.clusterCheckinInterval = 20000

#==============================================================

#Non-Managed Configure Datasource 集群数据库配置

#==============================================================

org.quartz.dataSource.myDS.driver = oracle.jdbc.driver.OracleDriver

org.quartz.dataSource.myDS.URL = jdbc:oracle:thin:@127.0.0.1/orcl

org.quartz.dataSource.myDS.user = username

org.quartz.dataSource.myDS.password = password

org.quartz.dataSource.myDS.maxConnections = 10

 

 

修改其中数据库相关配置为你自己用的。

org.quartz.jobStore.isClustered = true启用集群的配置项。

 

4. 定义一个作业类(Job)。

import java.util.Date;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.quartz.Job;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

public class SimpleJob implements Job {

    private static Log _log = LogFactory.getLog(SimpleJob.class);

    public SimpleJob() {

    }

   public void execute(JobExecutionContext context)

        throws JobExecutionException {

        // This job simply prints out its job name and the

        // date and time that it is running

        String jobName = context.getJobDetail().getFullName();

        _log.info("SimpleJob says: " + jobName + " executing at " + new Date());

    }

}

 

 

5.定义作业的执行类。

import java.util.Date;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.quartz.CronTrigger;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import org.quartz.SchedulerFactory;

import org.quartz.SchedulerMetaData;

import org.quartz.impl.StdSchedulerFactory;

public class JdbcJobStoreExample {

         public void runCluster()throws Exception{

                   Log log = LogFactory.getLog(JdbcJobStoreExample.class);

                   log.info("------- Initializing -------------------");

                   // First we must get a reference to a scheduler

                   String propName = "conf/quartz_cluster.properties";

                   SchedulerFactory sf = new StdSchedulerFactory(propName);

                   Scheduler sched = sf.getScheduler();

                   log.info("------- Initialization Complete --------");

                   log.info("------- Scheduling Jobs ----------------");

                   log.info("------- Starting Scheduler ----------------");

                   // All of the jobs have been added to the scheduler, but none of the

                   // jobs

                  // will run until the scheduler has been started

                   sched.start();

                   log.info("------- Started Scheduler -----------------");

                   log.info("------- Waiting five minutes... ------------");

                   try {

                            // wait five minutes to show jobs

                            Thread.sleep(300L * 1000L);

                            // executing...

                   } catch (Exception e) {

                   }

                   log.info("------- Shutting Down ---------------------");

                   sched.shutdown(true);

                   log.info("------- Shutdown Complete -----------------");

                   SchedulerMetaData metaData = sched.getMetaData();

                   log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");

         }

         public void run() throws Exception {

                   Log log = LogFactory.getLog(JdbcJobStoreExample.class);

                   log.info("------- Initializing -------------------");

                   // First we must get a reference to a scheduler

                   String propName = "conf/quartz_cluster.properties";

                   SchedulerFactory sf = new StdSchedulerFactory(propName);

                   Scheduler sched = sf.getScheduler();

                   log.info("------- Initialization Complete --------");

                   log.info("------- Scheduling Jobs ----------------");

                   // jobs can be scheduled before sched.start() has been called

                   // job 1 will run every 20 seconds

                   JobDetail job = new JobDetail("job1", "group1", SimpleJob.class);

                   CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1",

                                     "group1", "0/20 * * * * ?");

                   sched.addJob(job, true);

                   Date ft = sched.scheduleJob(trigger);

                   log.info(job.getFullName() + " has been scheduled to run at: " + ft

                                     + " and repeat based on expression: "

                                     + trigger.getCronExpression());

                   // job 2 will run every other minute (at 15 seconds past the minute)

                   job = new JobDetail("job2", "group1", SimpleJob.class);

                   trigger = new CronTrigger("trigger2", "group1", "job2", "group1",

                                     "15 0/2 * * * ?");

                   sched.addJob(job, true);

                   ft = sched.scheduleJob(trigger);

                   log.info(job.getFullName() + " has been scheduled to run at: " + ft

                                     + " and repeat based on expression: "

                                     + trigger.getCronExpression());

                   // job 3 will run every other minute but only between 8am and 5pm

                   job = new JobDetail("job3", "group1", SimpleJob.class);

                   trigger = new CronTrigger("trigger3", "group1", "job3", "group1",

                                     "0 0/2 8-17 * * ?");

                   sched.addJob(job, true);

                   ft = sched.scheduleJob(trigger);

                   log.info(job.getFullName() + " has been scheduled to run at: " + ft

                                     + " and repeat based on expression: "

                                     + trigger.getCronExpression());

                   // job 4 will run every three minutes but only between 5pm and 11pm

                   job = new JobDetail("job4", "group1", SimpleJob.class);

                   trigger = new CronTrigger("trigger4", "group1", "job4", "group1",

                                     "0 0/3 17-23 * * ?");

                   sched.addJob(job, true);

                   ft = sched.scheduleJob(trigger);

                   log.info(job.getFullName() + " has been scheduled to run at: " + ft

                                     + " and repeat based on expression: "

                                     + trigger.getCronExpression());

                   // job 5 will run at 10am on the 1st and 15th days of the month

                   job = new JobDetail("job5", "group1", SimpleJob.class);

                   trigger = new CronTrigger("trigger5", "group1", "job5", "group1",

                                     "0 0 10am 1,15 * ?");

                   sched.addJob(job, true);

                   ft = sched.scheduleJob(trigger);

                   log.info(job.getFullName() + " has been scheduled to run at: " + ft

                                     + " and repeat based on expression: "

                                     + trigger.getCronExpression());

                   // job 6 will run every 30 seconds but only on Weekdays (Monday through

                   // Friday)

                   job = new JobDetail("job6", "group1", SimpleJob.class);

                   trigger = new CronTrigger("trigger6", "group1", "job6", "group1",

                                     "0,30 * * ? * MON-FRI");

                   sched.addJob(job, true);

                   ft = sched.scheduleJob(trigger);

                   log.info(job.getFullName() + " has been scheduled to run at: " + ft

                                     + " and repeat based on expression: "

                                     + trigger.getCronExpression());

                   // job 7 will run every 30 seconds but only on Weekends (Saturday and

                   // Sunday)

                   job = new JobDetail("job7", "group1", SimpleJob.class);

                   trigger = new CronTrigger("trigger7", "group1", "job7", "group1",

                                     "0,30 * * ? * SAT,SUN");

                   sched.addJob(job, true);

                   ft = sched.scheduleJob(trigger);

                   log.info(job.getFullName() + " has been scheduled to run at: " + ft

                                     + " and repeat based on expression: "

                                     + trigger.getCronExpression());

 

                   log.info("------- Starting Scheduler ----------------");

 

                   // All of the jobs have been added to the scheduler, but none of the

                   // jobs

                   // will run until the scheduler has been started

                   sched.start();

 

                   log.info("------- Started Scheduler -----------------");

 

                   log.info("------- Waiting five minutes... ------------");

                   try {

                            // wait five minutes to show jobs

                            Thread.sleep(300L * 1000L);

                            // executing...

                   } catch (Exception e) {

                   }

 

                   log.info("------- Shutting Down ---------------------");

 

                   sched.shutdown(true);

 

                   log.info("------- Shutdown Complete -----------------");

 

                   SchedulerMetaData metaData = sched.getMetaData();

                   log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");

         }

 

         /**

          * @param args

          */

         public static void main(String[] args) {

                   JdbcJobStoreExample test = new JdbcJobStoreExample();

                   try {

//                          test.run();

                            test. runCluster ();

                   } catch (Exception e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                   }

                   System.out.println("ok....");

         }

}

 

在main()方法调用 test.run();方法会把作业调度信息持久化到数据库中。

 test. runCluster ()是集群启动的方法,在启动集群方法前,首先要先执行一遍JdbcJobStoreExample类的run()方法,

因为我在run()方法中添加作业的调度信息,运行后quartz会把调度信息持久

化到数据库,也可以直接在数据库中配置作业的调度信息(由于本人比较懒,就这样偷懒了一下)。

 

6.程序中用到的jar文件如下:

commons-dbcp-1.3.jar
commons-logging-1.1.jar
commons-pool-1.5.4.jar
jta-1.1.jar
junit-4.5.jar
log4j-1.2.14.jar
oracle-driver-14.jar
quartz-all-1.8.5.jar
slf4j-api-1.6.0.jar
slf4j-log4j12-1.6.0.jar

7.在不同的位置运行多个程序(即调用该runCluster ()方法),集群就启动了。

posted @ 2012-02-28 14:40  丹江湖畔养蜂子赵大爹  阅读(1987)  评论(0编辑  收藏  举报