Java Code Examples for reactor.core.publisher.Flux#onErrorResume()

The following examples show how to use reactor.core.publisher.Flux#onErrorResume() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReactiveResilience4JCircuitBreaker.java    From spring-cloud-circuitbreaker with Apache License 2.0 6 votes vote down vote up
public <T> Flux<T> run(Flux<T> toRun, Function<Throwable, Flux<T>> fallback) {
	io.github.resilience4j.circuitbreaker.CircuitBreaker defaultCircuitBreaker = registry
			.circuitBreaker(id, config.getCircuitBreakerConfig());
	circuitBreakerCustomizer
			.ifPresent(customizer -> customizer.customize(defaultCircuitBreaker));
	Flux<T> toReturn = toRun
			.transform(CircuitBreakerOperator.of(defaultCircuitBreaker))
			.timeout(config.getTimeLimiterConfig().getTimeoutDuration())
			// Since we are using the Flux timeout we need to tell the circuit breaker
			// about the error
			.doOnError(TimeoutException.class,
					t -> defaultCircuitBreaker.onError(config.getTimeLimiterConfig()
							.getTimeoutDuration().toMillis(), TimeUnit.MILLISECONDS,
							t));
	if (fallback != null) {
		toReturn = toReturn.onErrorResume(fallback);
	}
	return toReturn;
}
 
Example 2
Source File: CtrlRscToggleDiskApiCallHandler.java    From linstor-server with GNU General Public License v3.0 6 votes vote down vote up
private Flux<ApiCallRc> resetDiskAddingInTransaction(
    NodeName nodeName,
    ResourceName rscName
)
{
    Resource rsc = ctrlApiDataLoader.loadRsc(nodeName, rscName, true);

    unmarkDiskAdding(rsc);

    ctrlTransactionHelper.commit();

    Flux<ApiCallRc> satelliteUpdateResponses = ctrlSatelliteUpdateCaller.updateSatellites(rsc, Flux.empty())
        .transform(responses -> CtrlResponseUtils.combineResponses(
            responses,
            rscName,
            "Diskless state temporarily reset on {0}"
        ));

    return satelliteUpdateResponses
        .onErrorResume(CtrlResponseUtils.DelayedApiRcException.class, ignored -> Flux.empty());
}
 
Example 3
Source File: ReactorHttpRpcClient.java    From etherjar with Apache License 2.0 6 votes vote down vote up
@Override
public Flux<RpcCallResponse> execute(ReactorBatch batch) {
    BatchCallContext<ReactorBatch.ReactorBatchItem> context = new BatchCallContext<>();

    Flux<RpcCallResponse> result = batch.getItems()
        .doOnNext(context::add)
        .thenMany(transport.execute(batch.getItems(), context))
        .onErrorResume(ConnectException.class, ReactorHandlers.catchConnectException())
        ;


    FailedBatchProcessor failedBatchProcessor = this.getFailedBatchProcessor();
    if (failedBatchProcessor != null) {
        Function<RpcException, Publisher<RpcCallResponse>> fallback = failedBatchProcessor.createFallback(batch);
        if (fallback != null) {
            result = result.onErrorResume(RpcException.class, fallback);
        }
    }

    result = postProcess(batch, context, result);

    result = result.doOnError((t) -> System.err.println("HTTP Error " + t.getClass() + ": " + t.getMessage()));

    return result.share();
}
 
Example 4
Source File: ReactiveSentinelCircuitBreaker.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Flux<T> run(Flux<T> toRun, Function<Throwable, Flux<T>> fallback) {
	Flux<T> toReturn = toRun.transform(new SentinelReactorTransformer<>(
			new EntryConfig(resourceName, entryType)));
	if (fallback != null) {
		toReturn = toReturn.onErrorResume(fallback);
	}
	return toReturn;
}
 
Example 5
Source File: HttpSupplier.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
private Flux<?> get(WebClient client) {
	Flux<?> result = client.get().uri(this.props.getSource().getUrl()).exchange()
			.flatMap(this::transform).repeat();
	if (this.props.isDebug()) {
		result = result.log();
	}
	return result.onErrorResume(TerminateException.class, error -> Mono.empty());
}
 
Example 6
Source File: RecoveryFunction.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
default Flux<? super O> onErrorResume(Flux<? super O> flux) {
    return flux.onErrorResume(t -> {
        O fallbackValue;
        try {
            Throwable actual = Optional.ofNullable(t.getCause()).orElse(t);
            fallbackValue = apply(actual);
            if (fallbackValue instanceof Flux) {
                return (Flux) fallbackValue;
            }
        } catch (Exception e) {
            return Flux.error(e);
        }
        return Flux.just(fallbackValue);
    });
}
 
