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 相关参数介绍如下:
#============================================================================