io.vertx.rxjava.circuitbreaker.CircuitBreaker Maven / Gradle / Ivy
/*
* Copyright 2014 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.vertx.rxjava.circuitbreaker;
import rx.Observable;
import rx.Single;
import io.vertx.rx.java.RxHelper;
import io.vertx.rx.java.WriteStreamSubscriber;
import io.vertx.rx.java.SingleOnSubscribeAdapter;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Collectors;
import io.vertx.core.Handler;
import io.vertx.core.AsyncResult;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.lang.rx.RxGen;
import io.vertx.lang.rx.TypeArg;
import io.vertx.lang.rx.MappingIterator;
/**
* An implementation of the circuit breaker pattern for Vert.x
*
*
* NOTE: This class has been automatically generated from the {@link io.vertx.circuitbreaker.CircuitBreaker original} non RX-ified interface using Vert.x codegen.
*/
@RxGen(io.vertx.circuitbreaker.CircuitBreaker.class)
public class CircuitBreaker {
@Override
public String toString() {
return delegate.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CircuitBreaker that = (CircuitBreaker) o;
return delegate.equals(that.delegate);
}
@Override
public int hashCode() {
return delegate.hashCode();
}
public static final TypeArg __TYPE_ARG = new TypeArg<>( obj -> new CircuitBreaker((io.vertx.circuitbreaker.CircuitBreaker) obj),
CircuitBreaker::getDelegate
);
private final io.vertx.circuitbreaker.CircuitBreaker delegate;
public CircuitBreaker(io.vertx.circuitbreaker.CircuitBreaker delegate) {
this.delegate = delegate;
}
public CircuitBreaker(Object delegate) {
this.delegate = (io.vertx.circuitbreaker.CircuitBreaker)delegate;
}
public io.vertx.circuitbreaker.CircuitBreaker getDelegate() {
return delegate;
}
/**
* Creates a new instance of {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker}.
* @param name the name
* @param vertx the Vert.x instance
* @param options the configuration option
* @return the created instance
*/
public static io.vertx.rxjava.circuitbreaker.CircuitBreaker create(String name, io.vertx.rxjava.core.Vertx vertx, io.vertx.circuitbreaker.CircuitBreakerOptions options) {
io.vertx.rxjava.circuitbreaker.CircuitBreaker ret = io.vertx.rxjava.circuitbreaker.CircuitBreaker.newInstance((io.vertx.circuitbreaker.CircuitBreaker)io.vertx.circuitbreaker.CircuitBreaker.create(name, vertx.getDelegate(), options));
return ret;
}
/**
* Creates a new instance of {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker}, with default options.
* @param name the name
* @param vertx the Vert.x instance
* @return the created instance
*/
public static io.vertx.rxjava.circuitbreaker.CircuitBreaker create(String name, io.vertx.rxjava.core.Vertx vertx) {
io.vertx.rxjava.circuitbreaker.CircuitBreaker ret = io.vertx.rxjava.circuitbreaker.CircuitBreaker.newInstance((io.vertx.circuitbreaker.CircuitBreaker)io.vertx.circuitbreaker.CircuitBreaker.create(name, vertx.getDelegate()));
return ret;
}
/**
* Closes the circuit breaker. It stops sending events on its state on the event bus.
* This method is not related to the close
state of the circuit breaker. To set the circuit breaker in the
* close
state, use {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker#reset}.
* @return
*/
public io.vertx.rxjava.circuitbreaker.CircuitBreaker close() {
delegate.close();
return this;
}
/**
* Sets a invoked when the circuit breaker state switches to open.
* @param handler the handler, must not be null
* @return the current {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker}
*/
public io.vertx.rxjava.circuitbreaker.CircuitBreaker openHandler(Handler handler) {
delegate.openHandler(handler);
return this;
}
/**
* Sets a invoked when the circuit breaker state switches to half-open.
* @param handler the handler, must not be null
* @return the current {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker}
*/
public io.vertx.rxjava.circuitbreaker.CircuitBreaker halfOpenHandler(Handler handler) {
delegate.halfOpenHandler(handler);
return this;
}
/**
* Sets a invoked when the circuit breaker state switches to close.
* @param handler the handler, must not be null
* @return the current {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker}
*/
public io.vertx.rxjava.circuitbreaker.CircuitBreaker closeHandler(Handler handler) {
delegate.closeHandler(handler);
return this;
}
/**
* Same as {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker#executeWithFallback} but using a callback.
* @param command the operation
* @param fallback the fallback
* @param handler the completion handler receiving either the operation result or the fallback result. The parameter is an because if the fallback is not called, the error is passed to the handler.
*/
public void executeWithFallback(Handler> command, Function fallback, Handler> handler) {
delegate.executeWithFallback(new Handler>() {
public void handle(io.vertx.core.Promise event) {
command.handle(io.vertx.rxjava.core.Promise.newInstance((io.vertx.core.Promise)event, TypeArg.unknown()));
}
}, fallback, handler);
}
/**
* Same as {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker#executeWithFallback} but using a callback.
* @param command the operation
* @param fallback the fallback
*/
public void executeWithFallback(Handler> command, Function fallback) {
executeWithFallback(command, fallback, ar -> { });
}
/**
* Same as {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker#executeWithFallback} but using a callback.
* @param command the operation
* @param fallback the fallback
* @return
*/
public Single rxExecuteWithFallback(Handler> command, Function fallback) {
return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
executeWithFallback(command, fallback, fut);
}));
}
/**
* Same as {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker#executeWithFallback} but using the circuit breaker default fallback.
* @param command the operation
* @param handler the completion handler receiving either the operation result or the fallback result. The parameter is an because if the fallback is not called, the error is passed to the handler.
*/
public void execute(Handler> command, Handler> handler) {
delegate.execute(new Handler>() {
public void handle(io.vertx.core.Promise event) {
command.handle(io.vertx.rxjava.core.Promise.newInstance((io.vertx.core.Promise)event, TypeArg.unknown()));
}
}, handler);
}
/**
* Same as {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker#executeWithFallback} but using the circuit breaker default fallback.
* @param command the operation
*/
public void execute(Handler> command) {
execute(command, ar -> { });
}
/**
* Same as {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker#executeWithFallback} but using the circuit breaker default fallback.
* @param command the operation
* @return
*/
public Single rxExecute(Handler> command) {
return Single.create(new SingleOnSubscribeAdapter<>(fut -> {
execute(command, fut);
}));
}
/**
* Same as {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker#executeAndReportWithFallback} but using the circuit breaker default
* fallback.
* @param resultPromise the promise on which the operation result is reported
* @param command the operation
* @return the current {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker}
*/
public io.vertx.rxjava.circuitbreaker.CircuitBreaker executeAndReport(io.vertx.rxjava.core.Promise resultPromise, Handler> command) {
delegate.executeAndReport(resultPromise.getDelegate(), new Handler>() {
public void handle(io.vertx.core.Promise event) {
command.handle(io.vertx.rxjava.core.Promise.newInstance((io.vertx.core.Promise)event, resultPromise.__typeArg_0));
}
});
return this;
}
/**
* Executes the given operation with the circuit breaker control. The operation is generally calling an
* external system. The operation receives a object as parameter and must
* call when the operation has terminated successfully. The operation must also
* call in case of failure.
*
* The operation is not invoked if the circuit breaker is open, and the given fallback is called immediately. The
* circuit breaker also monitor the completion of the operation before a configure timeout. The operation is
* considered as failed if it does not terminate in time.
*
* Unlike {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker#executeWithFallback}, this method does return a object, but
* let the caller pass a object on which the result is reported. If the fallback is called, the future
* is successfully completed with the value returned by the fallback function. If the fallback throws an exception,
* the future is marked as failed.
* @param resultPromise the promise on which the operation result is reported
* @param command the operation
* @param fallback the fallback function. It gets an exception as parameter and returns the fallback result
* @return the current {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker}
*/
public io.vertx.rxjava.circuitbreaker.CircuitBreaker executeAndReportWithFallback(io.vertx.rxjava.core.Promise resultPromise, Handler> command, Function fallback) {
delegate.executeAndReportWithFallback(resultPromise.getDelegate(), new Handler>() {
public void handle(io.vertx.core.Promise event) {
command.handle(io.vertx.rxjava.core.Promise.newInstance((io.vertx.core.Promise)event, resultPromise.__typeArg_0));
}
}, fallback);
return this;
}
/**
* Sets a default invoked when the bridge is open to handle the "request", or on failure
* if {@link io.vertx.circuitbreaker.CircuitBreakerOptions} is enabled.
*
* The function gets the exception as parameter and returns the fallback result.
* @param handler the handler
* @return the current {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker}
*/
public io.vertx.rxjava.circuitbreaker.CircuitBreaker fallback(Function handler) {
delegate.fallback(handler);
return this;
}
/**
* Resets the circuit breaker state (number of failure set to 0 and state set to closed).
* @return the current {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker}
*/
public io.vertx.rxjava.circuitbreaker.CircuitBreaker reset() {
delegate.reset();
return this;
}
/**
* Explicitly opens the circuit.
* @return the current {@link io.vertx.rxjava.circuitbreaker.CircuitBreaker}
*/
public io.vertx.rxjava.circuitbreaker.CircuitBreaker open() {
delegate.open();
return this;
}
/**
* @return the current state.
*/
public io.vertx.circuitbreaker.CircuitBreakerState state() {
io.vertx.circuitbreaker.CircuitBreakerState ret = delegate.state();
return ret;
}
/**
* @return the current number of failures.
*/
public long failureCount() {
long ret = delegate.failureCount();
return ret;
}
/**
* @return the name of the circuit breaker.
*/
public String name() {
if (cached_0 != null) {
return cached_0;
}
String ret = delegate.name();
cached_0 = ret;
return ret;
}
public io.vertx.rxjava.circuitbreaker.CircuitBreaker retryPolicy(Function retryPolicy) {
delegate.retryPolicy(retryPolicy);
return this;
}
private String cached_0;
public static CircuitBreaker newInstance(io.vertx.circuitbreaker.CircuitBreaker arg) {
return arg != null ? new CircuitBreaker(arg) : null;
}
}