`

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 ()方法),集群就启动了。

来源:
http://www.cnblogs.com/honeybee/articles/2371540.html
分享到:
评论

相关推荐

    quartz-scheduler分享.docx

    Quartz的核心特性包括可持久化的作业存储、集群支持以及丰富的API,使得它成为企业级应用中进行定时任务调度的理想选择。 在使用Quartz-Scheduler时,首先需要在项目的`pom.xml`文件中添加对应的依赖。以下是一个...

    Getting_Started_with_Quartz_Scheduler.pdf Version 2.2.1

    - **集群支持**:Quartz Scheduler可以在集群环境中工作,确保即使部分节点失败,作业也能得到正确的调度和执行。 - **高性能**:通过高效的内部架构设计,Quartz Scheduler能够在高并发环境下保持稳定的性能表现。 ...

    quartz-2.4.0-SNAPSHOT-distribution.tar.gz

    - **集群支持**:Quartz可以在多台服务器上进行分布式调度,确保任务在集群中的任何节点失败时仍能执行。 - **持久化**:任务和触发器的状态可以持久化到数据库,即使服务器重启也不会丢失已安排的任务。 - **监控与...

    quartz-2.2.3版本的quartz初始化sql语句

    - 初始化SQL语句执行后,还需确保Quartz的配置文件(如`quartz.properties`)中的表名与创建的数据库表一致。 总之,Quartz 2.2.3的初始化SQL语句是搭建Quartz任务调度系统的重要环节,它们构建了存储任务调度数据...

    quartz-2.3.0-distribution.zip

    - Job持久化:Quartz支持将Job和Trigger信息存储在数据库中,确保系统重启后任务能够恢复执行。 - Job状态管理:可以通过`JobExecutionContext`获取和设置Job的运行状态。 - 群组管理:Job和Trigger可以分组管理...

    quartzall1.6.0jar_quartz-all-1.6.0.jar_

    6. **集群支持**:Quartz可以配置在多台服务器上运行,形成一个集群,这样可以提高系统的可用性和容错性。当一台服务器出现问题时,其他服务器可以接管任务。 7. **API友好**:Quartz提供了简单易用的API,使得...

    quartz-all-1.6.0.jar

    3. **集群支持**:Quartz 可以在多台服务器组成的集群中运行,当一台服务器故障时,任务可以被其他服务器接管,保证服务的高可用性。 4. **插件系统**:Quartz 具有强大的插件扩展能力,可以方便地添加新的功能或...

    quartz-1.6.6 教程,代码

    - **Spring 集成**:在 Spring 应用中,可以通过 XML 配置或 Java Config 方式来配置和初始化 Scheduler。 3. **数据库表结构** - Quartz 使用数据库存储 Jobs 和 Triggers,因此需要创建相应的数据库表。在提供...

    quartz-2.2.3.jar

    2. **文档**:可能包括用户指南、API文档等,帮助开发者了解如何配置和使用Quartz。 3. **示例代码**:提供现成的代码示例,演示如何创建和调度Job,以及如何与数据库交互存储Job和Trigger信息。 4. **依赖库**:...

    quartz-2.3.0-distribution.tar.gz

    1. **配置 Scheduler**:创建 Scheduler 实例,配置相关的属性,如使用哪种 JobStore(如 JDBCJobStore)来存储 Job 和 Trigger。 2. **定义 Job**:编写 Job 类,实现 `execute()` 方法。 3. **创建 JobDetail**...

    quartz-2.2.1.jar

    总结,Quartz作为强大的作业调度工具,能够有效地帮助开发者自动化执行周期性任务,提高应用的效率和智能化程度。通过深入学习和实践,我们可以充分利用Quartz的特性,构建高效、可靠的后台任务处理系统。

    quartz-2.1.7.gz

    1. **初始化Scheduler**:通过`StdSchedulerFactory`获取Scheduler实例,配置相关的属性,如JobStore类型、线程池大小等。 2. **定义Job**:创建Job类,实现`Job`接口,并在`execute()`方法中编写任务逻辑。 3. **...

    quartz-2.2.1相关jar

    使用Quartz时,开发者首先需要配置Scheduler,指定JobStore类型(如RAMJobStore或JDBCJobStore),然后定义作业类(实现`org.quartz.Job`接口)和触发器(通过`org.quartz.Trigger`的子类)。最后,通过Scheduler...

    quartz2.2.3实战

    - **集群支持**:多个 Quartz 节点可以组成集群,共享任务调度,提供高可用性。 2. **Quartz 配置** - **Scheduler**:作为 Quartz 的核心,Scheduler 负责管理和执行所有作业和触发器。可以通过 `...

    quartz-1.8.6.jar

    Quartz是一个开源的作业调度框架,它为Java应用程序提供了强大的定时任务管理能力。...在实际开发中,根据项目需求选择合适的Trigger类型,结合Job和Scheduler的配置,能够有效地提升系统的自动化程度和效率。

    quartz-1.8.3.jar

    2. **集群支持**:Quartz 可以在多台服务器上分布式部署,确保任务的高可用性和容错性。即使某个节点故障,任务仍能在其他节点上执行。 3. **持久化作业**:Quartz 可以将作业和触发器持久化到数据库,保证任务不会...

    quartz-2.2 以及关联jar包

    1. **集群支持**:Quartz-2.2加强了集群功能,允许多个Scheduler实例共享状态,即使在一个节点故障时,其他节点也能接管任务,确保高可用性。 2. **JDBC Job Store**:提供了一种将Job和Trigger存储在关系数据库中...

    quartz-2.2.3-distribution.tar.gz

    4. **集群支持**:在多服务器环境中,Quartz可以实现集群,确保任务的高可用性和负载均衡。 5. **作业存储与分组**:作业可以被组织在作业组中,便于管理和查找。同时,多个实例的同一作业也可以存在,通过不同的...

    quartz scheduler 入门教程

    Quartz Scheduler 提供了集群特性,提供了 fail-over 和负载均衡。Quartz Scheduler 自有集群特性,在数据库持久保存的基础上。Terracotta 对 Quartz 的扩展,实现了一个集群能力,不需要终端数据库的支持。 监听器...

    quartz-1.7.3架包

    此外,Quartz支持集群,这意味着多个Scheduler实例可以协同工作,提供高可用性和负载均衡。在大型分布式系统中,这一点尤为重要。 通过深入学习和实践这个"quartz-1.7.3"压缩包的内容,我们可以熟练掌握Quartz的...

Global site tag (gtag.js) - Google Analytics