public class Polling extends Object implements Callable<Boolean>
call()
method.
Can be used with lambda expressions, using the constructor Polling(Callable c)
.Modifier and Type | Field and Description |
---|---|
protected Callable<Boolean> |
c
Optional object to be used by the default implementation of call()
|
protected Exception |
lastException
Holder for the last exception thrown by call(), to be used for logging
|
protected long |
waited
Counter for total waiting time
|
Constructor and Description |
---|
Polling()
Default constructor to be used in subclasses that override the
call() method. |
Polling(Callable<Boolean> c)
Creates a new instance that uses the
Callable parameter for polling |
Modifier and Type | Method and Description |
---|---|
Boolean |
call()
Method to be called by
poll(long timeout, long delay) , potentially multiple times,
until it returns true or timeout is reached.Subclasses can override it to change the check accordingly. |
long |
getWaited() |
protected String |
message()
Returns the string to be used in the
TimeoutException , if needed. |
void |
poll(long timeout,
long delay)
Tries to execute
call() until it returns true or until timeout is reached. |
protected final Callable<Boolean> c
protected Exception lastException
protected long waited
public Polling()
call()
method.
Should not be used directly on Polling
instances, but only on extended classes.
If used directly to get a Polling
instance, executing poll(long timeout, long delay)
will be equivalent to Thread.sleep(timeout)
public Boolean call() throws Exception
Method to be called by poll(long timeout, long delay)
, potentially multiple times,
until it returns true or timeout is reached.
Subclasses can override it to change the check accordingly. The method should return true
only when the call was successful.
It can return false or throw any Exception
to make the poller try again later.
The default implementation delegates the call to the Callable c
instance.
public void poll(long timeout, long delay) throws TimeoutException, InterruptedException
Tries to execute call()
until it returns true or until timeout
is reached.
Between retries, it waits using Thread.sleep(delay)
. It means the retry is not at a fixed pace,
but depends on the execution time of the call itself.
The method guarantees that the call() will be executed at least once. If the timeout is 0 or less, then call() will be executed exactly once.
The timeout is adjusted using TimeoutsProvider
so the final value can be changed using the
system property: "sling.testing.timeout.multiplier"
timeout
- max total execution time, in millisecondsdelay
- time to wait between calls, in millisecondsTimeoutException
- if timeout
was reachedInterruptedException
- if the thread was interrupted while sleeping; caller should throw it furtherpublic long getWaited()
protected String message()
TimeoutException
, if needed.
The string is passed to String.format(message(), timeout, delay)
, so it can be a format
including %1$d
and %2$d
. The field lastException
is also available for loggingCopyright © 2007–2018 The Apache Software Foundation. All rights reserved.