Class Scheduler

java.lang.Object
com.codename1.backend.Scheduler

public final class Scheduler extends Object

Runs the @Scheduled methods of a server.

The build registers each job from the entry point it generates -- the cron masks already computed, the executor and thread kind already chosen -- and starts this once the server is accepting. One platform thread keeps the timetable; the jobs themselves run on their executors, so a slow job delays nothing but its own next run.

A run never overlaps the previous run of the same job. A fire that finds the previous run still going is skipped, and the job is next due at its following time, which is what keeps a job that has fallen behind from stacking up copies of itself.

With a lock, a run first claims a row in cn1_scheduler_lock and is skipped when another instance holds it, so a job runs on one replica at a time. The claim expires after lockAtMostFor milliseconds, so a replica that dies mid-run does not keep it.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    One registered job and what has happened to it.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final int
     
    static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Parameters
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    bind(Backend backend)
    Takes the measuring and tracing of the server that runs this scheduler.
    void
    cron(String name, CronSchedule schedule, String executor, int threadKind, String lock, long lockAtMostFor, Runnable body)
    Registers a cron job.
    Every job and what has happened to it, for the management endpoint and MCP.
    void
    fixedDelay(String name, long initialDelay, long period, String executor, int threadKind, String lock, long lockAtMostFor, Runnable body)
    Registers a job that starts period milliseconds after the last run ended.
    void
    fixedRate(String name, long initialDelay, long period, String executor, int threadKind, String lock, long lockAtMostFor, Runnable body)
    Registers a job that starts every period milliseconds.
    The registered jobs.
    void
    setMeasured(boolean measured)
    Set by the server once it knows whether it records metrics.
    void
    Starts the timetable.
    void
    stop(long waitMillis)
    Stops starting runs and waits up to waitMillis for the ones in progress.
    boolean
    Runs a job now, on its executor, whatever its schedule says.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • Scheduler

      public Scheduler(DataSource locks)
      Parameters
      • locks: @param locks the pool locked jobs claim their row in, or null when this server has no database -- the build refuses a lock without one
  • Method Details

    • setMeasured

      public void setMeasured(boolean measured)
      Set by the server once it knows whether it records metrics.
    • bind

      public void bind(Backend backend)
      Takes the measuring and tracing of the server that runs this scheduler. Called by the generated application before it starts the scheduler.
    • cron

      public void cron(String name, CronSchedule schedule, String executor, int threadKind, String lock, long lockAtMostFor, Runnable body)
      Registers a cron job. Generated code calls this before start().
    • fixedRate

      public void fixedRate(String name, long initialDelay, long period, String executor, int threadKind, String lock, long lockAtMostFor, Runnable body)
      Registers a job that starts every period milliseconds.
    • fixedDelay

      public void fixedDelay(String name, long initialDelay, long period, String executor, int threadKind, String lock, long lockAtMostFor, Runnable body)
      Registers a job that starts period milliseconds after the last run ended.
    • start

      public void start()
      Starts the timetable.
    • stop

      public void stop(long waitMillis)
      Stops starting runs and waits up to waitMillis for the ones in progress.
    • trigger

      public boolean trigger(String name)
      Runs a job now, on its executor, whatever its schedule says. Answers false when no job has that name or its previous run is still going.
    • describe

      public List describe()
      Every job and what has happened to it, for the management endpoint and MCP.
    • getJobs

      public List getJobs()
      The registered jobs.