Package io.smallrye.faulttolerance.api
Interface FaultTolerance.Builder<T,R>
- Type Parameters:
T
- type of value of the guarded actionR
- type of result of this builder, depends on how this builder was created
- Enclosing interface:
FaultTolerance<T>
public static interface FaultTolerance.Builder<T,R>
A builder for configuring fault tolerance strategies. A fault tolerance strategy is included in the resulting
set if the corresponding
with[Strategy]
method is called. Each strategy has its own builder to configure
the necessary attributes, and each such builder has a done()
method that returns back to this builder.
In general, all builders in this API accept multiple invocations of the same method, but only the last invocation is meaningful. Any previous invocations are forgotten.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
Configures a bulkhead.static interface
Configures a circuit breaker.static interface
Configures a fallback.static interface
Configures a rate limit.static interface
Configures a retry.static interface
Configures a timeout. -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Returns a ready-to-use instance ofFaultTolerance
or guardedCallable
, depending on how this builder was created.default FaultTolerance.Builder
<T, R> with
(Consumer<FaultTolerance.Builder<T, R>> consumer) Syntactic sugar for calling the builder methods conditionally without breaking the invocation chain.Adds a bulkhead strategy.Adds a circuit breaker strategy.withDescription
(String value) Assigns a description to the resulting set of configured fault tolerance strategies.Adds a fallback strategy.Adds a rate limit strategy.Adds a retry strategy.withThreadOffload
(boolean value) Configures whether the guarded action should be offloaded to another thread.withThreadOffloadExecutor
(Executor executor) Configures the executor to use when offloading the guarded action to another thread.Adds a timeout strategy.
-
Method Details
-
withDescription
Assigns a description to the resulting set of configured fault tolerance strategies. The description is used in logging messages and exception messages, and also as an identifier for metrics .The description may be an arbitrary string. Duplicates are permitted.
If no description is set, a random UUID is used.
- Parameters:
value
- a description, must not benull
- Returns:
- this fault tolerance builder
-
withBulkhead
FaultTolerance.Builder.BulkheadBuilder<T,R> withBulkhead()Adds a bulkhead strategy. In this API, bulkhead is a simple concurrency limiter.- Returns:
- a builder to configure the bulkhead strategy
- See Also:
-
withCircuitBreaker
FaultTolerance.Builder.CircuitBreakerBuilder<T,R> withCircuitBreaker()Adds a circuit breaker strategy.- Returns:
- a builder to configure the circuit breaker strategy
- See Also:
-
withFallback
FaultTolerance.Builder.FallbackBuilder<T,R> withFallback()Adds a fallback strategy.- Returns:
- a builder to configure the fallback strategy
- See Also:
-
withRateLimit
FaultTolerance.Builder.RateLimitBuilder<T,R> withRateLimit()Adds a rate limit strategy.- Returns:
- a builder to configure the rate limit strategy
- See Also:
-
withRetry
FaultTolerance.Builder.RetryBuilder<T,R> withRetry()Adds a retry strategy. Retry uses constant backoff between attempts by default, but may be configured to use exponential backoff, Fibonacci backoff, or custom backoff.- Returns:
- a builder to configure the retry strategy
- See Also:
-
withTimeout
FaultTolerance.Builder.TimeoutBuilder<T,R> withTimeout()Adds a timeout strategy.- Returns:
- a builder to configure the timeout strategy
- See Also:
-
withThreadOffload
Configures whether the guarded action should be offloaded to another thread. Thread offload is only possible for asynchronous actions. If this builder was not created usingcreateAsync
, attempting to enable thread offload throws an exception.- Parameters:
value
- whether the guarded action should be offloaded to another thread- Returns:
- this fault tolerance builder
- See Also:
-
withThreadOffloadExecutor
Configures the executor to use when offloading the guarded action to another thread. Thread offload is only possible for asynchronous actions. If this builder was not created usingcreateAsync
, this method throws an exception.If this method is not called, the guarded action is offloaded to the default executor provided by the integrator.
- Parameters:
executor
- the executor to which the guarded action should be offloaded- Returns:
- this fault tolerance builder
-
build
R build()Returns a ready-to-use instance ofFaultTolerance
or guardedCallable
, depending on how this builder was created. -
with
Syntactic sugar for calling the builder methods conditionally without breaking the invocation chain. For example:FaultTolerance.create(this::doSomething) .withFallback() ... .done() .with(builder -> { if (useTimeout) { builder.withTimeout() ... .done(); } }) .build();
- Parameters:
consumer
- block of code to execute with this builder- Returns:
- this fault tolerance builder
-