Search in sources :

Example 1 with HystrixTimeoutException

use of com.netflix.hystrix.exception.HystrixTimeoutException in project Hystrix by Netflix.

the class AbstractCommand method executeCommandAndObserve.

/**
     * This decorates "Hystrix" functionality around the run() Observable.
     *
     * @return R
     */
private Observable<R> executeCommandAndObserve(final AbstractCommand<R> _cmd) {
    final HystrixRequestContext currentRequestContext = HystrixRequestContext.getContextForCurrentThread();
    final Action1<R> markEmits = new Action1<R>() {

        @Override
        public void call(R r) {
            if (shouldOutputOnNextEvents()) {
                executionResult = executionResult.addEvent(HystrixEventType.EMIT);
                eventNotifier.markEvent(HystrixEventType.EMIT, commandKey);
            }
            if (commandIsScalar()) {
                long latency = System.currentTimeMillis() - executionResult.getStartTimestamp();
                eventNotifier.markCommandExecution(getCommandKey(), properties.executionIsolationStrategy().get(), (int) latency, executionResult.getOrderedList());
                eventNotifier.markEvent(HystrixEventType.SUCCESS, commandKey);
                executionResult = executionResult.addEvent((int) latency, HystrixEventType.SUCCESS);
                circuitBreaker.markSuccess();
            }
        }
    };
    final Action0 markOnCompleted = new Action0() {

        @Override
        public void call() {
            if (!commandIsScalar()) {
                long latency = System.currentTimeMillis() - executionResult.getStartTimestamp();
                eventNotifier.markCommandExecution(getCommandKey(), properties.executionIsolationStrategy().get(), (int) latency, executionResult.getOrderedList());
                eventNotifier.markEvent(HystrixEventType.SUCCESS, commandKey);
                executionResult = executionResult.addEvent((int) latency, HystrixEventType.SUCCESS);
                circuitBreaker.markSuccess();
            }
        }
    };
    final Func1<Throwable, Observable<R>> handleFallback = new Func1<Throwable, Observable<R>>() {

        @Override
        public Observable<R> call(Throwable t) {
            Exception e = getExceptionFromThrowable(t);
            executionResult = executionResult.setExecutionException(e);
            if (e instanceof RejectedExecutionException) {
                return handleThreadPoolRejectionViaFallback(e);
            } else if (t instanceof HystrixTimeoutException) {
                return handleTimeoutViaFallback();
            } else if (t instanceof HystrixBadRequestException) {
                return handleBadRequestByEmittingError(e);
            } else {
                /*
                     * Treat HystrixBadRequestException from ExecutionHook like a plain HystrixBadRequestException.
                     */
                if (e instanceof HystrixBadRequestException) {
                    eventNotifier.markEvent(HystrixEventType.BAD_REQUEST, commandKey);
                    return Observable.error(e);
                }
                return handleFailureViaFallback(e);
            }
        }
    };
    final Action1<Notification<? super R>> setRequestContext = new Action1<Notification<? super R>>() {

        @Override
        public void call(Notification<? super R> rNotification) {
            setRequestContextIfNeeded(currentRequestContext);
        }
    };
    Observable<R> execution;
    if (properties.executionTimeoutEnabled().get()) {
        execution = executeCommandWithSpecifiedIsolation(_cmd).lift(new HystrixObservableTimeoutOperator<R>(_cmd));
    } else {
        execution = executeCommandWithSpecifiedIsolation(_cmd);
    }
    return execution.doOnNext(markEmits).doOnCompleted(markOnCompleted).onErrorResumeNext(handleFallback).doOnEach(setRequestContext);
}
Also used : Action0(rx.functions.Action0) Action1(rx.functions.Action1) HystrixTimeoutException(com.netflix.hystrix.exception.HystrixTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) HystrixTimeoutException(com.netflix.hystrix.exception.HystrixTimeoutException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) Observable(rx.Observable) Notification(rx.Notification) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) HystrixRequestContext(com.netflix.hystrix.strategy.concurrency.HystrixRequestContext) Func1(rx.functions.Func1)

Aggregations

HystrixBadRequestException (com.netflix.hystrix.exception.HystrixBadRequestException)1 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)1 HystrixTimeoutException (com.netflix.hystrix.exception.HystrixTimeoutException)1 HystrixRequestContext (com.netflix.hystrix.strategy.concurrency.HystrixRequestContext)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Notification (rx.Notification)1 Observable (rx.Observable)1 Action0 (rx.functions.Action0)1 Action1 (rx.functions.Action1)1 Func1 (rx.functions.Func1)1