Interface CircuitBreaker


  • public interface CircuitBreaker
    An implementation of the circuit breaker pattern for Vert.x
    Author:
    Clement Escoffier
    • Method Detail

      • create

        static CircuitBreaker create​(String name,
                                     Vertx vertx)
        Creates a new instance of CircuitBreaker, with default options.
        Parameters:
        name - the name
        vertx - the Vert.x instance
        Returns:
        the created instance
      • close

        CircuitBreaker close()
        Closes the circuit breaker. It stops sending events on its state on the event bus.

        This method is not related to the closed state of the circuit breaker. To move the circuit breaker to the closed state, use reset().

      • halfOpenHandler

        CircuitBreaker halfOpenHandler​(Handler<Void> handler)
        Sets a Handler to be invoked when the circuit breaker state switches to half-open.
        Parameters:
        handler - the handler, must not be null
        Returns:
        this CircuitBreaker
      • closeHandler

        CircuitBreaker closeHandler​(Handler<Void> handler)
        Sets a Handler to be invoked when the circuit breaker state switches to closed.
        Parameters:
        handler - the handler, must not be null
        Returns:
        this CircuitBreaker
      • executeWithFallback

        <T> Future<T> executeWithFallback​(Handler<Promise<T>> command,
                                          java.util.function.Function<Throwable,​T> fallback)
        Executes the given operation with the circuit breaker control. The operation is generally calling an external system. The operation receives a Promise object as parameter and must call Promise.complete(Object) when the operation has terminated successfully. The operation must also call Promise.fail(Throwable) in case of a failure.

        The operation is not invoked if the circuit breaker is open, and the given fallback is called instead. The circuit breaker also monitors whether the operation completes in time. The operation is considered failed if it does not terminate before the configured timeout.

        This method returns a Future object to retrieve the status and result of the operation, with the status being a success or a failure. If the fallback is called, the returned future is successfully completed with the value returned from the fallback. If the fallback throws an exception, the returned future is marked as failed.

        Type Parameters:
        T - the type of result
        Parameters:
        command - the operation
        fallback - the fallback function; gets an exception as parameter and returns the fallback result
        Returns:
        a future object completed when the operation or the fallback completes
      • executeWithFallback

        <T> Future<T> executeWithFallback​(java.util.function.Supplier<Future<T>> command,
                                          java.util.function.Function<Throwable,​T> fallback)
        Executes the given operation with the circuit breaker control. The operation is generally calling an external system. The operation receives a Promise object as parameter and must call Promise.complete(Object) when the operation has terminated successfully. The operation must also call Promise.fail(Throwable) in case of a failure.

        The operation is not invoked if the circuit breaker is open, and the given fallback is called instead. The circuit breaker also monitors whether the operation completes in time. The operation is considered failed if it does not terminate before the configured timeout.

        This method returns a Future object to retrieve the status and result of the operation, with the status being a success or a failure. If the fallback is called, the returned future is successfully completed with the value returned from the fallback. If the fallback throws an exception, the returned future is marked as failed.

        Type Parameters:
        T - the type of result
        Parameters:
        command - the operation
        fallback - the fallback function; gets an exception as parameter and returns the fallback result
        Returns:
        a future object completed when the operation or the fallback completes
      • execute

        <T> Future<T> execute​(java.util.function.Supplier<Future<T>> command)
        Same as executeWithFallback(Supplier, Function) but using the circuit breaker default fallback.
        Type Parameters:
        T - the type of result
        Parameters:
        command - the operation
        Returns:
        a future object completed when the operation or its fallback completes
      • executeAndReportWithFallback

        <T> CircuitBreaker executeAndReportWithFallback​(Promise<T> resultPromise,
                                                        Handler<Promise<T>> command,
                                                        java.util.function.Function<Throwable,​T> fallback)
        Executes the given operation with the circuit breaker control. The operation is generally calling an external system. The operation receives a Promise object as parameter and must call Promise.complete(Object) when the operation has terminated successfully. The operation must also call Promise.fail(Throwable) in case of a failure.

        The operation is not invoked if the circuit breaker is open, and the given fallback is called instead. The circuit breaker also monitors whether the operation completes in time. The operation is considered failed if it does not terminate before the configured timeout.

        Unlike executeWithFallback(Handler, Function), this method does not return a Future object, but lets the caller pass a Promise object on which the result is reported. If the fallback is called, the promise is successfully completed with the value returned by the fallback function. If the fallback throws an exception, the promise is marked as failed.

        Type Parameters:
        T - the type of result
        Parameters:
        resultPromise - the promise on which the operation result is reported
        command - the operation
        fallback - the fallback function; gets an exception as parameter and returns the fallback result
        Returns:
        this CircuitBreaker
      • fallback

        <T> CircuitBreaker fallback​(java.util.function.Function<Throwable,​T> handler)
        Sets a default fallback Function to be invoked when the circuit breaker is open or when failure occurs and CircuitBreakerOptions.isFallbackOnFailure() is enabled.

        The function gets the exception as parameter and returns the fallback result.

        Parameters:
        handler - the fallback handler
        Returns:
        this CircuitBreaker
      • reset

        CircuitBreaker reset()
        Resets the circuit breaker state. The number of recent failures is set to 0 and if the state is half-open, it is set to closed.
        Returns:
        this CircuitBreaker
      • failureCount

        long failureCount()
        Returns:
        the current number of recorded failures
      • name

        String name()
        Returns:
        the name of this circuit breaker