Anonymous avatar Anonymous committed 28cf455

QUARTZ-658: Introduce mechanism for delaying the starttup of the scheduler

git-svn-id: http://svn.opensymphony.com/svn/quartz/trunk@776 69f7d36a-ea1c-0410-88ea-9fd03e4c9665

Comments (0)

Files changed (8)

src/java/org/quartz/Scheduler.java

      * @throws SchedulerException
      *           if <code>shutdown()</code> has been called, or there is an
      *           error within the <code>Scheduler</code>.
-     * 
-     * @see #standby
-     * @see #shutdown
+     *
+     * @see #startDelayed(int)
+     * @see #standby()
+     * @see #shutdown()
      */
     void start() throws SchedulerException;
 
     /**
+     * <p>
+     * Calls {#start()} after the indicated number of seconds.
+     * (This call does not block). This can be useful within applications that
+     * have initializers that create the scheduler immediately, before the
+     * resources needed by the executing jobs have been fully initialized.
+     * </p>
+     *
+     * @throws SchedulerException
+     *           if <code>shutdown()</code> has been called, or there is an
+     *           error within the <code>Scheduler</code>.
+     *
+     * @see #start() 
+     * @see #standby()
+     * @see #shutdown()
+     */
+    void startDelayed(int seconds) throws SchedulerException;
+
+    /**
      * Whether the scheduler has been started.  
      * 
      * <p>

src/java/org/quartz/core/QuartzScheduler.java

                 "Scheduler " + resources.getUniqueIdentifier() + " started.");
     }
 
+    public void startDelayed(final int seconds) throws SchedulerException
+    {
+        if (closed) {
+            throw new SchedulerException(
+                    "The Scheduler cannot be restarted after shutdown() has been called.");
+        }
+
+        Thread t = new Thread(new Runnable() {
+            public void run() {
+                try { Thread.sleep(seconds * 1000L); }
+                catch(InterruptedException ignore) {}
+                try { start(); }
+                catch(SchedulerException se) {
+                    getLog().error("Unable to start secheduler after startup delay.", se);
+                }
+            }
+        });
+    }
+
     /**
      * <p>
      * Temporarily halts the <code>QuartzScheduler</code>'s firing of <code>{@link org.quartz.Trigger}s</code>.

src/java/org/quartz/core/RemotableQuartzScheduler.java

 
     void start() throws SchedulerException, RemoteException;
 
+    void startDelayed(int seconds) throws SchedulerException, RemoteException;
+    
     void standby() throws RemoteException;
 
     boolean isInStandbyMode() throws RemoteException;

src/java/org/quartz/ee/servlet/QuartzInitializerListener.java

  * which case you will want to use this name rather than 
  * <code>QuartzInitializerListener.QUARTZ_FACTORY_KEY</code> in the above example.
  * </p>
- * 
+ *
+ * <p>
+ * The init parameter 'start-delay-seconds' can be used to specify the amount
+ * of time to wait after initializing the scheduler before scheduler.start()
+ * is called.
+ * </p>
+ *
  * Once you have the factory instance, you can retrieve the Scheduler instance by calling
  * <code>getScheduler()</code> on the factory.
  *
             // Should the Scheduler being started now or later
             String startOnLoad = servletContext
                     .getInitParameter("start-scheduler-on-load");
+
+            int startDelay = 0;
+            String startDelayS = servletContext.getInitParameter("start-delay-seconds");
+            try {
+                if(startDelayS != null && startDelayS.trim().length() > 0)
+                    startDelay = Integer.parseInt(startDelayS);
+            } catch(Exception e) {
+                System.out.println("Cannot parse value of 'start-delay-seconds' to an integer: " + startDelayS + ", defaulting to 5 seconds.");
+                startDelay = 5;
+            }
+
             /*
              * If the "start-scheduler-on-load" init-parameter is not specified,
              * the scheduler will be started. This is to maintain backwards
              * compatability.
              */
             if (startOnLoad == null || (Boolean.valueOf(startOnLoad).booleanValue())) {
-                // Start now
-                scheduler.start();
-                System.out.println("Scheduler has been started...");
+                if(startDelay <= 0) {
+                    // Start now
+                    scheduler.start();
+                    System.out.println("Scheduler has been started...");
+                }
+                else {
+                    // Start delayed
+                    scheduler.startDelayed(startDelay);
+                    System.out.println("Scheduler will start in " + startDelay + " seconds.");
+                }
             } else {
                 System.out.println("Scheduler has not been started. Use scheduler.start()");
             }