Example 7
Source File: AbstractMessageReaderArgumentResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Read the body from a method argument with {@link HttpMessageReader}.
 * @param bodyParam represents the element type for the body
 * @param actualParam the actual method argument type; possibly different
 * from {@code bodyParam}, e.g. for an {@code HttpEntity} argument
 * @param isBodyRequired true if the body is required
 * @param bindingContext the binding context to use
 * @param exchange the current exchange
 * @return a Mono with the value to use for the method argument
 * @since 5.0.2
 */
protected Mono<Object> readBody(MethodParameter bodyParam, @Nullable MethodParameter actualParam,
		boolean isBodyRequired, BindingContext bindingContext, ServerWebExchange exchange) {

	ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParam);
	ResolvableType actualType = (actualParam != null ? ResolvableType.forMethodParameter(actualParam) : bodyType);
	Class<?> resolvedType = bodyType.resolve();
	ReactiveAdapter adapter = (resolvedType != null ? getAdapterRegistry().getAdapter(resolvedType) : null);
	ResolvableType elementType = (adapter != null ? bodyType.getGeneric() : bodyType);
	isBodyRequired = isBodyRequired || (adapter != null && !adapter.supportsEmpty());

	ServerHttpRequest request = exchange.getRequest();
	ServerHttpResponse response = exchange.getResponse();

	MediaType contentType = request.getHeaders().getContentType();
	MediaType mediaType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);
	Object[] hints = extractValidationHints(bodyParam);

	if (mediaType.isCompatibleWith(MediaType.APPLICATION_FORM_URLENCODED)) {
		return Mono.error(new IllegalStateException(
				"In a WebFlux application, form data is accessed via ServerWebExchange.getFormData()."));
	}

	if (logger.isDebugEnabled()) {
		logger.debug(exchange.getLogPrefix() + (contentType != null ?
				"Content-Type:" + contentType :
				"No Content-Type, using " + MediaType.APPLICATION_OCTET_STREAM));
	}

	for (HttpMessageReader<?> reader : getMessageReaders()) {
		if (reader.canRead(elementType, mediaType)) {
			Map<String, Object> readHints = Hints.from(Hints.LOG_PREFIX_HINT, exchange.getLogPrefix());
			if (adapter != null && adapter.isMultiValue()) {
				if (logger.isDebugEnabled()) {
					logger.debug(exchange.getLogPrefix() + "0..N [" + elementType + "]");
				}
				Flux<?> flux = reader.read(actualType, elementType, request, response, readHints);
				flux = flux.onErrorResume(ex -> Flux.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					flux = flux.switchIfEmpty(Flux.error(() -> handleMissingBody(bodyParam)));
				}
				if (hints != null) {
					flux = flux.doOnNext(target ->
							validate(target, hints, bodyParam, bindingContext, exchange));
				}
				return Mono.just(adapter.fromPublisher(flux));
			}
			else {
				// Single-value (with or without reactive type wrapper)
				if (logger.isDebugEnabled()) {
					logger.debug(exchange.getLogPrefix() + "0..1 [" + elementType + "]");
				}
				Mono<?> mono = reader.readMono(actualType, elementType, request, response, readHints);
				mono = mono.onErrorResume(ex -> Mono.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					mono = mono.switchIfEmpty(Mono.error(() -> handleMissingBody(bodyParam)));
				}
				if (hints != null) {
					mono = mono.doOnNext(target ->
							validate(target, hints, bodyParam, bindingContext, exchange));
				}
				return (adapter != null ? Mono.just(adapter.fromPublisher(mono)) : Mono.from(mono));
			}
		}
	}

	// No compatible reader but body may be empty..

	HttpMethod method = request.getMethod();
	if (contentType == null && method != null && SUPPORTED_METHODS.contains(method)) {
		Flux<DataBuffer> body = request.getBody().doOnNext(buffer -> {
			DataBufferUtils.release(buffer);
			// Body not empty, back to 415..
			throw new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes, elementType);
		});
		if (isBodyRequired) {
			body = body.switchIfEmpty(Mono.error(() -> handleMissingBody(bodyParam)));
		}
		return (adapter != null ? Mono.just(adapter.fromPublisher(body)) : Mono.from(body));
	}

	return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes, elementType));
}
 
