/qtest/src/main/java/org/qtest/QuartzInit.java
Java | 184 lines | 120 code | 22 blank | 42 comment | 2 complexity | 653b7f4edcb965bd4ff95773d52b6a6f MD5 | raw file
Possible License(s): LGPL-3.0
1/** 2 * JWatch - Quartz Monitor: http://code.google.com/p/jwatch/ 3 * Copyright (C) 2011 Roy Russo and the original author or authors. 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation; either 8 * version 3 of the License, or (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General 16 * Public License along with this library; if not, write to the 17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * Boston, MA 02110-1301 USA 19 **/ 20 21package org.qtest; 22 23import org.apache.log4j.Logger; 24import org.qtest.job.HelloJob; 25import org.quartz.JobDetail; 26import org.quartz.Scheduler; 27import org.quartz.SchedulerFactory; 28import org.quartz.Trigger; 29import org.quartz.impl.StdSchedulerFactory; 30 31import javax.servlet.ServletContextEvent; 32import javax.servlet.ServletContextListener; 33 34import static org.quartz.CronScheduleBuilder.cronSchedule; 35import static org.quartz.JobBuilder.newJob; 36import static org.quartz.SimpleScheduleBuilder.simpleSchedule; 37import static org.quartz.TriggerBuilder.newTrigger; 38 39/** 40 * @author <a href="mailto:royrusso@gmail.com">Roy Russo</a> 41 * Date: 6/28/11 1:51 PM 42 */ 43public class QuartzInit implements ServletContextListener { 44 private Scheduler sched = null; 45 private Scheduler sched3 = null; 46 private Scheduler sched2 = null; 47 static Logger log = Logger.getLogger(QuartzInit.class); 48 49 public void contextInitialized(ServletContextEvent event) { 50 try { 51 SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(); 52 53 sched = schedFact.getScheduler(); 54 55 sched.start(); 56 57 // define the job and tie it to our HelloJob class 58 JobDetail job = newJob(HelloJob.class) 59 .withIdentity("myJob", "group1").withDescription("Some rand job").storeDurably(true) 60 .build(); 61 62 // Trigger the job to run now, and then every 40 seconds 63 Trigger trigger = newTrigger() 64 .withIdentity("myTrigger", "group1") 65 .startNow() 66 .withSchedule(simpleSchedule() 67 .withIntervalInSeconds(120) 68 .repeatForever()) 69 .build(); 70 71 // Tell quartz to schedule the job using our trigger 72 sched.scheduleJob(job, trigger); 73 log.info("Job Created: " + job); 74 75 // Job #2 76 // define the job and tie it to our HelloJob class 77 job = newJob(HelloJob.class) 78 .withIdentity("myJob2", "group1").withDescription("Some rand job2").storeDurably(true) 79 .build(); 80 81 // Trigger the job to run now, and then every 40 seconds 82 trigger = newTrigger() 83 .withIdentity("myTrigger2", "group1") 84 .startNow() 85 .withSchedule(simpleSchedule() 86 .withIntervalInMinutes(5) 87 .repeatForever()) 88 .build(); 89 90 // Tell quartz to schedule the job using our trigger 91 sched.scheduleJob(job, trigger); 92 log.info("Job Created: " + job); 93 94 // Group #2, Job #1 95 // define the job and tie it to our HelloJob class 96 job = newJob(HelloJob.class) 97 .withIdentity("myJob1", "group2").withDescription("Some rand job3").storeDurably(true) 98 .build(); 99 100 // Trigger the job to run now, and then every 40 seconds 101 trigger = newTrigger() 102 .withIdentity("myTrigger3", "group2") 103 .startNow() 104 .withSchedule(simpleSchedule() 105 .withIntervalInMinutes(1) 106 .repeatForever()) 107 .build(); 108 109 // Tell quartz to schedule the job using our trigger 110 sched.scheduleJob(job, trigger); 111 log.info("Job Created: " + job); 112 113 // second scheduler 114 // Tell Quartz to look out out for quartz2.properties 115 System.setProperty(StdSchedulerFactory.PROPERTIES_FILE, "quartz2.properties"); 116 schedFact = new org.quartz.impl.StdSchedulerFactory(); 117 sched2 = schedFact.getScheduler(); 118 sched2.start(); 119 // define the job and tie it to our HelloJob class 120 JobDetail jobx = newJob(HelloJob.class) 121 .withIdentity("myJobx", "groupx").withDescription("Some rand jobx").storeDurably(true) 122 .build(); 123 // Trigger the job to run now, and then every 40 seconds 124 Trigger triggerx = newTrigger().forJob(jobx) 125 .withIdentity("myTriggerx", "groupx") 126 .startNow() 127 .withSchedule(simpleSchedule() 128 .withIntervalInSeconds(450) 129 .repeatForever()) 130 .build(); 131 // Tell quartz to schedule the job using our trigger 132 sched2.addJob(jobx, true); 133 sched2.scheduleJob(triggerx); 134 log.info("Job Created: " + jobx); 135 Trigger triggerx2 = newTrigger().forJob(jobx) 136 .withIdentity("myTriggerx2", "groupx2") 137 .startNow() 138 .withSchedule(simpleSchedule() 139 .withIntervalInHours(1) 140 .repeatForever()) 141 .build(); 142 // Tell quartz to schedule the job using our trigger 143 sched2.scheduleJob(triggerx2); 144 145 System.setProperty(StdSchedulerFactory.PROPERTIES_FILE, "quartz3.properties"); 146 schedFact = new org.quartz.impl.StdSchedulerFactory(); 147 sched3 = schedFact.getScheduler(); 148 sched3.start(); 149 int groups = 3; 150 int jobspergroup = 3; 151 for (int g = 0; g < groups; g++) { 152 String group = "group" + g; 153 for (int i = 0; i < jobspergroup; i++) { 154 JobDetail jd1 = newJob(HelloJob.class) 155 .withIdentity("j_" + i, group).withDescription("Some rand jobx").storeDurably(true) 156 .build(); 157 //new JobDetail("j_" + i, group, HelloJob.class); 158 159 Trigger tr1 = newTrigger() 160 .withIdentity("t_" + i, group) 161 .startNow() 162 .withSchedule(cronSchedule(String.valueOf(i % 60) + " * * * * ?")) 163 .build(); 164 165 //new CronTrigger("t_" + i, group, String.valueOf(i % 60) + " * * * * ?"); 166 sched3.scheduleJob(jd1, tr1); 167 } 168 } 169 log.info("Finished job loop"); 170 } catch (Throwable se) { 171 se.printStackTrace(); 172 } 173 } 174 175 public void contextDestroyed(ServletContextEvent event) { 176 try { 177 sched.shutdown(); 178 sched2.shutdown(); 179 sched3.shutdown(); 180 } catch (Throwable t) { 181 t.printStackTrace(); 182 } 183 } 184}