The ScheduledService is a Service that will automatically restart
itself after a successful execution, and under some conditions will
restart even in case of failure. A new ScheduledService begins in
the READY state, just as a normal Service. After calling
start or restart, the ScheduledService will
enter the SCHEDULED state for the duration specified by delay.
Once RUNNING, the ScheduledService will execute its Task. On successful
completion, the ScheduledService will transition to the SUCCEEDED state,
and then to the READY state and back to the SCHEDULED state. The amount
of time the ScheduledService will remain in this state depends on the
amount of time between the last state transition to RUNNING, and the
current time, and the period. In short, the period
defines the minimum amount of time from the start of one run and the start of
the next. If the previous execution completed before period expires,
then the ScheduledService will remain in the SCHEDULED state until the period
expires. If on the other hand the execution took longer than the
specified period, then the ScheduledService will immediately transition
back to RUNNING.
If, while RUNNING, the ScheduledService's Task throws an error or in
some other way ends up transitioning to FAILED, then the ScheduledService
will either restart or quit, depending on the values for
backoffStrategy, restartOnFailure, and
maximumFailureCount.
If a failure occurs and restartOnFailure is false, then
the ScheduledService will transition to FAILED and will stop. To restart
a failed ScheduledService, you must call restart manually.
If a failure occurs and restartOnFailure is true, then
the the ScheduledService may restart automatically. First,
the result of calling backoffStrategy will become the
new cumulativePeriod. In this way, after each failure, you can cause
the service to wait a longer and longer period of time before restarting.
Once the task completes successfully, the cumulativePeriod is reset to
the value of period.
ScheduledService defines static EXPONENTIAL_BACKOFF_STRATEGY and LOGARITHMIC_BACKOFF_STRATEGY
implementations, of which LOGARITHMIC_BACKOFF_STRATEGY is the default value for
backoffStrategy. After maximumFailureCount is reached, the
ScheduledService will transition to FAILED in exactly the same way as if
restartOnFailure were false.
If the period or delay is changed while the
ScheduledService is running, the new values will be taken into account on the
next iteration. For example, if the period is increased, then the next time the
ScheduledService enters the SCHEDULED state, the new period will be used.
Likewise, if the delay is changed, the new value will be honored on
the next restart or reset/start.
ScheduledService<Document> svc = new ScheduledService<Document>() {
protected Task<Document> createTask() {
return new Task<Document>() {
protected Document call() {
// Connect to a Server
// Get the XML document
// Parse it into a document
return document;
}
};
}
};
svc.setPeriod(Duration.seconds(1));
This example will ping the remote server every 1 second.
Timing for this class is not absolutely reliable. A very busy event thread might introduce some timing lag into the beginning of the execution of the background Task, so very small values for the period or delay are likely to be inaccurate. A delay or period in the hundreds of milliseconds or larger should be fairly reliable.
The ScheduledService in its default configuration has a default period of 0 and a
default delay of 0. This will cause the ScheduledService to execute the task immediately
upon start(), and re-executing immediately upon successful completion.
For this purposes of this class, any Duration that answers true to javafx.
will treat that duration as if it were Duration.ZERO. Likewise, any Duration which answers true
to javafx. will be treated as if it were a duration of Double.MAX_VALUE
milliseconds. Any null Duration is treated as Duration.ZERO. Any custom implementation of a backoff
strategy callback must be prepared to handle these different potential values.
The ScheduledService introduces a new property called lastValue. The
lastValue is the value that was last successfully computed. Because a Service clears its value
property on each run, and because the ScheduledService will reschedule a run immediately after completion (unless it
enters the cancelled or failed states), the value property is not overly useful on a ScheduledService. In most cases
you will want to instead use the value returned by lastValue.
Implementation Note
The ready(), scheduled(), running(), succeeded(),
cancelled(), and failed() methods are implemented in this class. Subclasses which also
override these methods must take care to invoke the super implementation.
| Modifier and Type | Field and Description |
|---|---|
| private ObjectProperty | backoffStrategy
Computes the amount of time to add to the period on each failure. |
| private ReadOnlyObjectWrapper | cumulativePeriod
The current cumulative period in use between iterations. |
| private ReadOnlyIntegerWrapper | currentFailureCount
The current number of times the ScheduledService has failed. |
| private ObjectProperty | delay
The initial delay between when the ScheduledService is first started, and when it will begin operation. |
| private static final Timer | DELAY_TIMER
This Timer is used to schedule the delays for each ScheduledService. |
| private TimerTask | delayTask
This is a TimerTask scheduled with the DELAY_TIMER. |
| public static final Callback | EXPONENTIAL_BACKOFF_STRATEGY
A Callback implementation for the |
| private boolean | freshStart
Whether or not this iteration is a "fresh start", such as the initial call to start, or a call to restart, or a call to reset followed by a call to start. |
| private long | lastRunTime
The timestamp of the last time the task was run. |
| private ReadOnlyObjectWrapper | lastValue
The last successfully computed value. |
| public static final Callback | LINEAR_BACKOFF_STRATEGY
A Callback implementation for the |
| public static final Callback | LOGARITHMIC_BACKOFF_STRATEGY
A Callback implementation for the |
| private ObjectProperty | maximumCumulativePeriod
The maximum allowed value for the cumulativePeriod. |
| private IntegerProperty | maximumFailureCount
The maximum number of times the ScheduledService can fail before it simply ends in the FAILED state. |
| private ObjectProperty | period
The minimum amount of time to allow between the start of the last run and the start of the next run. |
| private BooleanProperty | restartOnFailure
Indicates whether the ScheduledService should automatically restart in the case of a failure in the Task. |
| private boolean | stop
This is set to false when the "cancel" method is called, and reset to true on "reset". |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public final ObjectProperty | |
| public boolean | Returns: whether any running task was cancelled, false if no task was cancelled. In any case, the ScheduledService will stop iterating.Overrides javafx. Implements javafx. Cancels any currently running task and stops this scheduled service, such that no additional iterations will occur. |
| pack-priv long | |
| private TimerTask | Returns: the delay TimerTask.must not be null. task)Creates the TimerTask used for delaying execution. |
| public final ReadOnlyObjectProperty | |
| public final ReadOnlyIntegerProperty | |
| public final ObjectProperty | |
| protected void | executeTask(final Task<V> task)
Overrides javafx.
Uses the |
| private void | executeTaskNow(Task<V>
must not be null task)Called when it is time to actually execute the task (any delay has by now been accounted for). |
| protected void | failed()
Overrides javafx. A protected convenience method for subclasses, called whenever the state of the Service has transitioned to the FAILED state. |
| public final Callback | |
| public final Duration | |
| public final int | |
| public final Duration | |
| public final V | |
| public final Duration | |
| public final int | |
| public final Duration | |
| public final boolean | |
| pack-priv boolean | |
| public final ReadOnlyObjectProperty | |
| public final ObjectProperty | |
| public final IntegerProperty | |
| private static double | |
| public final ObjectProperty | |
| public void | |
| public final BooleanProperty | |
| pack-priv void | |
| public final void | |
| pack-priv void | |
| private void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| protected void | succeeded()
Overrides javafx. A protected convenience method for subclasses, called whenever the state of the Service has transitioned to the SUCCEEDED state. |
| private void | superReset()
Called by this class when we need to avoid calling this class' implementation of reset which has the side effect of resetting the "freshStart", currentFailureCount, and other state. |
| backoffStrategy | back to summary |
|---|---|
| private ObjectProperty<Callback<ScheduledService<?>, Duration>> backoffStrategy Computes the amount of time to add to the period on each failure. This cumulative amount is reset whenever the the ScheduledService is manually restarted. | |
| cumulativePeriod | back to summary |
|---|---|
| private ReadOnlyObjectWrapper<Duration> cumulativePeriod The current cumulative period in use between iterations. This will be the same as | |
| currentFailureCount | back to summary |
|---|---|
| private ReadOnlyIntegerWrapper currentFailureCount The current number of times the ScheduledService has failed. This is reset whenever the ScheduledService is manually restarted. | |
| delay | back to summary |
|---|---|
| private ObjectProperty<Duration> delay The initial delay between when the ScheduledService is first started, and when it will begin
operation. This is the amount of time the ScheduledService will remain in the SCHEDULED state,
before entering the RUNNING state, following a fresh invocation of | |
| DELAY_TIMER | back to summary |
|---|---|
| private static final Timer DELAY_TIMER This Timer is used to schedule the delays for each ScheduledService. A single timer ought to be able to easily service thousands of ScheduledService objects. | |
| delayTask | back to summary |
|---|---|
| private TimerTask delayTask This is a TimerTask scheduled with the DELAY_TIMER. All it does is kick off the execution of the actual background Task. | |
| EXPONENTIAL_BACKOFF_STRATEGY | back to summary |
|---|---|
| public static final Callback<ScheduledService<?>, Duration> EXPONENTIAL_BACKOFF_STRATEGY A Callback implementation for the If the | |
| freshStart | back to summary |
|---|---|
| private boolean freshStart Whether or not this iteration is a "fresh start", such as the initial call to start, or a call to restart, or a call to reset followed by a call to start. | |
| lastRunTime | back to summary |
|---|---|
| private long lastRunTime The timestamp of the last time the task was run. This is used to compute the amount of delay between successive iterations by taking the cumulativePeriod into account. | |
| lastValue | back to summary |
|---|---|
| private ReadOnlyObjectWrapper<V> lastValue The last successfully computed value. During each iteration, the "value" of the ScheduledService will be reset to null, as with any other Service. The "lastValue" however will be set to the most recently successfully computed value, even across iterations. It is reset however whenever you manually call reset or restart. | |
| LINEAR_BACKOFF_STRATEGY | back to summary |
|---|---|
| public static final Callback<ScheduledService<?>, Duration> LINEAR_BACKOFF_STRATEGY A Callback implementation for the If the | |
| LOGARITHMIC_BACKOFF_STRATEGY | back to summary |
|---|---|
| public static final Callback<ScheduledService<?>, Duration> LOGARITHMIC_BACKOFF_STRATEGY A Callback implementation for the If the | |
| maximumCumulativePeriod | back to summary |
|---|---|
| private ObjectProperty<Duration> maximumCumulativePeriod The maximum allowed value for the cumulativePeriod. Setting this value will help ensure that in the case of repeated failures the back-off algorithm doesn't end up producing unreasonably large values for cumulative period. The cumulative period is guaranteed not to be any larger than this value. If the maximumCumulativePeriod is negative, then cumulativePeriod will be capped at 0. If maximumCumulativePeriod is NaN or null, then it will not influence the cumulativePeriod. | |
| maximumFailureCount | back to summary |
|---|---|
| private IntegerProperty maximumFailureCount The maximum number of times the ScheduledService can fail before it simply ends in the FAILED state. You can of course restart the ScheduledService manually, which will cause the current count to be reset. | |
| period | back to summary |
|---|---|
| private ObjectProperty<Duration> period The minimum amount of time to allow between the start of the last run and the start of the next run.
The actual period (also known as | |
| restartOnFailure | back to summary |
|---|---|
| private BooleanProperty restartOnFailure Indicates whether the ScheduledService should automatically restart in the case of a failure in the Task. | |
| stop | back to summary |
|---|---|
| private boolean stop This is set to false when the "cancel" method is called, and reset to true on "reset". We need this so that any time the developer calls 'cancel', even when from within one of the event handlers, it will cause us to transition to the cancelled state. | |
| ScheduledService | back to summary |
|---|---|
| public ScheduledService() Constructor for subclasses to call. | |
| backoffStrategyProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| cancel | back to summary |
|---|---|
| public boolean cancel() Overrides javafx. Implements javafx. Cancels any currently running task and stops this scheduled service, such that no additional iterations will occur.
| |
| clock | back to summary |
|---|---|
| pack-priv long clock() Gets the time of the current clock. At runtime this is simply getting the results of System.currentTimeMillis, however during testing this is hammered so as to return a time that works well during testing.
| |
| createTimerTask | back to summary |
|---|---|
| private TimerTask createTimerTask(final Task<V> task) Creates the TimerTask used for delaying execution. The delay can either be due to the initial delay (if this is a freshStart), or it can be the computed delay in order to execute the task on its fixed schedule. | |
| cumulativePeriodProperty | back to summary |
|---|---|
| public final ReadOnlyObjectProperty | |
| currentFailureCountProperty | back to summary |
|---|---|
| public final ReadOnlyIntegerProperty currentFailureCountProperty() | |
| delayProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| executeTask | back to summary |
|---|---|
| protected void executeTask(final Task<V> task) Overrides javafx. Doc from javafx.
Uses the This method is intended only to be called by the Service implementation.
| |
| executeTaskNow | back to summary |
|---|---|
| private void executeTaskNow(Task<V> task) Called when it is time to actually execute the task (any delay has by now been accounted for). Essentially this ends up simply calling the super implementation of executeTask and doing some bookkeeping.
| |
| failed | back to summary |
|---|---|
| protected void failed() Overrides javafx. Doc from javafx. A protected convenience method for subclasses, called whenever the state of the Service has transitioned to the FAILED state. This method is invoked after the Service has been fully transitioned to the new state. Implementation Note: Subclasses which override this method must call this super implementation.
| |
| getBackoffStrategy | back to summary |
|---|---|
| public final Callback | |
| getCumulativePeriod | back to summary |
|---|---|
| public final Duration getCumulativePeriod() | |
| getCurrentFailureCount | back to summary |
|---|---|
| public final int getCurrentFailureCount() | |
| getDelay | back to summary |
|---|---|
| public final Duration getDelay() | |
| getLastValue | back to summary |
|---|---|
| public final V getLastValue() | |
| getMaximumCumulativePeriod | back to summary |
|---|---|
| public final Duration getMaximumCumulativePeriod() | |
| getMaximumFailureCount | back to summary |
|---|---|
| public final int getMaximumFailureCount() | |
| getPeriod | back to summary |
|---|---|
| public final Duration getPeriod() | |
| getRestartOnFailure | back to summary |
|---|---|
| public final boolean getRestartOnFailure() | |
| isFreshStart | back to summary |
|---|---|
| pack-priv boolean isFreshStart() This method only exists for the sake of testing.
| |
| lastValueProperty | back to summary |
|---|---|
| public final ReadOnlyObjectProperty | |
| maximumCumulativePeriodProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| maximumFailureCountProperty | back to summary |
|---|---|
| public final IntegerProperty maximumFailureCountProperty() | |
| normalize | back to summary |
|---|---|
| private static double normalize(Duration d) Normalize our handling of Durations according to the class documentation.
| |
| periodProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| reset | back to summary |
|---|---|
| public void reset() Overrides javafx. Doc from javafx. Resets the Service. May only be called while in one of the finish states, that is, SUCCEEDED, FAILED, or CANCELLED, or when READY. This method should only be called on the FX application thread. Implementation Note: Subclasses which override this method must call this super implementation.
| |
| restartOnFailureProperty | back to summary |
|---|---|
| public final BooleanProperty restartOnFailureProperty() | |
| schedule | back to summary |
|---|---|
| pack-priv void schedule(TimerTask task, long delay) This method exists only for testing purposes. The normal implementation will delegate to a java.util.Timer, however during testing we want to simply inspect the value for the delay and execute immediately.
| |
| setBackoffStrategy | back to summary |
|---|---|
| public final void setBackoffStrategy(Callback<ScheduledService<?>, Duration> value) | |
| setCumulativePeriod | back to summary |
|---|---|
| pack-priv void setCumulativePeriod(Duration value) | |
| setCurrentFailureCount | back to summary |
|---|---|
| private void setCurrentFailureCount(int value) | |
| setDelay | back to summary |
|---|---|
| public final void setDelay(Duration value) | |
| setMaximumCumulativePeriod | back to summary |
|---|---|
| public final void setMaximumCumulativePeriod(Duration value) | |
| setMaximumFailureCount | back to summary |
|---|---|
| public final void setMaximumFailureCount(int value) | |
| setPeriod | back to summary |
|---|---|
| public final void setPeriod(Duration value) | |
| setRestartOnFailure | back to summary |
|---|---|
| public final void setRestartOnFailure(boolean value) | |
| succeeded | back to summary |
|---|---|
| protected void succeeded() Overrides javafx. Doc from javafx. A protected convenience method for subclasses, called whenever the state of the Service has transitioned to the SUCCEEDED state. This method is invoked after the Service has been fully transitioned to the new state. Implementation Note: Subclasses which override this method must call this super implementation.
| |
| superReset | back to summary |
|---|---|
| private void superReset() Called by this class when we need to avoid calling this class' implementation of reset which has the side effect of resetting the "freshStart", currentFailureCount, and other state. | |