Example 8
Source File: AbstractMessageReaderArgumentResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Read the body from a method argument with {@link HttpMessageReader}.
 * @param bodyParam represents the element type for the body
 * @param actualParam the actual method argument type; possibly different
 * from {@code bodyParam}, e.g. for an {@code HttpEntity} argument
 * @param isBodyRequired true if the body is required
 * @param bindingContext the binding context to use
 * @param exchange the current exchange
 * @return a Mono with the value to use for the method argument
 * @since 5.0.2
 */
protected Mono<Object> readBody(MethodParameter bodyParam, @Nullable MethodParameter actualParam,
		boolean isBodyRequired, BindingContext bindingContext, ServerWebExchange exchange) {

	ResolvableType bodyType = ResolvableType.forMethodParameter(bodyParam);
	ResolvableType actualType = (actualParam != null ? ResolvableType.forMethodParameter(actualParam) : bodyType);
	Class<?> resolvedType = bodyType.resolve();
	ReactiveAdapter adapter = (resolvedType != null ? getAdapterRegistry().getAdapter(resolvedType) : null);
	ResolvableType elementType = (adapter != null ? bodyType.getGeneric() : bodyType);
	isBodyRequired = isBodyRequired || (adapter != null && !adapter.supportsEmpty());

	ServerHttpRequest request = exchange.getRequest();
	ServerHttpResponse response = exchange.getResponse();

	MediaType contentType = request.getHeaders().getContentType();
	MediaType mediaType = (contentType != null ? contentType : MediaType.APPLICATION_OCTET_STREAM);
	Object[] hints = extractValidationHints(bodyParam);

	if (logger.isDebugEnabled()) {
		logger.debug(exchange.getLogPrefix() + (contentType != null ?
				"Content-Type:" + contentType :
				"No Content-Type, using " + MediaType.APPLICATION_OCTET_STREAM));
	}

	for (HttpMessageReader<?> reader : getMessageReaders()) {
		if (reader.canRead(elementType, mediaType)) {
			Map<String, Object> readHints = Hints.from(Hints.LOG_PREFIX_HINT, exchange.getLogPrefix());
			if (adapter != null && adapter.isMultiValue()) {
				if (logger.isDebugEnabled()) {
					logger.debug(exchange.getLogPrefix() + "0..N [" + elementType + "]");
				}
				Flux<?> flux = reader.read(actualType, elementType, request, response, readHints);
				flux = flux.onErrorResume(ex -> Flux.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					flux = flux.switchIfEmpty(Flux.error(() -> handleMissingBody(bodyParam)));
				}
				if (hints != null) {
					flux = flux.doOnNext(target ->
							validate(target, hints, bodyParam, bindingContext, exchange));
				}
				return Mono.just(adapter.fromPublisher(flux));
			}
			else {
				// Single-value (with or without reactive type wrapper)
				if (logger.isDebugEnabled()) {
					logger.debug(exchange.getLogPrefix() + "0..1 [" + elementType + "]");
				}
				Mono<?> mono = reader.readMono(actualType, elementType, request, response, readHints);
				mono = mono.onErrorResume(ex -> Mono.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					mono = mono.switchIfEmpty(Mono.error(() -> handleMissingBody(bodyParam)));
				}
				if (hints != null) {
					mono = mono.doOnNext(target ->
							validate(target, hints, bodyParam, bindingContext, exchange));
				}
				return (adapter != null ? Mono.just(adapter.fromPublisher(mono)) : Mono.from(mono));
			}
		}
	}

	// No compatible reader but body may be empty..

	HttpMethod method = request.getMethod();
	if (contentType == null && method != null && SUPPORTED_METHODS.contains(method)) {
		Flux<DataBuffer> body = request.getBody().doOnNext(o -> {
			// Body not empty, back to 415..
			throw new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes, elementType);
		});
		if (isBodyRequired) {
			body = body.switchIfEmpty(Mono.error(() -> handleMissingBody(bodyParam)));
		}
		return (adapter != null ? Mono.just(adapter.fromPublisher(body)) : Mono.from(body));
	}

	return Mono.error(new UnsupportedMediaTypeStatusException(mediaType, this.supportedMediaTypes, elementType));
}
 
