com.sun.slamd.server
Class Scheduler

java.lang.Object
  extended byjava.lang.Thread
      extended bycom.sun.slamd.server.Scheduler
All Implemented Interfaces:
ConfigSubscriber, java.lang.Runnable

public class Scheduler
extends java.lang.Thread
implements ConfigSubscriber

This class acts as the job scheduler for the SLAMD server. It allows jobs to be submitted for processing (either immediately or at some point in the future) and handles all of the work necessary to make sure that they get started and stopped properly.


Field Summary
static java.lang.String CONFIG_SUBSCRIBER_NAME
          The name used to register the logger as a subscriber to the configuration handler.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Scheduler(SLAMDServer slamdServer)
          Creates a new instance of the SLAMD scheduler.
 
Method Summary
 void cancelAndDeleteJob(java.lang.String jobID)
          Cancels a job that has been submitted and removes it from the SLAMD server entirely.
 Job cancelJob(java.lang.String jobID, boolean waitForStop)
          Cancels a job that has been submitted.
 boolean cancelOptimizingJob(OptimizingJob optimizingJob)
          Cancels any iterations of the specified optimizing job that may be pending or running.
 void disableJob(java.lang.String jobID)
          Disables a job that is pending execution and updates the job state in the config directory.
 void enableJob(java.lang.String jobID)
          Enables a job that is currently disabled and updates the job state in the config directory.
 java.lang.String generateUniqueID()
          Generates a unique ID to use for identifying a job in the scheduler.
 int getCancelledJobCount()
          Retrieves the number of jobs that have been cancelled since the server started.
 int getCompletedJobCount()
          Retrieves the number of jobs that have completed running since the server started.
 int getCurrentScheduledJobCount()
          Retrieves the number of jobs that are currently scheduled (both pending and running).
 Job getJob(java.lang.String jobID)
          Retreives the requested job from the scheduler.
 Job getPendingJob(java.lang.String jobID)
          Retrieves the job with the requested ID from the pending jobs queue.
 int getPendingJobCount()
          Retrieves the number of jobs that are currently pending (waiting to run).
 Job[] getPendingJobs()
          Retrieves the set of jobs that are currently scheduled for execution but not yet running.
 java.lang.String getPendingReason(java.lang.String jobID)
          Retrieves information about why the specifiedjob is currently in the pending jobs queue and has not yet started running.
 Job getRunningJob(java.lang.String jobID)
          Retrieves the job with the requested ID from the running jobs queue.
 int getRunningJobCount()
          Retrieves the number of jobs that are currently running.
 Job[] getRunningJobs()
          Retrieves the set of jobs that are currently running.
 java.lang.String getSubscriberName()
          Retrieves the name that the scheduler uses to subscribe to the configuration handler in order to be notified of configuration changes.
 ParameterList getSubscriberParameters()
          Retrieves the set of configuration parameters associated with this configuration subscriber.
 int getTotalScheduledJobCount()
          Retrieves the total number of jobs that have been scheduled since the SLAMD server started.
 boolean isJobPending(java.lang.String jobID)
          Indicates whether the specified job is currently in the pending queue.
 boolean isJobRunning(java.lang.String jobID)
          Indicates whether the specified job is currently in the running queue.
 boolean isJobScheduled(java.lang.String jobID)
          Indicates whether the specified job is currently in either the pending or running queue.
 boolean isRunning()
          Indicates whether the scheduler is currently running.
 void jobDone(Job job)
          Updates the scheduler and the configuration to indicate that the job has completed processing.
 void pollForNewJobs()
          Polls the configuration directory to see if any new jobs have been added outside of the administrative interface.
 void refreshSubscriberConfiguration()
          Re-reads all configuration information used by the SLAMD scheduler.
 void refreshSubscriberConfiguration(java.lang.String parameterName)
          Re-reads the configuration information for the specified parameter.
 void run()
          Handles all of the real work of managing the jobs that have been scheduled, including making sure jobs are started at the appropriate time.
 java.lang.String scheduleJob(Job job, java.lang.String folderName)
          Submits a new job for processing and returns the job ID associated with that job.
 void scheduleOptimizingJob(OptimizingJob optimizingJob, java.lang.String folderName)
          Submits a new optimizing job for processing.
 void startScheduler()
          Starts the scheduler so that it can dispatch jobs to clients.
 boolean stopRequested()
          Indicates whether a request has been made to stop the scheduler.
 void stopScheduler()
          Stops the scheduler so that it will no longer dispatch new jobs to clients.
 void waitForStop()
          This method will not return until the scheduler has actually stopped.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CONFIG_SUBSCRIBER_NAME

