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

public Class CronTrigger

extends Object
implements Trigger
Class Inheritance
All Implemented Interfaces
org.springframework.scheduling.Trigger
Imports
java.time.Instant, .ZoneId, .ZonedDateTime, java.util.TimeZone, org.springframework.lang.Nullable, org.springframework.scheduling.Trigger, .TriggerContext, org.springframework.util.Assert

Trigger implementation for cron expressions. Wraps a CronExpression which parses according to common crontab conventions.

Supports a Quartz day-of-month/week field with an L/# expression. Follows common cron conventions in every other respect, including 0-6 for SUN-SAT (plus 7 for SUN as well). Note that Quartz deviates from the day-of-week convention in cron through 1-7 for SUN-SAT whereas Spring strictly follows cron even in combination with the optional Quartz-specific L/# expressions.

Authors
Juergen Hoeller, Arjen Poutsma
Since
3.0
See Also
CronExpression

Field Summary

Modifier and TypeField and Description
private final CronExpression
private final ZoneId

Constructor Summary

AccessConstructor and Description
public
CronTrigger(String
a space-separated list of time fields, following cron expression conventions
expression
)

Build a CronTrigger from the pattern provided in the default time zone.

public
CronTrigger(String
a space-separated list of time fields, following cron expression conventions
expression
,
TimeZone
a time zone in which the trigger times will be generated
timeZone
)

Build a CronTrigger from the pattern provided in the given time zone, with the same lenient execution as CronTrigger#CronTrigger(String).

public
CronTrigger(String
a space-separated list of time fields, following cron expression conventions
expression
,
ZoneId
a time zone in which the trigger times will be generated
zoneId
)

Build a CronTrigger from the pattern provided in the given time zone, with the same lenient execution as CronTrigger#CronTrigger(String).

Method Summary

Modifier and TypeMethod and Description
pack-priv Instant
pack-priv Instant
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 static CronTrigger
forFixedExecution(String
a space-separated list of time fields, following cron expression conventions
expression
)

Create a CronTrigger for fixed execution, to be rescheduled after every task based on the last scheduled time.

public static CronTrigger
forLenientExecution(String
a space-separated list of time fields, following cron expression conventions
expression
)

Create a CronTrigger for lenient execution, to be rescheduled after every task based on the completion time.

public String
getExpression()

Return the cron pattern that this trigger has been built with.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

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

Implements org.springframework.scheduling.Trigger.nextExecution.

Determine the next execution time according to the given trigger context.

public static CronTrigger
resumeFixedExecution(String
a space-separated list of time fields, following cron expression conventions
expression
,
Instant
the timestamp to resume from (the last-known scheduled timestamp), with every trigger in-between immediately firing to make up for every execution that would have happened in the meantime
resumptionTimestamp
)

Create a CronTrigger for fixed execution, to be rescheduled after every task based on the last scheduled time.

public static CronTrigger
resumeLenientExecution(String
a space-separated list of time fields, following cron expression conventions
expression
,
Instant
the timestamp to resume from (the last-known completion timestamp), with the new trigger calculated from there and possibly immediately firing (but only once, every subsequent calculation will start from the completion time of that first resumed trigger)
resumptionTimestamp
)

Create a CronTrigger for lenient execution, to be rescheduled after every task based on the completion time.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

expressionback to summary
private final CronExpression expression
zoneIdback to summary
private final ZoneId zoneId
Annotations
@Nullable

Constructor Detail

CronTriggerback to summary
public CronTrigger(String expression)

Build a CronTrigger from the pattern provided in the default time zone.

This is equivalent to the CronTrigger#forLenientExecution factory method. Original trigger firings may be skipped if the previous task is still running; if this is not desirable, consider CronTrigger#forFixedExecution.

Parameters
expression:String

a space-separated list of time fields, following cron expression conventions

See Also
CronTrigger#forLenientExecution, CronTrigger#forFixedExecution
CronTriggerback to summary
public CronTrigger(String expression, TimeZone timeZone)

Build a CronTrigger from the pattern provided in the given time zone, with the same lenient execution as CronTrigger#CronTrigger(String).

