In this short article, we are going to present how to use the Timeout policy available in the Failsafe library. Timeouts allow you to fail an execution with TimeoutExceededException if it takes too long to complete.
2. Basic use
To start using Timeout first we need to create an instance of this object like in the following code:
then we can use it in Failsafe.with(...) method to handle timeout on specific execution:
3. Event listeners
Timeouts support the standard policy listeners which can notify you when a timeout is exceeded:
Or when an execution completes and the timeout is not exceeded:
4. JUnit test
In the following JUnit test we created a Timeout object that should interrupt execution after 10 seconds:
In this article, we presented how to use the Timeout policy in the Failsafe library. This is the simplest way to interrupt the execution of any code when if it takes too long to complete.