Class Polling
call()
method.
Can be used with lambda expressions, using the constructor Polling(Callable c)
.- Since:
- 1.1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionOptional object to be used by the default implementation of call()List of all the exceptions thrown by call(), to be used for loggingprotected Exception
Holder for the last exception thrown by call(), to be used for loggingprotected long
Counter for total waiting time -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncall()
Method to be called bypoll(long timeout, long delay)
, potentially multiple times, until it returns true or timeout is reached.
Subclasses can override it to change the check accordingly.Return the list of all exceptions while pollingReturn the last exception while polling or {null}long
protected String
message()
Returns the string to be used in theTimeoutException
, if needed.void
poll
(long timeout, long delay) Tries to executecall()
until it returns true or untiltimeout
is reached.
-
Field Details
-
c
Optional object to be used by the default implementation of call() -
lastException
Holder for the last exception thrown by call(), to be used for logging -
exceptions
List of all the exceptions thrown by call(), to be used for logging -
waited
protected long waitedCounter for total waiting time
-
-
Constructor Details
-
Polling
public Polling()Default constructor to be used in subclasses that override thecall()
method. Should not be used directly onPolling
instances, but only on extended classes. If used directly to get aPolling
instance, executingpoll(long timeout, long delay)
will be equivalent toThread.sleep(timeout)
-
Polling
Creates a new instance that uses theCallable
parameter for polling- Parameters:
c
- object whosecall()
method will be polled
-
-
Method Details
-
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. The method should return true only when the call was successful.
It can return false or throw anyException
to make the poller try again later.The default implementation delegates the call to the
Callable c
instance. -
poll
Tries to execute
call()
until it returns true or untiltimeout
is reached. Between retries, it waits usingThread.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"- Parameters:
timeout
- max total execution time, in millisecondsdelay
- time to wait between calls, in milliseconds- Throws:
TimeoutException
- iftimeout
was reachedInterruptedException
- if the thread was interrupted while sleeping; caller should throw it further
-
getWaited
public long getWaited() -
message
Returns the string to be used in theTimeoutException
, if needed. The string is passed toString.format(message(), timeout, delay)
, so it can be a format including%1$d
and%2$d
. The fieldlastException
is also available for logging- Returns:
- the format string
-
getLastException
Return the last exception while polling or {null}- Returns:
- the last exception (can be null)
-
getExceptions
Return the list of all exceptions while polling- Returns:
- the non-null list of exceptions
-