Class Polling
- java.lang.Object
-
- org.apache.sling.testing.clients.util.poller.Polling
-
- All Implemented Interfaces:
java.util.concurrent.Callable<java.lang.Boolean>
public class Polling extends java.lang.Object implements java.util.concurrent.Callable<java.lang.Boolean>Helper for repeating a call until it returns true, with timeout capabilities. Subclasses should override thecall()method. Can be used with lambda expressions, using the constructorPolling(Callable c).- Since:
- 1.1.0
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.concurrent.Callable<java.lang.Boolean>cOptional object to be used by the default implementation of call()protected java.util.List<java.lang.Exception>exceptionsList of all the exceptions thrown by call(), to be used for loggingprotected java.lang.ExceptionlastExceptionHolder for the last exception thrown by call(), to be used for loggingprotected longwaitedCounter for total waiting time
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Booleancall()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.java.util.List<java.lang.Exception>getExceptions()Return the list of all exceptions while pollingjava.lang.ExceptiongetLastException()Return the last exception while polling or {null}longgetWaited()protected java.lang.Stringmessage()Returns the string to be used in theTimeoutException, if needed.voidpoll(long timeout, long delay)Tries to executecall()until it returns true or untiltimeoutis reached.
-
-
-
Field Detail
-
c
protected final java.util.concurrent.Callable<java.lang.Boolean> c
Optional object to be used by the default implementation of call()
-
lastException
protected java.lang.Exception lastException
Holder for the last exception thrown by call(), to be used for logging
-
exceptions
protected java.util.List<java.lang.Exception> exceptions
List of all the exceptions thrown by call(), to be used for logging
-
waited
protected long waited
Counter for total waiting time
-
-
Constructor Detail
-
Polling
public Polling()
Default constructor to be used in subclasses that override thecall()method. Should not be used directly onPollinginstances, but only on extended classes. If used directly to get aPollinginstance, executingpoll(long timeout, long delay)will be equivalent toThread.sleep(timeout)
-
Polling
public Polling(java.util.concurrent.Callable<java.lang.Boolean> c)
Creates a new instance that uses theCallableparameter for polling- Parameters:
c- object whosecall()method will be polled
-
-
Method Detail
-
call
public java.lang.Boolean call() throws java.lang.ExceptionMethod 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 anyExceptionto make the poller try again later.The default implementation delegates the call to the
Callable cinstance.- Specified by:
callin interfacejava.util.concurrent.Callable<java.lang.Boolean>- Returns:
trueto end polling- Throws:
java.lang.Exception- if unable to compute a result
-
poll
public void poll(long timeout, long delay) throws java.util.concurrent.TimeoutException, java.lang.InterruptedExceptionTries to execute
call()until it returns true or untiltimeoutis 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
TimeoutsProviderso 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:
java.util.concurrent.TimeoutException- iftimeoutwas reachedjava.lang.InterruptedException- if the thread was interrupted while sleeping; caller should throw it further
-
getWaited
public long getWaited()
-
message
protected java.lang.String 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$dand%2$d. The fieldlastExceptionis also available for logging- Returns:
- the format string
-
getLastException
public java.lang.Exception getLastException()
Return the last exception while polling or {null}- Returns:
-
getExceptions
public java.util.List<java.lang.Exception> getExceptions()
Return the list of all exceptions while polling- Returns:
-
-