001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.scheduling.support; 018 019import java.util.Date; 020import java.util.concurrent.TimeUnit; 021 022import org.springframework.lang.Nullable; 023import org.springframework.scheduling.Trigger; 024import org.springframework.scheduling.TriggerContext; 025import org.springframework.util.Assert; 026 027/** 028 * A trigger for periodic task execution. The period may be applied as either 029 * fixed-rate or fixed-delay, and an initial delay value may also be configured. 030 * The default initial delay is 0, and the default behavior is fixed-delay 031 * (i.e. the interval between successive executions is measured from each 032 * <i>completion</i> time). To measure the interval between the 033 * scheduled <i>start</i> time of each execution instead, set the 034 * 'fixedRate' property to {@code true}. 035 * 036 * <p>Note that the TaskScheduler interface already defines methods for scheduling 037 * tasks at fixed-rate or with fixed-delay. Both also support an optional value 038 * for the initial delay. Those methods should be used directly whenever 039 * possible. The value of this Trigger implementation is that it can be used 040 * within components that rely on the Trigger abstraction. For example, it may 041 * be convenient to allow periodic triggers, cron-based triggers, and even 042 * custom Trigger implementations to be used interchangeably. 043 * 044 * @author Mark Fisher 045 * @since 3.0 046 */ 047public class PeriodicTrigger implements Trigger { 048 049 private final long period; 050 051 private final TimeUnit timeUnit; 052 053 private volatile long initialDelay = 0; 054 055 private volatile boolean fixedRate = false; 056 057 058 /** 059 * Create a trigger with the given period in milliseconds. 060 */ 061 public PeriodicTrigger(long period) { 062 this(period, null); 063 } 064 065 /** 066 * Create a trigger with the given period and time unit. The time unit will 067 * apply not only to the period but also to any 'initialDelay' value, if 068 * configured on this Trigger later via {@link #setInitialDelay(long)}. 069 */ 070 public PeriodicTrigger(long period, @Nullable TimeUnit timeUnit) { 071 Assert.isTrue(period >= 0, "period must not be negative"); 072 this.timeUnit = (timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS); 073 this.period = this.timeUnit.toMillis(period); 074 } 075 076 077 /** 078 * Return this trigger's period. 079 * @since 5.0.2 080 */ 081 public long getPeriod() { 082 return this.period; 083 } 084 085 /** 086 * Return this trigger's time unit (milliseconds by default). 087 * @since 5.0.2 088 */ 089 public TimeUnit getTimeUnit() { 090 return this.timeUnit; 091 } 092 093 /** 094 * Specify the delay for the initial execution. It will be evaluated in 095 * terms of this trigger's {@link TimeUnit}. If no time unit was explicitly 096 * provided upon instantiation, the default is milliseconds. 097 */ 098 public void setInitialDelay(long initialDelay) { 099 this.initialDelay = this.timeUnit.toMillis(initialDelay); 100 } 101 102 /** 103 * Return the initial delay, or 0 if none. 104 * @since 5.0.2 105 */ 106 public long getInitialDelay() { 107 return this.initialDelay; 108 } 109 110 /** 111 * Specify whether the periodic interval should be measured between the 112 * scheduled start times rather than between actual completion times. 113 * The latter, "fixed delay" behavior, is the default. 114 */ 115 public void setFixedRate(boolean fixedRate) { 116 this.fixedRate = fixedRate; 117 } 118 119 /** 120 * Return whether this trigger uses fixed rate ({@code true}) or 121 * fixed delay ({@code false}) behavior. 122 * @since 5.0.2 123 */ 124 public boolean isFixedRate() { 125 return this.fixedRate; 126 } 127 128 129 /** 130 * Returns the time after which a task should run again. 131 */ 132 @Override 133 public Date nextExecutionTime(TriggerContext triggerContext) { 134 Date lastExecution = triggerContext.lastScheduledExecutionTime(); 135 Date lastCompletion = triggerContext.lastCompletionTime(); 136 if (lastExecution == null || lastCompletion == null) { 137 return new Date(System.currentTimeMillis() + this.initialDelay); 138 } 139 if (this.fixedRate) { 140 return new Date(lastExecution.getTime() + this.period); 141 } 142 return new Date(lastCompletion.getTime() + this.period); 143 } 144 145 146 @Override 147 public boolean equals(@Nullable Object other) { 148 if (this == other) { 149 return true; 150 } 151 if (!(other instanceof PeriodicTrigger)) { 152 return false; 153 } 154 PeriodicTrigger otherTrigger = (PeriodicTrigger) other; 155 return (this.fixedRate == otherTrigger.fixedRate && this.initialDelay == otherTrigger.initialDelay && 156 this.period == otherTrigger.period); 157 } 158 159 @Override 160 public int hashCode() { 161 return (this.fixedRate ? 17 : 29) + (int) (37 * this.period) + (int) (41 * this.initialDelay); 162 } 163 164}