/qtest/src/main/java/org/qtest/QuartzInit.java

http://jwatch.googlecode.com/ · Java · 184 lines · 120 code · 22 blank · 42 comment · 2 complexity · 653b7f4edcb965bd4ff95773d52b6a6f MD5 · raw file

  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. package org.qtest;
  21. import org.apache.log4j.Logger;
  22. import org.qtest.job.HelloJob;
  23. import org.quartz.JobDetail;
  24. import org.quartz.Scheduler;
  25. import org.quartz.SchedulerFactory;
  26. import org.quartz.Trigger;
  27. import org.quartz.impl.StdSchedulerFactory;
  28. import javax.servlet.ServletContextEvent;
  29. import javax.servlet.ServletContextListener;
  30. import static org.quartz.CronScheduleBuilder.cronSchedule;
  31. import static org.quartz.JobBuilder.newJob;
  32. import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
  33. import static org.quartz.TriggerBuilder.newTrigger;
  34. /**
  35. * @author <a href="mailto:royrusso@gmail.com">Roy Russo</a>
  36. * Date: 6/28/11 1:51 PM
  37. */
  38. public class QuartzInit implements ServletContextListener {
  39. private Scheduler sched = null;
  40. private Scheduler sched3 = null;
  41. private Scheduler sched2 = null;
  42. static Logger log = Logger.getLogger(QuartzInit.class);
  43. public void contextInitialized(ServletContextEvent event) {
  44. try {
  45. SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
  46. sched = schedFact.getScheduler();
  47. sched.start();
  48. // define the job and tie it to our HelloJob class
  49. JobDetail job = newJob(HelloJob.class)
  50. .withIdentity("myJob", "group1").withDescription("Some rand job").storeDurably(true)
  51. .build();
  52. // Trigger the job to run now, and then every 40 seconds
  53. Trigger trigger = newTrigger()
  54. .withIdentity("myTrigger", "group1")
  55. .startNow()
  56. .withSchedule(simpleSchedule()
  57. .withIntervalInSeconds(120)
  58. .repeatForever())
  59. .build();
  60. // Tell quartz to schedule the job using our trigger
  61. sched.scheduleJob(job, trigger);
  62. log.info("Job Created: " + job);
  63. // Job #2
  64. // define the job and tie it to our HelloJob class
  65. job = newJob(HelloJob.class)
  66. .withIdentity("myJob2", "group1").withDescription("Some rand job2").storeDurably(true)
  67. .build();
  68. // Trigger the job to run now, and then every 40 seconds
  69. trigger = newTrigger()
  70. .withIdentity("myTrigger2", "group1")
  71. .startNow()
  72. .withSchedule(simpleSchedule()
  73. .withIntervalInMinutes(5)
  74. .repeatForever())
  75. .build();
  76. // Tell quartz to schedule the job using our trigger
  77. sched.scheduleJob(job, trigger);
  78. log.info("Job Created: " + job);
  79. // Group #2, Job #1
  80. // define the job and tie it to our HelloJob class
  81. job = newJob(HelloJob.class)
  82. .withIdentity("myJob1", "group2").withDescription("Some rand job3").storeDurably(true)
  83. .build();
  84. // Trigger the job to run now, and then every 40 seconds
  85. trigger = newTrigger()
  86. .withIdentity("myTrigger3", "group2")
  87. .startNow()
  88. .withSchedule(simpleSchedule()
  89. .withIntervalInMinutes(1)
  90. .repeatForever())
  91. .build();
  92. // Tell quartz to schedule the job using our trigger
  93. sched.scheduleJob(job, trigger);
  94. log.info("Job Created: " + job);
  95. // second scheduler
  96. // Tell Quartz to look out out for quartz2.properties
  97. System.setProperty(StdSchedulerFactory.PROPERTIES_FILE, "quartz2.properties");
  98. schedFact = new org.quartz.impl.StdSchedulerFactory();
  99. sched2 = schedFact.getScheduler();
  100. sched2.start();
  101. // define the job and tie it to our HelloJob class
  102. JobDetail jobx = newJob(HelloJob.class)
  103. .withIdentity("myJobx", "groupx").withDescription("Some rand jobx").storeDurably(true)
  104. .build();
  105. // Trigger the job to run now, and then every 40 seconds
  106. Trigger triggerx = newTrigger().forJob(jobx)
  107. .withIdentity("myTriggerx", "groupx")
  108. .startNow()
  109. .withSchedule(simpleSchedule()
  110. .withIntervalInSeconds(450)
  111. .repeatForever())
  112. .build();
  113. // Tell quartz to schedule the job using our trigger
  114. sched2.addJob(jobx, true);
  115. sched2.scheduleJob(triggerx);
  116. log.info("Job Created: " + jobx);
  117. Trigger triggerx2 = newTrigger().forJob(jobx)
  118. .withIdentity("myTriggerx2", "groupx2")
  119. .startNow()
  120. .withSchedule(simpleSchedule()
  121. .withIntervalInHours(1)
  122. .repeatForever())
  123. .build();
  124. // Tell quartz to schedule the job using our trigger
  125. sched2.scheduleJob(triggerx2);
  126. System.setProperty(StdSchedulerFactory.PROPERTIES_FILE, "quartz3.properties");
  127. schedFact = new org.quartz.impl.StdSchedulerFactory();
  128. sched3 = schedFact.getScheduler();
  129. sched3.start();
  130. int groups = 3;
  131. int jobspergroup = 3;
  132. for (int g = 0; g < groups; g++) {
  133. String group = "group" + g;
  134. for (int i = 0; i < jobspergroup; i++) {
  135. JobDetail jd1 = newJob(HelloJob.class)
  136. .withIdentity("j_" + i, group).withDescription("Some rand jobx").storeDurably(true)
  137. .build();
  138. //new JobDetail("j_" + i, group, HelloJob.class);
  139. Trigger tr1 = newTrigger()
  140. .withIdentity("t_" + i, group)
  141. .startNow()
  142. .withSchedule(cronSchedule(String.valueOf(i % 60) + " * * * * ?"))
  143. .build();
  144. //new CronTrigger("t_" + i, group, String.valueOf(i % 60) + " * * * * ?");
  145. sched3.scheduleJob(jd1, tr1);
  146. }
  147. }
  148. log.info("Finished job loop");
  149. } catch (Throwable se) {
  150. se.printStackTrace();
  151. }
  152. }
  153. public void contextDestroyed(ServletContextEvent event) {
  154. try {
  155. sched.shutdown();
  156. sched2.shutdown();
  157. sched3.shutdown();
  158. } catch (Throwable t) {
  159. t.printStackTrace();
  160. }
  161. }
  162. }