public static final java.lang.String CONFIG_SUBSCRIBER_NAME
The name used to register the logger as a subscriber to the configuration handler.

See Also:
Constant Field Values
Constructor Detail

Scheduler

public Scheduler(SLAMDServer slamdServer)
          throws SLAMDServerException
Creates a new instance of the SLAMD scheduler. Once this has completed, the scheduler will allow new jobs to be submitted, but nothing will actually be started until the scheduler itself is started.

Parameters:
slamdServer - The SLAMD server with which this scheduler is associated.
Throws:
SLAMDServerException - If a problem occurs while initializing the scheduler.
Method Detail

startScheduler

public void startScheduler()
Starts the scheduler so that it can dispatch jobs to clients.


stopScheduler

public void stopScheduler()
Stops the scheduler so that it will no longer dispatch new jobs to clients. This will not stop any jobs that are currently in progress, nor will it clear the queue of pending jobs.


waitForStop

public void waitForStop()
This method will not return until the scheduler has actually stopped. Note that it does not stop the scheduler -- you should first call the stopScheduler method to signal the scheduler that it needs to stop.


scheduleJob

public java.lang.String scheduleJob(Job job,
                                    java.lang.String folderName)
                             throws SLAMDServerException
Submits a new job for processing and returns the job ID associated with that job.

Parameters:
job - The SLAMD job to be scheduled for processing.
folderName - The name of the folder in which the job should be placed.
Returns:
The job ID associated with the scheduled job.
Throws:
SLAMDServerException - If a problem is encountered while scheduling the job for execution.

scheduleOptimizingJob

public void scheduleOptimizingJob(OptimizingJob optimizingJob,
                                  java.lang.String folderName)
                           throws SLAMDServerException
Submits a new optimizing job for processing.

Parameters:
optimizingJob - The SLAMD optimizing job to be scheduled for processing.
folderName - The name of the folder in which the optimizing job should be placed.
Throws:
SLAMDServerException - If a problem is encountered while scheduling the job for execution.

cancelJob

public Job cancelJob(java.lang.String jobID,
                     boolean waitForStop)
Cancels a job that has been submitted. If the job has not yet been started, then it will simply be removed from the queue of pending jobs. If it has already started, then it will be stopped and removed from the queue of running jobs. If it has already completed, then no action will be taken.

Parameters:
jobID - Indicates which job should be stopped.
waitForStop - Indicates that if the job is currently running, whether the scheduler should wait for it to stop before returning.
Returns:
The SLAMD job that was cancelled, or null if no reference to that job could be found.

cancelAndDeleteJob

public void cancelAndDeleteJob(java.lang.String jobID)
Cancels a job that has been submitted and removes it from the SLAMD server entirely. This may only be performed on jobs that have not yet started running.

Parameters:
jobID - Indicates which job should be stopped.

cancelOptimizingJob

public boolean cancelOptimizingJob(OptimizingJob optimizingJob)
Cancels any iterations of the specified optimizing job that may be pending or running.

Parameters:
optimizingJob - The optimizing job for which to cancel any scheduled iterations.
Returns:
true if any iterations were found that are still running, or false if there were no running iterations found. Note that this return value only accounts for running jobs and not pending jobs because it is used by the caller to determine if it should wait for running jobs to complete.

disableJob

public void disableJob(java.lang.String jobID)
                throws SLAMDServerException
Disables a job that is pending execution and updates the job state in the config directory.

Parameters:
jobID - The job ID of the job to be disabled.
Throws:
SLAMDServerException - If a problem occurs while trying to disable the job.

enableJob

public void enableJob(java.lang.String jobID)
               throws SLAMDServerException
Enables a job that is currently disabled and updates the job state in the config directory.

Parameters:
jobID - The job ID of the job to be enabled.
Throws:
SLAMDServerException - If a problem occurs while trying to enable the job.

jobDone

public void jobDone(Job job)
Updates the scheduler and the configuration to indicate that the job has completed processing.

Parameters:
job - The job that the client has completed processing.

getJob

public Job getJob(java.lang.String jobID)
           throws SLAMDServerException
Retreives the requested job from the scheduler.

Parameters:
jobID - The job ID of the job to be retrieved.
Returns:
The requested job, or null if no reference to the specified job could be found.
Throws:
SLAMDServerException - If there is a problem while retrieving the specified job.

getPendingJobs

public Job[] getPendingJobs()
Retrieves the set of jobs that are currently scheduled for execution but not yet running.

Returns:
The set of jobs that are currently scheduled for execution but not yet running.

getPendingJob

public Job getPendingJob(java.lang.String jobID)
Retrieves the job with the requested ID from the pending jobs queue.

Returns:
The requested job, or null if it does not exist in the pending jobs queue.

