Search in sources :

Example 1 with HystrixObservable

use of com.netflix.hystrix.HystrixObservable in project Hystrix by Netflix.

the class CommandExecutor method execute.

/**
     * Calls a method of {@link HystrixExecutable} in accordance with specified execution type.
     *
     * @param invokable  {@link HystrixInvokable}
     * @param metaHolder {@link MetaHolder}
     * @return the result of invocation of specific method.
     * @throws RuntimeException
     */
public static Object execute(HystrixInvokable invokable, ExecutionType executionType, MetaHolder metaHolder) throws RuntimeException {
    Validate.notNull(invokable);
    Validate.notNull(metaHolder);
    switch(executionType) {
        case SYNCHRONOUS:
            {
                return castToExecutable(invokable, executionType).execute();
            }
        case ASYNCHRONOUS:
            {
                HystrixExecutable executable = castToExecutable(invokable, executionType);
                if (metaHolder.hasFallbackMethodCommand() && ExecutionType.ASYNCHRONOUS == metaHolder.getFallbackExecutionType()) {
                    return new FutureDecorator(executable.queue());
                }
                return executable.queue();
            }
        case OBSERVABLE:
            {
                HystrixObservable observable = castToObservable(invokable);
                return ObservableExecutionMode.EAGER == metaHolder.getObservableExecutionMode() ? observable.observe() : observable.toObservable();
            }
        default:
            throw new RuntimeException("unsupported execution type: " + executionType);
    }
}
Also used : FutureDecorator(com.netflix.hystrix.contrib.javanica.utils.FutureDecorator) HystrixExecutable(com.netflix.hystrix.HystrixExecutable) HystrixObservable(com.netflix.hystrix.HystrixObservable)

Example 2 with HystrixObservable

use of com.netflix.hystrix.HystrixObservable in project java-chassis by ServiceComb.

the class BizkeeperHandlerDelegate method forceFallbackCommand.

protected HystrixObservable<Response> forceFallbackCommand(Invocation invocation) {
    return new HystrixObservable<Response>() {

        @Override
        public Observable<Response> observe() {
            ReplaySubject<Response> subject = ReplaySubject.create();
            final Subscription sourceSubscription = toObservable().subscribe(subject);
            return subject.doOnUnsubscribe(new Action0() {

                @Override
                public void call() {
                    sourceSubscription.unsubscribe();
                }
            });
        }

        @Override
        public Observable<Response> toObservable() {
            return Observable.create(f -> {
                try {
                    if (Configuration.FALLBACKPOLICY_POLICY_RETURN.equals(Configuration.INSTANCE.getFallbackPolicyPolicy(handler.groupname, invocation.getMicroserviceName(), invocation.getOperationMeta().getMicroserviceQualifiedName()))) {
                        f.onNext(Response.succResp(null));
                    } else {
                        f.onNext(Response.failResp(invocation.getInvocationType(), BizkeeperExceptionUtils.createBizkeeperException(BizkeeperExceptionUtils.CSE_HANDLER_BK_FALLBACK, null, invocation.getOperationMeta().getMicroserviceQualifiedName())));
                    }
                } catch (Exception e) {
                    f.onError(e);
                }
            });
        }

        ;
    };
}
Also used : Response(io.servicecomb.core.Response) Action0(rx.functions.Action0) Subscription(rx.Subscription) HystrixObservable(com.netflix.hystrix.HystrixObservable)

Aggregations

HystrixObservable (com.netflix.hystrix.HystrixObservable)2 HystrixExecutable (com.netflix.hystrix.HystrixExecutable)1 FutureDecorator (com.netflix.hystrix.contrib.javanica.utils.FutureDecorator)1 Response (io.servicecomb.core.Response)1 Subscription (rx.Subscription)1 Action0 (rx.functions.Action0)1