Class JobSupport

java.lang.Object
net.solarnetwork.central.scheduler.JobSupport
All Implemented Interfaces:
Runnable, ManagedJob
Direct Known Subclasses:
DirectoryCleanerJob, JdbcCallJob, SharedValueCacheCleaner

public abstract class JobSupport extends Object implements ManagedJob
Base helper class for a scheduled job.

This job supports running a set of tasks in parallel by configuring the setParallelism(int) property to something greater than 1. The parallel tasks can compete for a pool of task iterations so that each task competes for a bounded amount of work. This is useful for scenarios like batch processing a queue, where the job is designed to process up to a maximum number of queued items each time it runs using parallel workers.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The schedule property default value.
    static final long
    The jitter property default value.
    static final int
    The maximumIterations property default value.
    static final long
    The maximumWaitMs property default value.
    static final int
    The parallelism property default value.
    protected final org.slf4j.Logger
    A class-level logger.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected int
    executeJobTask(AtomicInteger remainingIterataions)
    Execute a parallel job task.
    protected final boolean
    Execute the job in parallel via multiple threads.
    Get the job group to use.
    Get the unique ID of the job to schedule.
    long
    Get a maximum amount of time, in milliseconds, to randomly add to the start of parallel tasks so they don't all try to start so closely together.
    int
    Get the maximum number of iterations of the job task to run.
    long
    Get the maximum time, in milliseconds, to allow for the job to execute before it is considered a failed job.
    int
    Get the number of parallel threads to use while processing task iterations.
    org.springframework.core.task.AsyncTaskExecutor
    Get the executor to handle parallel job tasks with.
    Get the job cron expression to use for scheduling this job.
    void
    setGroupId(String jobGroup)
    Set the job group to use.
    void
    setId(String jobId)
    Set the unique ID of the job to schedule.
    void
    setJitter(long jitter)
    Set et a maximum amount of time, in milliseconds, to randomly add to the start of parallel tasks so they don't all try to start so closely together.
    void
    setMaximumIterations(int maximumIterations)
    Set the maximum number of claims to acquire per execution of this job.
    void
    setMaximumWaitMs(long maximumWaitMs)
    Set the maximum time, in milliseconds, to allow for the job to execute before it is considered a failed job.
    void
    setParallelism(int parallelism)
    Set the number of parallel threads to use while processing task iterations.
    void
    setParallelTaskExecutor(org.springframework.core.task.AsyncTaskExecutor parallelTaskExecutorService)
    Set the executor to handle parallel job tasks with.
    void
    Set the job cron expression to use for scheduling this job.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Runnable

    run
  • Field Details

    • DEFAULT_MAX_WAIT

      public static final long DEFAULT_MAX_WAIT
      The maximumWaitMs property default value.
      See Also:
    • DEFAULT_CRON

      public static final String DEFAULT_CRON
      The schedule property default value.
      See Also:
    • DEFAULT_MAX_ITERATIONS

      public static final int DEFAULT_MAX_ITERATIONS
      The maximumIterations property default value.
      See Also:
    • DEFAULT_PARALLELISM

      public static final int DEFAULT_PARALLELISM
      The parallelism property default value.
      See Also:
    • DEFAULT_JITTER

      public static final long DEFAULT_JITTER
      The jitter property default value.
      See Also:
    • log

      protected final org.slf4j.Logger log
      A class-level logger.
  • Constructor Details

    • JobSupport

      public JobSupport()
  • Method Details

    • executeParallelJob

      protected final boolean executeParallelJob(String taskName)
      Execute the job in parallel via multiple threads.

      This method can be invoked by extending classes from their Runnable.run() method. When invoked, this method will create parallelism tasks and submit them to the configured parallelTaskExecutor. Each task will call the executeJobTask(AtomicInteger) method, passing a shared AtomicInteger initially set to maximumIterations, which serves as a hint as to the number of overall iterations all tasks are trying to perform. The tasks thus compete for iterations and should decrement the AtomicInteger by the number of iterations they process, and stop processing iterations when the count reaches 0 or less.

      Note that this method does not track the iteration count. It merely waits for each task to complete, by returning or throwing an exception, for up to maximumWaitMs milliseconds.

      If parallelism is 1 then the executeJobTask(AtomicInteger) method is called directly from this method, without submitting the task to the configured executorService.

      Parameters:
      taskName - a descriptive name for the job task, to use in logging
      job - the job event
      Returns:
      the true if all tasks were completed
      Throws:
      Exception - if any error occurs
      Since:
      1.7
    • executeJobTask

      protected int executeJobTask(AtomicInteger remainingIterataions) throws Exception
      Execute a parallel job task.

      This method is called from the executeParallelJob(String) method by each thread. This method is supposed to execute up to remainingIterataions of the job's task, updating remainingIterataions as each iteration is processed. Keep in mind that each job task thread will be mutating (competing for) remainingIterataions.

      This method throws a UnsupportedOperationException and must be overridden by extending classes.

      Parameters:
      remainingIterataions - the number of iterations left to perform
      Returns:
      the number of iterations performed
      Throws:
      Exception - if any error occurs
      Since:
      1.7
    • getId

      public String getId()
      Get the unique ID of the job to schedule.
      Specified by:
      getId in interface ManagedJob
      Returns:
      the job ID
    • setId

      public void setId(String jobId)
      Set the unique ID of the job to schedule.
      Parameters:
      id - the job ID
    • getMaximumWaitMs

      public long getMaximumWaitMs()
      Get the maximum time, in milliseconds, to allow for the job to execute before it is considered a failed job.
      Returns:
      the maximum wait, in milliseconds; defaults to 15 minutes
    • setMaximumWaitMs

      public void setMaximumWaitMs(long maximumWaitMs)
      Set the maximum time, in milliseconds, to allow for the job to execute before it is considered a failed job.
      Parameters:
      maximumWaitMs - the maximum wait
    • getSchedule

      public String getSchedule()
      Get the job cron expression to use for scheduling this job.
      Specified by:
      getSchedule in interface ManagedJob
      Returns:
      the cron expression; defaults to 0 0/1 * * * ? (once per minute)
    • setSchedule

      public void setSchedule(String jobCron)
      Set the job cron expression to use for scheduling this job.
      Parameters:
      schedule - the cron expression
    • getGroupId

      public String getGroupId()
      Get the job group to use.
      Specified by:
      getGroupId in interface ManagedJob
      Returns:
      the job group
    • setGroupId

      public void setGroupId(String jobGroup)
      Set the job group to use.
      Parameters:
      groupId - the job group
    • getParallelTaskExecutor

      public org.springframework.core.task.AsyncTaskExecutor getParallelTaskExecutor()
      Get the executor to handle parallel job tasks with.

      If not defined, then

      invalid reference
      #getExecutorService()
      will be used.

      Returns:
      the service
    • setParallelTaskExecutor

      public void setParallelTaskExecutor(org.springframework.core.task.AsyncTaskExecutor parallelTaskExecutorService)
      Set the executor to handle parallel job tasks with.
      Parameters:
      parallelTaskExecutor - the service to set
    • getMaximumIterations

      public int getMaximumIterations()
      Get the maximum number of iterations of the job task to run.
      Returns:
      the maximum iterations; defaults to 1
      Since:
      1.7
    • setMaximumIterations

      public void setMaximumIterations(int maximumIterations)
      Set the maximum number of claims to acquire per execution of this job.
      Parameters:
      maximumIterations - the maximum iterations
      Since:
      1.7
    • getParallelism

      public int getParallelism()
      Get the number of parallel threads to use while processing task iterations.
      Returns:
      the parallelism; defaults to 1
      Since:
      1.7
    • setParallelism

      public void setParallelism(int parallelism)
      Set the number of parallel threads to use while processing task iterations.
      Parameters:
      parallelism - the parallelism to set; will be forced to 1 if < 1
      Since:
      1.7
    • getJitter

      public long getJitter()
      Get a maximum amount of time, in milliseconds, to randomly add to the start of parallel tasks so they don't all try to start so closely together.
      Returns:
      the jitter, in milliseconds; defaults to DEFAULT_JITTER
    • setJitter

      public void setJitter(long jitter)
      Set et a maximum amount of time, in milliseconds, to randomly add to the start of parallel tasks so they don't all try to start so closely together.

      This time is added to tasks started by the executeParallelJob(String) method, and only when getParallelism() is greater than 1. Set to 0 to disable adding any random jitter to the start of tasks.

      Parameters:
      jitter - the jitter to set, in milliseconds