getRunningJobs

public Job[] getRunningJobs()
Retrieves the set of jobs that are currently running.

Returns:
The set of jobs that are currently running.

getRunningJob

public Job getRunningJob(java.lang.String jobID)
Retrieves the job with the requested ID from the running jobs queue.

Returns:
The requested job, or null if it does not exist in the running jobs queue.

isJobPending

public boolean isJobPending(java.lang.String jobID)
Indicates whether the specified job is currently in the pending queue.

Parameters:
jobID - The ID of the job for which to make the determination.
Returns:
true if the specified job is in the pending queue, or false if it is not.

getPendingReason

public java.lang.String getPendingReason(java.lang.String jobID)
Retrieves information about why the specifiedjob is currently in the pending jobs queue and has not yet started running.

Parameters:
jobID - The job ID of the job for which to make the determination.
Returns:
A human-readable string that indicates why the job has not yet started running, or null if there is no such job in the pending jobs queue or if there is no reason that it should not start running.

isJobRunning

public boolean isJobRunning(java.lang.String jobID)
Indicates whether the specified job is currently in the running queue.

Parameters:
jobID - The ID of the job for which to make the determination.
Returns:
true if the specified job is in the running queue, or false if it is not.

isJobScheduled

public boolean isJobScheduled(java.lang.String jobID)
Indicates whether the specified job is currently in either the pending or running queue.

Parameters:
jobID - The ID of the job for which to make the determination.
Returns:
true if the specified job is in either queue, or false if it is not.

isRunning

public boolean isRunning()
Indicates whether the scheduler is currently running.

Returns:
true if the scheduler is running or false if it is not.

stopRequested

public boolean stopRequested()
Indicates whether a request has been made to stop the scheduler.

Returns:
true if a request has been made to stop the scheduler false if not.

run

public void run()
Handles all of the real work of managing the jobs that have been scheduled, including making sure jobs are started at the appropriate time.

Specified by:
run in interface java.lang.Runnable

pollForNewJobs

public void pollForNewJobs()
Polls the configuration directory to see if any new jobs have been added outside of the administrative interface. If so, then schedule them for execution.


generateUniqueID

public java.lang.String generateUniqueID()
Generates a unique ID to use for identifying a job in the scheduler.

Returns:
A unique ID to use for identifying a job in the scheduler.

getTotalScheduledJobCount

public int getTotalScheduledJobCount()
Retrieves the total number of jobs that have been scheduled since the SLAMD server started.

Returns:
The total number of jobs that have been scheduled since the SLAMD server started.

getCurrentScheduledJobCount

public int getCurrentScheduledJobCount()
Retrieves the number of jobs that are currently scheduled (both pending and running).

Returns:
The total number of jobs that are currently scheduled.

getRunningJobCount

public int getRunningJobCount()
Retrieves the number of jobs that are currently running.

Returns:
The number of jobs that are currently running.

getPendingJobCount

public int getPendingJobCount()
Retrieves the number of jobs that are currently pending (waiting to run).

Returns:
The number of jobs that are currently pending.

getCancelledJobCount

public int getCancelledJobCount()
Retrieves the number of jobs that have been cancelled since the server started.

Returns:
The number of jobs that have been cancelled since the server started.

getCompletedJobCount

public int getCompletedJobCount()
Retrieves the number of jobs that have completed running since the server started.

Returns:
The number of jobs that have completed since the server started.

getSubscriberName

public java.lang.String getSubscriberName()
Retrieves the name that the scheduler uses to subscribe to the configuration handler in order to be notified of configuration changes.

Specified by:
getSubscriberName in interface ConfigSubscriber
Returns:
The name that the scheduler uses to subscribe to the configuration handler in order to be notified of configuration changes.

getSubscriberParameters

public ParameterList getSubscriberParameters()
Retrieves the set of configuration parameters associated with this configuration subscriber.

Specified by:
getSubscriberParameters in interface ConfigSubscriber
Returns:
The set of configuration parameters associated with this configuration subscriber.

refreshSubscriberConfiguration

public void refreshSubscriberConfiguration()
Re-reads all configuration information used by the SLAMD scheduler. In this case, the only option is the delay between iterations of the scheduler loop.

Specified by:
refreshSubscriberConfiguration in interface ConfigSubscriber

refreshSubscriberConfiguration

public void refreshSubscriberConfiguration(java.lang.String parameterName)
Re-reads the configuration information for the specified parameter. In this case, the only option is the delay between iterations of the scheduler loop.

Specified by:
refreshSubscriberConfiguration in interface ConfigSubscriber
Parameters:
parameterName - The name of the parameter to re-read from the configuration.