Jive Forums API (5.5.20.2-oracle) Developer Javadocs

com.jivesoftware.util
Class CronTask

java.lang.Object
  extended by com.jivesoftware.util.CronTask
All Implemented Interfaces:
java.io.Serializable, java.lang.Runnable

public class CronTask
extends java.lang.Object
implements java.lang.Runnable, java.io.Serializable

A CronTask is a task which runs according to a schedule determined by a CronTimer. CronTasks are created and persisted by using an instance of a CronTaskManager.

In a clustered environment tasks are run on all cluster members. For a task to run only once across all cluster members the task must implement Invocable and do something similar to the following:

  public class MyTask implements Invocable {
      private long myvariable;
      private boolean isMaster = false;

      public void init(InvocationService invocationService) {
          if (invocationService.getInfo().getOldestMember().getId() ==
                  invocationService.getCluster().getLocalMember().getId())
          {
              isMaster = true;
          }
      }

      public void run() {
          // Only run this task on the master cluster member.
          if (CacheFactory.isClusteringEnabled() && !isMaster) {
              return;
          }

          // do something here
      }

      public boolean equals(Object o) {
         if (this == o) {
             return true;
         }
         if (!(o instanceof MyTask)) {
             return false;
         }

         final MyTask myTask = (MyTask) o;

         if (myvariable != myTask.myvariable) {
             return false;
         }

         return true;
      }

      public int hashCode() {
          return (int) (myvariable ^ (myvariable >>> 32));
      }
  }
 
Tasks must implement Serializable and override Object.equals(java.lang.Object) to work properly in a clustered environment. Failure to do so will result in errors.

Any changes to the cronTimer used in this class should be followed by a call to CronTaskManager.saveCronTask(CronTask) so that the changes are persisted.

See Also:
Serialized Form

Constructor Summary
protected CronTask(long id, java.lang.Runnable task, CronTimer cronTimer)
           
protected CronTask(java.lang.Runnable task, CronTimer cronTimer)
           
 
Method Summary
 boolean equals(java.lang.Object o)
          Both timer and task are used to compare equality.
 CronTimer getCronTimer()
          Returns the CronTimer instance used to schedule this task.
 long getID()
          Returns the unique id of the crontask.
 java.lang.Runnable getRunnable()
          Returns the Runnable object which is run on the schedule defined by the CronTimer.
 int hashCode()
           
 boolean isStopped()
          Returns true is the CronTask is stopped, false otherwise.
 void run()
           
 void setCronTimer(CronTimer cronTimer)
          Sets the CronTimer to use to schedule this task.
 void setCronTimerNoCluster(CronTimer cronTimer)
          * This method is only intended to be used internally to Jive by the CronClusterUpdateTask class.
 void start()
          Start the CronTask running after it has been previously stopped.
 void stop()
          Stop the CronTask from running.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CronTask

protected CronTask(java.lang.Runnable task,
                   CronTimer cronTimer)

CronTask

protected CronTask(long id,
                   java.lang.Runnable task,
                   CronTimer cronTimer)
Method Detail

getID

public long getID()
Returns the unique id of the crontask.

Returns:
the unique id of the crontask.

getCronTimer

public CronTimer getCronTimer()
Returns the CronTimer instance used to schedule this task.

Returns:
the CronTimer instance used to schedule this task

setCronTimer

public void setCronTimer(CronTimer cronTimer)
Sets the CronTimer to use to schedule this task.

Parameters:
cronTimer - the CronTimer to use to schedule this task

setCronTimerNoCluster

public void setCronTimerNoCluster(CronTimer cronTimer)
* This method is only intended to be used internally to Jive by the CronClusterUpdateTask class.

Parameters:
cronTimer - the CronTimer to use to schedule this task

getRunnable

public java.lang.Runnable getRunnable()
Returns the Runnable object which is run on the schedule defined by the CronTimer.

Returns:
the Runnable object which is run on the schedule defined by the CronTimer

run

public void run()
Specified by:
run in interface java.lang.Runnable

start

public void start()
Start the CronTask running after it has been previously stopped.


stop

public void stop()
Stop the CronTask from running. This will not remove the crontask from the list of tasks managed by the CronTaskManager - use CronTaskManager.removeCronTask(CronTask) to do that.


isStopped

public boolean isStopped()
Returns true is the CronTask is stopped, false otherwise.


equals

public boolean equals(java.lang.Object o)
Both timer and task are used to compare equality. This means that the runnable task should really override the default Object.equals(Object o) method for this to work properly.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

Jive Forums Project Page

Copyright © 1999-2006 Jive Software.