quartz scheduler 2.16 之集群

quartz scheduler 2.16 之集群

现在最新版本的quartz是2.16,这里简单介绍下这个版本下的集群。尝试过与spring的整合,但好像spring对这个版本还没办法整合,总有错。

先下载http://quartz-scheduler.org/downloads/catalog 然后运行quartz-2.1.6\docs\dbTables\tables_mysql_innodb.sql

复制代码
LOGGER.debug("#######################init quartz ....###################");
        Properties properties = new Properties();

        properties.put("org.quartz.scheduler.instanceName","MyClusteredScheduler");
        properties.put("org.quartz.scheduler.instanceId","AUTO");
        properties.put("org.quartz.scheduler.skipUpdateCheck","true");
        properties.put("org.quartz.threadPool.class","org.quartz.simpl.SimpleThreadPool");
        properties.put("org.quartz.threadPool.threadCount","12");
        properties.put("org.quartz.threadPool.threadPriority","5");
        properties.put("org.quartz.jobStore.misfireThreshold","10000");
        properties.put("org.quartz.jobStore.class","org.quartz.impl.jdbcjobstore.JobStoreTX");
        properties.put("org.quartz.jobStore.driverDelegateClass","org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
        properties.put("org.quartz.jobStore.useProperties","true");
        properties.put("org.quartz.jobStore.dataSource","myDS");
        properties.put("org.quartz.jobStore.tablePrefix","QRTZ_");
        properties.put("org.quartz.jobStore.isClustered","true");
        properties.put("org.quartz.dataSource.myDS.driver",DBDRIVER);
        properties.put("org.quartz.dataSource.myDS.URL",DBURL);
        properties.put("org.quartz.dataSource.myDS.user",DBUSER);
        properties.put("org.quartz.dataSource.myDS.password",DBPWD);
        properties.put("org.quartz.dataSource.myDS.maxConnections","5");
        // we must get a reference to a scheduler
        SchedulerFactory sf = new StdSchedulerFactory(properties);
        sched = sf.getScheduler();
        LOGGER.info("------- Initializing ----------------------");


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

        LOGGER.info("------- Scheduling Job  -------------------");
        // define the jobs and tie them to our HelloJob class
        JobDetail job1_1 = newJob(HelloJob.class).withIdentity("job1", "group1").build();
        JobDetail job2_1 = newJob(HelloJob.class).withIdentity("job2", "group1").build();
        JobDetail job1_2 = newJob(HelloJob.class).withIdentity("job1", "group2").build();
        JobDetail job2_2 = newJob(HelloJob.class).withIdentity("job2", "group2").build();

        trigger1_1 = newTrigger()
                .withIdentity("job1", "group1")
                .startNow()
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(INTERVAL_IN_SECONDS)
                                .repeatForever())
                .build();
        trigger2_1 = newTrigger()
                .withIdentity("job2", "group1")
                .startNow()
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(INTERVAL_IN_SECONDS)
                                .repeatForever())
                .build();
        trigger1_2 = newTrigger()
                .withIdentity("job1", "group2")
                .startNow()
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(INTERVAL_IN_SECONDS)
                                .repeatForever())
                .build();
        trigger2_2 = newTrigger()
                .withIdentity("job2", "group2")
                .startNow()
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(INTERVAL_IN_SECONDS)
                                .repeatForever())
                .build();

        Trigger[] triggers = new Trigger[] { trigger1_1, trigger1_2, trigger2_1, trigger2_2 };
        JobDetail[] jobDetails = new JobDetail[] { job1_1, job1_2, job2_1, job2_2 };
        for (int i = 0; i < triggers.length; i++) {
            JobDetail job = jobDetails[i];
            Trigger trigger = triggers[i];
            if (sched.checkExists(job.getKey())) {
                // the job already exists in jdbcjobstore; let's reschedule it
                sched.rescheduleJob(trigger.getKey(), trigger);
                LOGGER.info(trigger.getKey() + "@@@@@@@@@@@@@@@@@RESCHEDULEJOB");
            } else {
                LOGGER.info(trigger.getKey() + "#####################SCHEDULEJOB");
                sched.scheduleJob(job, trigger);
            }
        }

        // Start up the scheduler (nothing can actually run until the
        // scheduler has been started)
        sched.start();

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

        // wait long enough so that the scheduler as an opportunity to
        // run the job!
        try {
            Thread.sleep(DURATION_OF_FIRST_SCHEDULING * 1000L);
        } catch (Exception e) {
        }
    }
复制代码

先通过 properties构造 SchedulerFactory ,把job持久到数据库

SchedulerFactory sf = new StdSchedulerFactory(properties);

然后添加job,trigger到 SchedulerFactory

Properties 相关参数介绍如下:
复制代码
#============================================================================




# Configure Main Scheduler Properties #============================================================================ #instanceName属性可为任何值,用在 JDBC JobStore 中来唯一标识实例,但是所有集群节点中必须相同 org.quartz.scheduler.instanceName=MyClusteredScheduler #属性为 AUTO即可,基于主机名和时间戳来产生实例 ID org.quartz.scheduler.instanceId=AUTO #============================================================================ # Configure ThreadPool #============================================================================ org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount=25 org.quartz.threadPool.threadPriority=5 #============================================================================ # Configure JobStore #============================================================================ org.quartz.jobStore.misfireThreshold=60000 #属性为 JobStoreTX将任务持久化到数据中。因为集群中节点依赖于数据库来传播 org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.useProperties=true org.quartz.jobStore.dataSource=myDS org.quartz.jobStore.tablePrefix=QRTZ_ #属性为 true,你就告诉了 Scheduler 实例要它参与到一个集群当中。这一属性会贯穿于调度框架的始终,用于修改集群环境中操作的默认行为 org.quartz.jobStore.isClustered=true #属性定义了Scheduler 实例检入到数据库中的频率(单位:毫秒) org.quartz.jobStore.clusterCheckinInterval=20000 #============================================================================ # Configure Datasources #============================================================================ org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/jtxw?useUnicode=true &characterEncoding =utf8 org.quartz.dataSource.myDS.user=root org.quartz.dataSource.myDS.password=root org.quartz.dataSource.myDS.maxConnections=5 org.quartz.dataSource.myDS.validationQuery=select 1
复制代码

 

官方配置参考地址:http://quartz-scheduler.org/documentation/quartz-2.1.x/configuration/
quick start : http://quartz-scheduler.org/documentation/quartz-2.1.x/quick-start
tutorials     : http://quartz-scheduler.org/documentation/quartz-2.1.x/tutorials/
examples      : http://quartz-scheduler.org/documentation/quartz-2.1.x/examples/
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值