src/java/org/quartz/ee/servlet/QuartzInitializerServlet.java

  * <code>QuartzFactoryServlet.QUARTZ_FACTORY_KEY</code> in the above example.
  * </p>
  * 
+ * <p>
+ * The init parameter 'start-delay-seconds' can be used to specify the amount
+ * of time to wait after initializing the scheduler before scheduler.start()
+ * is called.
+ * </p>
+ *
  * Once you have the factory instance, you can retrieve the Scheduler instance by calling
  * <code>getScheduler()</code> on the factory.
  *
             // Should the Scheduler being started now or later
             String startOnLoad = cfg
                     .getInitParameter("start-scheduler-on-load");
+
+            int startDelay = 0;
+            String startDelayS = cfg.getInitParameter("start-delay-seconds");
+            try {
+                if(startDelayS != null && startDelayS.trim().length() > 0)
+                    startDelay = Integer.parseInt(startDelayS);
+            } catch(Exception e) {
+                log("Cannot parse value of 'start-delay-seconds' to an integer: " + startDelayS + ", defaulting to 5 seconds.", e);
+                startDelay = 5;
+            }
+            
             /*
              * If the "start-scheduler-on-load" init-parameter is not specified,
              * the scheduler will be started. This is to maintain backwards
              * compatability.
              */
             if (startOnLoad == null || (Boolean.valueOf(startOnLoad).booleanValue())) {
-                // Start now
-                scheduler.start();
-                log("Scheduler has been started...");
+                if(startDelay <= 0) {
+                    // Start now
+                    scheduler.start();
+                    log("Scheduler has been started...");
+                }
+                else {
+                    // Start delayed
+                    scheduler.startDelayed(startDelay);
+                    log("Scheduler will start in " + startDelay + " seconds.");
+                }
             } else {
                 log("Scheduler has not been started. Use scheduler.start()");
             }

src/java/org/quartz/impl/RemoteMBeanScheduler.java

      * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
      * </p>
      */
+    public void startDelayed(int seconds) throws SchedulerException {
+        invoke("startDelayed", new Object[] {new Integer(seconds)}, new String[] {int.class.getName()});
+    }
+    
+    /**
+     * <p>
+     * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
+     * </p>
+     */
     public void standby() throws SchedulerException {
         invoke("standby", new Object[] {}, new String[] {});
     }

src/java/org/quartz/impl/RemoteScheduler.java

      * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
      * </p>
      */
+    public void startDelayed(int seconds) throws SchedulerException {
+        try {
+            getRemoteScheduler().startDelayed(seconds);
+        } catch (RemoteException re) {
+            throw invalidateHandleCreateException(
+                    "Error communicating with remote scheduler.", re);
+        }
+    }
+    
+    /**
+     * <p>
+     * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
+     * </p>
+     */
     public void standby() throws SchedulerException {
         try {
             getRemoteScheduler().standby();

src/java/org/quartz/impl/StdScheduler.java

      * <p>
      * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
      * </p>
+     */
+    public void startDelayed(int seconds) throws SchedulerException {
+        sched.startDelayed(seconds);
+    }
+
+
+    /**
+     * <p>
+     * Calls the equivalent method on the 'proxied' <code>QuartzScheduler</code>.
+     * </p>
      * 
      * @deprecated
      * @see #standby()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.