Record Class Timeout<T>
java.lang.Object
java.lang.Record
io.github.teceli.resiliencia.patterns.timeout.Timeout<T>
- All Implemented Interfaces:
Resilient<T>
public record Timeout<T>(String name, Duration timeout, boolean cancelOnTimeout, List<ResilienceEvent.Listener> listeners, Clock clock)
extends Record
implements Resilient<T>
Timeout pattern: execute an operation on a virtual thread and bound how long the caller
waits for it. When the deadline passes and
cancelOnTimeout is true (the default), the
worker thread is interrupted — a real cancellation signal, not polling. Either way the caller
gets a ResilientTimeoutException (or Outcome.TimedOut via outcome(io.github.teceli.resiliencia.core.api.Resilient.Operation<T>))
immediately once the deadline passes. Whether the operation actually stops depends on it
responding to interruption; the caller is unblocked either way.
The deadline is enforced against real elapsed time; the Clock is used only for
event timestamps, so a manual clock in tests affects observability data, not the deadline.
Immutable and reusable: each withX method returns a new, independently usable
Timeout instance rather than mutating this one.-
Nested Class Summary
Nested classes/interfaces inherited from interface io.github.teceli.resiliencia.core.api.Resilient
Resilient.Operation<T> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncall(Resilient.Operation<T> operation) Execute an operation with resilience guarantees.booleanReturns the value of thecancelOnTimeoutrecord component.clock()Returns the value of theclockrecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.Returns the value of thelistenersrecord component.name()Returns the value of thenamerecord component.static <T> Timeout<T> ATimeoutwith the given deadline, ready to use as-is or refine further viawithXmethods.outcome(Resilient.Operation<T> operation) Execute an operation and capture the result or failure as an Outcome.The kind of this pattern, used for internal comparisons (e.g.The name of this pattern, e.g.timeout()Returns the value of thetimeoutrecord component.final StringtoString()Returns a string representation of this record class.withCancelOnTimeout(boolean cancelOnTimeout) Whether the operation's virtual thread is interrupted when the deadline passes.Use a customClockinstead of the system clock for event timestamps.withListener(ResilienceEvent.Listener listener) Add a listener notified of everyTimeoutEventemitted by this instance.withTimeout(Duration timeout) How long the caller waits before the operation is considered timed out.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface io.github.teceli.resiliencia.core.api.Resilient
callAsync, hasOwnDeadline
-
Constructor Details
-
Timeout
public Timeout(String name, Duration timeout, boolean cancelOnTimeout, List<ResilienceEvent.Listener> listeners, Clock clock) Creates an instance of aTimeoutrecord class.- Parameters:
name- the value for thenamerecord componenttimeout- the value for thetimeoutrecord componentcancelOnTimeout- the value for thecancelOnTimeoutrecord componentlisteners- the value for thelistenersrecord componentclock- the value for theclockrecord component
-
-
Method Details
-
of
ATimeoutwith the given deadline, ready to use as-is or refine further viawithXmethods. There is no default duration: a timeout is always an explicit business decision.- Parameters:
name- identifier used in everyTimeoutEventemitted by this instance. Not enforced unique across instances — there is no global registry to check against.
-
withTimeout
How long the caller waits before the operation is considered timed out. Must be positive. -
withCancelOnTimeout
Whether the operation's virtual thread is interrupted when the deadline passes. Default: true. Set to false to let the operation finish naturally in the background — the caller still receives the timeout exception immediately either way — useful when the operation holds resources that must be released cleanly rather than abandoned mid-interruption. -
withListener
Add a listener notified of everyTimeoutEventemitted by this instance. Listener exceptions are logged and otherwise ignored — a broken listener never affects the outcome. -
withClock
Use a customClockinstead of the system clock for event timestamps. -
patternName
Description copied from interface:ResilientThe name of this pattern, e.g. "retry", "timeout", "circuit-breaker". Used for identification (e.g. by Policy) without coupling to concrete pattern types. Defaults to "custom" for user-defined Resilient implementations.- Specified by:
patternNamein interfaceResilient<T>
-
patternKind
Description copied from interface:ResilientThe kind of this pattern, used for internal comparisons (e.g. Policy order validation). UnlikeResilient.patternName(), which is a free-form observability label, this is a closed enum the library can reason about exhaustively. Defaults toPatternKind.CUSTOMfor user-defined Resilient implementations.- Specified by:
patternKindin interfaceResilient<T>
-
call
Description copied from interface:ResilientExecute an operation with resilience guarantees. May throw ResilienciaException or a specific pattern exception.- Specified by:
callin interfaceResilient<T>- Throws:
ResilientException
-
outcome
Description copied from interface:ResilientExecute an operation and capture the result or failure as an Outcome. Never throws for a recordedException— always returns Success, Failure, or a pattern-specific outcome. AnErrorthrown by the operation propagates uncaught instead of being captured as a Failure: fatal JVM conditions (e.g.OutOfMemoryError) should not be treated as a recoverable result. -
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with '=='. -
name
Returns the value of thenamerecord component.- Returns:
- the value of the
namerecord component
-
timeout
Returns the value of thetimeoutrecord component.- Returns:
- the value of the
timeoutrecord component
-
cancelOnTimeout
public boolean cancelOnTimeout()Returns the value of thecancelOnTimeoutrecord component.- Returns:
- the value of the
cancelOnTimeoutrecord component
-
listeners
Returns the value of thelistenersrecord component.- Returns:
- the value of the
listenersrecord component
-
clock
Returns the value of theclockrecord component.- Returns:
- the value of the
clockrecord component
-