Note that such explicit time zone customization is usually not necessary, using org.springframework.scheduling.TaskScheduler#getClock() instead.

Parameters
expression:String

a space-separated list of time fields, following cron expression conventions

timeZone:TimeZone

a time zone in which the trigger times will be generated

CronTriggerback to summary
public CronTrigger(String expression, ZoneId zoneId)

Build a CronTrigger from the pattern provided in the given time zone, with the same lenient execution as CronTrigger#CronTrigger(String).

Note that such explicit time zone customization is usually not necessary, using org.springframework.scheduling.TaskScheduler#getClock() instead.

Parameters
expression:String

a space-separated list of time fields, following cron expression conventions

zoneId:ZoneId

a time zone in which the trigger times will be generated

Since
5.3
See Also
CronExpression#parse(String)

Method Detail

determineInitialTimestampback to summary
pack-priv Instant determineInitialTimestamp(TriggerContext triggerContext)
determineLatestTimestampback to summary
pack-priv Instant determineLatestTimestamp(TriggerContext triggerContext)
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
forFixedExecutionback to summary
public static CronTrigger forFixedExecution(String expression)

Create a CronTrigger for fixed execution, to be rescheduled after every task based on the last scheduled time.

This variant makes up for missed trigger firings if the associated task has taken too long, scheduling a task for every original trigger firing. Such follow-up tasks may execute late but will never be skipped.

Immediate versus late execution in case of long-running tasks may be scheduler-dependent but the guarantee to never skip a task is portable.

Parameters
expression:String

a space-separated list of time fields, following cron expression conventions

Since
6.1.3
See Also
resumeFixedExecution
forLenientExecutionback to summary
public static CronTrigger forLenientExecution(String expression)

Create a CronTrigger for lenient execution, to be rescheduled after every task based on the completion time.

This variant does not make up for missed trigger firings if the associated task has taken too long. As a consequence, original trigger firings may be skipped if the previous task is still running.

This is equivalent to the regular CronTrigger constructor. Note that lenient execution is scheduler-dependent: it may skip trigger firings with long-running tasks on a thread pool while executing at forFixedExecution-like precision with new threads per task.

Parameters
expression:String

a space-separated list of time fields, following cron expression conventions

Since
6.1.3
See Also
resumeLenientExecution
getExpressionback to summary
public String getExpression()

Return the cron pattern that this trigger has been built with.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this 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
nextExecutionback to summary
public Instant nextExecution(TriggerContext triggerContext)

Implements org.springframework.scheduling.Trigger.nextExecution.

Determine the next execution time according to the given trigger context.

Next execution times are calculated based on the completion time of the previous execution; therefore, overlapping executions won't occur.

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
resumeFixedExecutionback to summary
public static CronTrigger resumeFixedExecution(String expression, Instant resumptionTimestamp)

Create a CronTrigger for fixed execution, to be rescheduled after every task based on the last scheduled time.

This variant makes up for missed trigger firings if the associated task has taken too long, scheduling a task for every original trigger firing. Such follow-up tasks may execute late but will never be skipped.

Parameters
expression:String

a space-separated list of time fields, following cron expression conventions

resumptionTimestamp:Instant

the timestamp to resume from (the last-known scheduled timestamp), with every trigger in-between immediately firing to make up for every execution that would have happened in the meantime

Since
6.1.3
See Also
forFixedExecution
resumeLenientExecutionback to summary
public static CronTrigger resumeLenientExecution(String expression, Instant resumptionTimestamp)

Create a CronTrigger for lenient execution, to be rescheduled after every task based on the completion time.

This variant does not make up for missed trigger firings if the associated task has taken too long. As a consequence, original trigger firings may be skipped if the previous task is still running.

Parameters
expression:String

a space-separated list of time fields, following cron expression conventions

resumptionTimestamp:Instant

the timestamp to resume from (the last-known completion timestamp), with the new trigger calculated from there and possibly immediately firing (but only once, every subsequent calculation will start from the completion time of that first resumed trigger)

Since
6.1.3
See Also
forLenientExecution
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override