Class PeriodicTrigger

java.lang.Object
org.springframework.scheduling.support.PeriodicTrigger
All Implemented Interfaces:
Trigger

public class PeriodicTrigger extends Object implements Trigger
A trigger for periodic task execution. The period may be applied as either fixed-rate or fixed-delay, and an initial delay value may also be configured. The default initial delay is 0, and the default behavior is fixed-delay (i.e. the interval between successive executions is measured from each completion time). To measure the interval between the scheduled start time of each execution instead, set the 'fixedRate' property to true.

Note that the TaskScheduler interface already defines methods for scheduling tasks at fixed-rate or with fixed-delay. Both also support an optional value for the initial delay. Those methods should be used directly whenever possible. The value of this Trigger implementation is that it can be used within components that rely on the Trigger abstraction. For example, it may be convenient to allow periodic triggers, cron-based triggers, and even custom Trigger implementations to be used interchangeably.

Since:
3.0
Author:
Mark Fisher
  • Constructor Details

    • PeriodicTrigger

      @Deprecated(since="6.0") public PeriodicTrigger(long period)
      Deprecated.
      as of 6.0, in favor on PeriodicTrigger(Duration)
      Create a trigger with the given period in milliseconds.
    • PeriodicTrigger

      @Deprecated(since="6.0") public PeriodicTrigger(long period, @Nullable TimeUnit timeUnit)
      Deprecated.
      as of 6.0, in favor on PeriodicTrigger(Duration)
      Create a trigger with the given period and time unit. The time unit will apply not only to the period but also to any 'initialDelay' value, if configured on this Trigger later via setInitialDelay(long).
    • PeriodicTrigger

      public PeriodicTrigger(Duration period)
      Create a trigger with the given period as a duration.
      Since:
      6.0
  • Method Details

    • getPeriod

      @Deprecated(since="6.0") public long getPeriod()
      Deprecated.
      as of 6.0, in favor on getPeriodDuration()
      Return this trigger's period.
      Since:
      5.0.2
    • getPeriodDuration

      public Duration getPeriodDuration()
      Return this trigger's period.
      Since:
      6.0
    • getTimeUnit

      @Deprecated(since="6.0") public TimeUnit getTimeUnit()
      Deprecated.
      as of 6.0, with no direct replacement
      Return this trigger's time unit (milliseconds by default).
      Since:
      5.0.2
    • setInitialDelay

      @Deprecated(since="6.0") public void setInitialDelay(long initialDelay)
      Deprecated.
      as of 6.0, in favor of setInitialDelay(Duration)
      Specify the delay for the initial execution. It will be evaluated in terms of this trigger's TimeUnit. If no time unit was explicitly provided upon instantiation, the default is milliseconds.
    • setInitialDelay

      public void setInitialDelay(Duration initialDelay)
      Specify the delay for the initial execution.
      Since:
      6.0
    • getInitialDelay

      @Deprecated(since="6.0") public long getInitialDelay()
      Deprecated.
      as of 6.0, in favor on getInitialDelayDuration()
      Return the initial delay, or 0 if none.
      Since:
      5.0.2
    • getInitialDelayDuration

      @Nullable public Duration getInitialDelayDuration()
      Return the initial delay, or null if none.
      Since:
      6.0
    • setFixedRate

      public void setFixedRate(boolean fixedRate)
      Specify whether the periodic interval should be measured between the scheduled start times rather than between actual completion times. The latter, "fixed delay" behavior, is the default.
    • isFixedRate

      public boolean isFixedRate()
      Return whether this trigger uses fixed rate (true) or fixed delay (false) behavior.
      Since:
      5.0.2
    • nextExecution

      public Instant nextExecution(TriggerContext triggerContext)
      Returns the time after which a task should run again.
      Specified by:
      nextExecution in interface Trigger
      Parameters:
      triggerContext - context object encapsulating last execution times and last completion time
      Returns:
      the next execution time as defined by the trigger, or null if the trigger won't fire anymore
    • equals

      public boolean equals(@Nullable Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object