Example 9
Source File: ReactorEmeraldClient.java    From etherjar with Apache License 2.0 4 votes vote down vote up
@Override
public Flux<RpcCallResponse> execute(ReactorBatch batch) {
    BlockchainOuterClass.NativeCallRequest.Builder requestBuilder = BlockchainOuterClass.NativeCallRequest.newBuilder();
    requestBuilder.setChain(chainRef);
    if (selector != null) {
        requestBuilder.setSelector(selector);
    }

    BatchCallContext<ReactorBatch.ReactorBatchItem> context = new BatchCallContext<>();
    Mono<BlockchainOuterClass.NativeCallRequest> request = batch.getItems()
        .doOnNext(context::add)
        .map(this::asNative)
        .reduce(requestBuilder, BlockchainOuterClass.NativeCallRequest.Builder::addItems)
        .map(BlockchainOuterClass.NativeCallRequest.Builder::build);

    Flux<RpcCallResponse> result = stub.nativeCall(request)
        .flatMap((item) -> {
            RpcCall<?, ?> call = null;
            try {
                call = context.getCall(item.getId());
            } catch (Exception e) {
                System.err.println("Invalid id returned from upstream: " + item.getId());
            }
            return read(item, call);
        })
        .onErrorResume(StatusRuntimeException.class, (e) -> {
          if (e.getStatus().getCode() == Status.Code.CANCELLED) {
              return Mono.empty();
          }
          return Mono.error(new RpcException(
              RpcResponseError.CODE_UPSTREAM_CONNECTION_ERROR,
              "gRPC connection error. Status: " + e.getStatus(),
              null,
              e
          ));
        })
        .map(responseJsonConverter.forContext(context));


    FailedBatchProcessor failedBatchProcessor = this.getFailedBatchProcessor();
    if (failedBatchProcessor != null) {
        Function<RpcException, Publisher<RpcCallResponse>> fallback = failedBatchProcessor.createFallback(batch);
        if (fallback != null) {
            result = result.onErrorResume(RpcException.class, fallback);
        }
    }

    result = result.doOnError((t) -> System.err.println("Client error " + t.getClass() + ": " + t.getMessage()));

    result = postProcess(batch, context, result);

    return result.share();
}
 
Example 10
Source File: RequestProcessor.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
private Publisher<?> body(Object handler, ServerWebExchange exchange) {
	ResolvableType elementType = ResolvableType
			.forClass(this.inspector.getInputType(handler));

	// we effectively delegate type conversion to FunctionCatalog
	elementType = ResolvableType.forClass(String.class);

	ResolvableType actualType = elementType;

	Class<?> resolvedType = elementType.resolve();
	ReactiveAdapter adapter = (resolvedType != null
			? getAdapterRegistry().getAdapter(resolvedType) : null);

	ServerHttpRequest request = exchange.getRequest();
	ServerHttpResponse response = exchange.getResponse();

	MediaType contentType = request.getHeaders().getContentType();
	MediaType mediaType = (contentType != null ? contentType
			: MediaType.APPLICATION_OCTET_STREAM);

	if (logger.isDebugEnabled()) {
		logger.debug(exchange.getLogPrefix() + (contentType != null
				? "Content-Type:" + contentType
				: "No Content-Type, using " + MediaType.APPLICATION_OCTET_STREAM));
	}
	boolean isBodyRequired = (adapter != null && !adapter.supportsEmpty());

	MethodParameter bodyParam = new MethodParameter(handlerMethod(handler), 0);
	for (HttpMessageReader<?> reader : getMessageReaders()) {
		if (reader.canRead(elementType, mediaType)) {
			Map<String, Object> readHints = Hints.from(Hints.LOG_PREFIX_HINT,
					exchange.getLogPrefix());
			if (adapter != null && adapter.isMultiValue()) {
				if (logger.isDebugEnabled()) {
					logger.debug(
							exchange.getLogPrefix() + "0..N [" + elementType + "]");
				}
				Flux<?> flux = reader.read(actualType, elementType, request, response,
						readHints);
				flux = flux.onErrorResume(
						ex -> Flux.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					flux = flux.switchIfEmpty(
							Flux.error(() -> handleMissingBody(bodyParam)));
				}
				return Mono.just(adapter.fromPublisher(flux));
			}
			else {
				// Single-value (with or without reactive type wrapper)
				if (logger.isDebugEnabled()) {
					logger.debug(exchange.getLogPrefix() + "0..1 [" + elementType + "]");
				}
				Mono<?> mono = reader.readMono(actualType, elementType, request,
						response, readHints).doOnNext(v -> {
							if (logger.isDebugEnabled()) {
								logger.debug("received: " + v);
							}
						});
				mono = mono.onErrorResume(
						ex -> Mono.error(handleReadError(bodyParam, ex)));
				if (isBodyRequired) {
					mono = mono.switchIfEmpty(
							Mono.error(() -> handleMissingBody(bodyParam)));
				}
				return (adapter != null ? Mono.just(adapter.fromPublisher(mono))
						: Mono.from(mono));
			}
		}
	}

	return Mono.error(new UnsupportedMediaTypeStatusException(mediaType,
			Arrays.asList(MediaType.APPLICATION_JSON), elementType));
}