Top Description Fields Constructors Methods
org.springframework.scheduling.support

public Class PeriodicTrigger

extends Object
implements Trigger
Class Inheritance
All Implemented Interfaces
org.springframework.scheduling.Trigger
Imports
java.time.Duration, .Instant, java.time.temporal.ChronoUnit, java.util.concurrent.TimeUnit, org.springframework.lang.Nullable, org.springframework.scheduling.Trigger, .TriggerContext, org.springframework.util.Assert, .ObjectUtils

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.

Author
Mark Fisher
Since
3.0

Field Summary

Modifier and TypeField and Description
private final ChronoUnit
private volatile boolean
private volatile Duration
private final Duration

Constructor Summary

AccessConstructor and Description
public
PeriodicTrigger(long period)

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

Deprecated since 6.0. as of 6.0, in favor on PeriodicTrigger(Duration)
Create a trigger with the given period and time unit.
public
PeriodicTrigger(Duration period)

Create a trigger with the given period as a duration.

private
PeriodicTrigger(Duration period, TimeUnit timeUnit)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public long
getInitialDelay()

Deprecated since 6.0. as of 6.0, in favor on getInitialDelayDuration()
Return the initial delay, or 0 if none.
public Duration
getInitialDelayDuration()

Return the initial delay, or null if none.

public long
getPeriod()

Deprecated since 6.0. as of 6.0, in favor on getPeriodDuration()
Return this trigger's period.
public Duration
getPeriodDuration()

Return this trigger's period.

public TimeUnit
getTimeUnit()

Deprecated since 6.0. as of 6.0, with no direct replacement
Return this trigger's time unit (milliseconds by default).
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public boolean
isFixedRate()

Return whether this trigger uses fixed rate (true) or fixed delay (false) behavior.

public Instant
nextExecution(TriggerContext
context object encapsulating last execution times and last completion time
triggerContext
)

Implements org.springframework.scheduling.Trigger.nextExecution.

Returns the time after which a task should run again.
public void
setFixedRate(boolean fixedRate)

Specify whether the periodic interval should be measured between the scheduled start times rather than between actual completion times.

public void
setInitialDelay(long initialDelay)

Deprecated since 6.0. as of 6.0, in favor of setInitialDelay(Duration)
Specify the delay for the initial execution.
public void
setInitialDelay(Duration initialDelay)

Specify the delay for the initial execution.

private static Duration
toDuration(long amount, TimeUnit timeUnit)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

chronoUnitback to summary
private final ChronoUnit chronoUnit
Annotations
@Nullable
fixedRateback to summary
private volatile boolean fixedRate
initialDelayback to summary
private volatile Duration initialDelay
Annotations
@Nullable
periodback to summary
private final Duration period

Constructor Detail

PeriodicTriggerback to summary
public PeriodicTrigger(long period)

Deprecated

since 6.0.

as of 6.0, in favor on PeriodicTrigger(Duration)

Create a trigger with the given period in milliseconds.

Annotations
@Deprecated
since:6.0
PeriodicTriggerback to summary
public PeriodicTrigger(long period, TimeUnit timeUnit)

Deprecated

since 6.0.

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).

Annotations
@Deprecated
since:6.0
PeriodicTriggerback to summary
public PeriodicTrigger(Duration period)

Create a trigger with the given period as a duration.

Since
6.0
PeriodicTriggerback to summary
private PeriodicTrigger(Duration period, TimeUnit timeUnit)

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getInitialDelayback to summary
public long getInitialDelay()

Deprecated

since 6.0.

as of 6.0, in favor on getInitialDelayDuration()

Return the initial delay, or 0 if none.

Annotations
@Deprecated
since:6.0
Since
5.0.2
getInitialDelayDurationback to summary
public Duration getInitialDelayDuration()

Return the initial delay, or null if none.

Annotations
@Nullable
Since
6.0
getPeriodback to summary
public long getPeriod()

Deprecated

since 6.0.

as of 6.0, in favor on getPeriodDuration()

Return this trigger's period.

Annotations
@Deprecated
since:6.0
Since
5.0.2
getPeriodDurationback to summary
public Duration getPeriodDuration()

Return this trigger's period.

Since
6.0
getTimeUnitback to summary
public TimeUnit getTimeUnit()

Deprecated

since 6.0.

as of 6.0, with no direct replacement

Return this trigger's time unit (milliseconds by default).

Annotations
@Deprecated
since:6.0
Since
5.0.2
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
isFixedRateback to summary
public boolean isFixedRate()

Return whether this trigger uses fixed rate (true) or fixed delay (false) behavior.

Since
5.0.2
nextExecutionback to summary
public Instant nextExecution(TriggerContext triggerContext)

Implements org.springframework.scheduling.Trigger.nextExecution.

Returns the time after which a task should run again.

Parameters
triggerContext:TriggerContext

Doc from org.springframework.scheduling.Trigger.nextExecution.

context object encapsulating last execution times and last completion time

Returns:Instant

Doc from org.springframework.scheduling.Trigger.nextExecution.

the next execution time as defined by the trigger, or null if the trigger won't fire anymore

Annotations
@Override
setFixedRateback to summary
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.

setInitialDelayback to summary
public void setInitialDelay(long initialDelay)

Deprecated

since 6.0.

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.

Annotations
@Deprecated
since:6.0
setInitialDelayback to summary
public void setInitialDelay(Duration initialDelay)

Specify the delay for the initial execution.

Since
6.0
toDurationback to summary
private static Duration toDuration(long amount, TimeUnit timeUnit)