1 17 18 21 package org.quartz.impl; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.quartz.Scheduler; 26 import org.quartz.SchedulerException; 27 import org.quartz.SchedulerFactory; 28 import org.quartz.core.JobRunShellFactory; 29 import org.quartz.core.QuartzScheduler; 30 import org.quartz.core.QuartzSchedulerResources; 31 import org.quartz.core.SchedulingContext; 32 import org.quartz.simpl.CascadingClassLoadHelper; 33 import org.quartz.simpl.RAMJobStore; 34 import org.quartz.simpl.SimpleThreadPool; 35 import org.quartz.spi.ClassLoadHelper; 36 import org.quartz.spi.JobStore; 37 import org.quartz.spi.SchedulerPlugin; 38 import org.quartz.spi.ThreadPool; 39 40 import java.util.Collection ; 41 import java.util.Iterator ; 42 import java.util.Map ; 43 44 97 public class DirectSchedulerFactory implements SchedulerFactory { 98 99 106 public static final String DEFAULT_INSTANCE_ID = "SIMPLE_NON_CLUSTERED"; 107 108 public static final String DEFAULT_SCHEDULER_NAME = "SimpleQuartzScheduler"; 109 110 117 118 private boolean initialized = false; 119 120 private static DirectSchedulerFactory instance = new DirectSchedulerFactory(); 121 122 private final Log log = LogFactory.getLog(getClass()); 123 124 131 132 protected Log getLog() { 133 return log; 134 } 135 136 139 protected DirectSchedulerFactory() { 140 } 141 142 149 150 public static DirectSchedulerFactory getInstance() { 151 return instance; 152 } 153 154 163 public void createVolatileScheduler(int maxThreads) 164 throws SchedulerException { 165 SimpleThreadPool threadPool = new SimpleThreadPool(maxThreads, 166 Thread.NORM_PRIORITY); 167 threadPool.initialize(); 168 JobStore jobStore = new RAMJobStore(); 169 this.createScheduler(threadPool, jobStore); 170 171 } 172 173 177 public void createVolatileSchduler(int maxThreads) 178 throws SchedulerException { 179 createVolatileScheduler(maxThreads); 180 } 181 182 193 public void createRemoteScheduler(String rmiHost, int rmiPort) 194 throws SchedulerException { 195 createRemoteScheduler(DEFAULT_SCHEDULER_NAME, DEFAULT_INSTANCE_ID, 196 rmiHost, rmiPort); 197 initialized = true; 198 } 199 200 218 public void createRemoteScheduler(String schedulerName, 219 String schedulerInstanceId, String rmiHost, int rmiPort) 220 throws SchedulerException { 221 createRemoteScheduler(schedulerName, 222 schedulerInstanceId, null, rmiHost, rmiPort); 223 } 224 225 246 public void createRemoteScheduler(String schedulerName, 247 String schedulerInstanceId, String rmiBindName, String rmiHost, int rmiPort) 248 throws SchedulerException { 249 SchedulingContext schedCtxt = new SchedulingContext(); 250 schedCtxt.setInstanceId(schedulerInstanceId); 251 252 String uid = (rmiBindName != null) ? rmiBindName : 253 QuartzSchedulerResources.getUniqueIdentifier( 254 schedulerName, schedulerInstanceId); 255 256 RemoteScheduler remoteScheduler = new RemoteScheduler(schedCtxt, uid, 257 rmiHost, rmiPort); 258 259 SchedulerRepository schedRep = SchedulerRepository.getInstance(); 260 schedRep.bind(remoteScheduler); 261 } 262 263 275 public void createScheduler(ThreadPool threadPool, JobStore jobStore) 276 throws SchedulerException { 277 createScheduler(DEFAULT_SCHEDULER_NAME, DEFAULT_INSTANCE_ID, 278 threadPool, jobStore); 279 initialized = true; 280 } 281 282 300 public void createScheduler(String schedulerName, 301 String schedulerInstanceId, ThreadPool threadPool, JobStore jobStore) 302 throws SchedulerException { 303 createScheduler(schedulerName, schedulerInstanceId, threadPool, 304 jobStore, null, 0, -1, -1); 305 } 306 307 330 public void createScheduler(String schedulerName, 331 String schedulerInstanceId, ThreadPool threadPool, 332 JobStore jobStore, String rmiRegistryHost, int rmiRegistryPort, 333 long idleWaitTime, long dbFailureRetryInterval) 334 throws SchedulerException { 335 createScheduler(schedulerName, 336 schedulerInstanceId, threadPool, 337 jobStore, null, rmiRegistryHost, rmiRegistryPort, 339 idleWaitTime, dbFailureRetryInterval); 340 } 341 342 369 public void createScheduler(String schedulerName, 370 String schedulerInstanceId, ThreadPool threadPool, 371 JobStore jobStore, Map schedulerPluginMap, 372 String rmiRegistryHost, int rmiRegistryPort, 373 long idleWaitTime, long dbFailureRetryInterval) 374 throws SchedulerException { 375 JobRunShellFactory jrsf = new StdJobRunShellFactory(); 377 378 SchedulingContext schedCtxt = new SchedulingContext(); 381 schedCtxt.setInstanceId(schedulerInstanceId); 382 383 QuartzSchedulerResources qrs = new QuartzSchedulerResources(); 384 385 qrs.setName(schedulerName); 386 qrs.setInstanceId(schedulerInstanceId); 387 qrs.setJobRunShellFactory(jrsf); 388 qrs.setThreadPool(threadPool); 389 qrs.setJobStore(jobStore); 390 qrs.setRMIRegistryHost(rmiRegistryHost); 391 qrs.setRMIRegistryPort(rmiRegistryPort); 392 393 if (schedulerPluginMap != null) { 395 for (Iterator pluginIter = schedulerPluginMap.values().iterator(); pluginIter.hasNext();) { 396 qrs.addSchedulerPlugin((SchedulerPlugin)pluginIter.next()); 397 } 398 } 399 400 QuartzScheduler qs = new QuartzScheduler(qrs, schedCtxt, idleWaitTime, 401 dbFailureRetryInterval); 402 403 ClassLoadHelper cch = new CascadingClassLoadHelper(); 404 cch.initialize(); 405 406 jobStore.initialize(cch, qs.getSchedulerSignaler()); 407 408 Scheduler scheduler = new StdScheduler(qs, schedCtxt); 409 410 if (schedulerPluginMap != null) { 412 for (Iterator pluginEntryIter = schedulerPluginMap.entrySet().iterator(); pluginEntryIter.hasNext();) { 413 Map.Entry pluginEntry = (Map.Entry )pluginEntryIter.next(); 414 415 ((SchedulerPlugin)pluginEntry.getValue()).initialize( 416 (String )pluginEntry.getKey(), scheduler); 417 } 418 } 419 420 jrsf.initialize(scheduler, schedCtxt); 421 422 getLog().info("Quartz scheduler '" + scheduler.getSchedulerName()); 423 424 getLog().info("Quartz scheduler version: " + qs.getVersion()); 425 426 SchedulerRepository schedRep = SchedulerRepository.getInstance(); 427 428 qs.addNoGCObject(schedRep); 431 schedRep.bind(scheduler); 432 } 433 434 441 442 452 public Scheduler getScheduler() throws SchedulerException { 453 if (!initialized) { 454 throw new SchedulerException( 455 "you must call createRemoteScheduler or createScheduler methods before calling getScheduler()"); 456 } 457 458 return getScheduler(DEFAULT_SCHEDULER_NAME); 459 } 460 461 466 public Scheduler getScheduler(String schedName) throws SchedulerException { 467 SchedulerRepository schedRep = SchedulerRepository.getInstance(); 468 469 return schedRep.lookup(schedName); 470 } 471 472 478 public Collection getAllSchedulers() throws SchedulerException { 479 return SchedulerRepository.getInstance().lookupAll(); 480 } 481 482 } 483 | Popular Tags |