Hystrix icon indicating copy to clipboard operation
Hystrix copied to clipboard

Javanica: Allow real Observables as return types, not just ObservableResult

Open spencergibb opened this issue 11 years ago • 18 comments

Using @HystrixCommand with a normal Observable fails with the exception.

java.lang.RuntimeException: return type of 'feed' method should be com.netflix.hystrix.contrib.javanica.command.ObservableResult.
    at com.netflix.hystrix.contrib.javanica.command.closure.AbstractClosureFactory.createClosure(AbstractClosureFactory.java:59)
    at com.netflix.hystrix.contrib.javanica.command.closure.AbstractClosureFactory.createClosure(AbstractClosureFactory.java:41)
    at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.executeWithArgs(MethodExecutionAction.java:79)
    at com.netflix.hystrix.contrib.javanica.command.MethodExecutionAction.execute(MethodExecutionAction.java:66)
    at com.netflix.hystrix.contrib.javanica.command.GenericCommand$1.execute(GenericCommand.java:45)
    at com.netflix.hystrix.contrib.javanica.command.AbstractHystrixCommand.process(AbstractHystrixCommand.java:161)
    at com.netflix.hystrix.contrib.javanica.command.GenericCommand.run(GenericCommand.java:42)
    at com.netflix.hystrix.HystrixCommand$1.call(HystrixCommand.java:294)
    at com.netflix.hystrix.HystrixCommand$1.call(HystrixCommand.java:289)
    at rx.Observable$2.call(Observable.java:173)
    at rx.Observable$2.call(Observable.java:166)
    at rx.Observable$2.call(Observable.java:173)
    at rx.Observable$2.call(Observable.java:166)
    at rx.Observable$2.call(Observable.java:173)
    at rx.Observable$2.call(Observable.java:166)
    at rx.Observable.unsafeSubscribe(Observable.java:8591)
    at com.netflix.hystrix.AbstractCommand$5.call(AbstractCommand.java:514)
    at com.netflix.hystrix.AbstractCommand$5.call(AbstractCommand.java:497)
    at rx.Observable.unsafeSubscribe(Observable.java:8591)
    at rx.internal.operators.OperatorSubscribeOn$1$1.call(OperatorSubscribeOn.java:62)
    at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:56)
    at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:47)
    at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call(HystrixContexSchedulerAction.java:69)
    at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:43)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

spencergibb avatar Mar 26 '15 01:03 spencergibb

Thanks, I will take a look and fix

dmgcodevil avatar Mar 26 '15 09:03 dmgcodevil

Hi @spencergibb, actually it doesn't make sense to return normal Observable because it will be replaced with Observable created during execution in real-time, thus it doesn't matter that you return in this method because it always considered as a stub. Can you say why you need this ?

dmgcodevil avatar Mar 27 '15 19:03 dmgcodevil

Anything that returns an Observable that isn't an ObservableResult will fail with that exception.

@HystrixCommand
public Observable<String> mymethod() {
  return Observable.from("1", "2");
}

spencergibb avatar Mar 30 '15 13:03 spencergibb

@dmgcodevil what would be the right pattern for using Observables in a hystrix command, regardless of javanica or not?

spencergibb avatar Apr 14 '15 19:04 spencergibb

Hi @spencergibb, Please read the following articles: http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/ http://reactivex.io/documentation/observable.html https://github.com/Netflix/Hystrix/wiki/How-To-Use#Reactive-Execution https://github.com/Netflix/Hystrix/wiki/How-To-Use#Reactive-Commands

dmgcodevil avatar Apr 15 '15 09:04 dmgcodevil

@spencergibb There are some examples of this at https://github.com/Netflix/ReactiveLab.

If you're specifically interested in HystrixObservableCommands, see https://github.com/Netflix/ReactiveLab/tree/master/reactive-lab-gateway/src/main/java/io/reactivex/lab/gateway/clients

mattrjacobs avatar Apr 15 '15 19:04 mattrjacobs

@dmgcodevil @mattrjacobs thanks. So @HystrixCommand with ObservableResult will never have the usability that using HystrixObservableCommand would.

spencergibb avatar Apr 15 '15 19:04 spencergibb

@spencergibb why ? ObservableResult is just a stab, at runtime it's replaced with Observable produced by HystrixObservableCommand that can be used as normal Observable.

   @HystrixCommand
    public Observable<User> getUserById(final String id) {
        return new ObservableResult<User>() {
            @Override
            public User invoke() {
                return userResource.getUserById(id);
            }
        };
    }

Equivalent to

// command code is omitted
Observable<User> user= new CommandGetUserById("1").observe();

Could you give me some case when javanica doesn't make sense for reactive execution.

dmgcodevil avatar Apr 16 '15 08:04 dmgcodevil

any news about that ?

java.lang.RuntimeException: return type of 'feed' method should be com.netflix.hystrix.contrib.javanica.command.ObservableResult.

i get the same exception if i use :

    @HystrixCommand
    public Observable<String> sitecall() {
return ObservableResult.create(
                (Observable.OnSubscribe<String>) subscriber -> {
                                    .......
                }
        );
   }

ArekCzarnik avatar Apr 30 '15 14:04 ArekCzarnik

HystrixObservableCommand allows many values to be returned reactivly like this:

class MyObservableCommand extends HystrixObservableCommand {

  @Override
protected Observable<EventDTO> construct() {
    return Observable.create(new Observable.OnSubscribe<MyDTO >() {
        @Override
        public void call(Subscriber<? super MyDTO > observer) {
            try {
                    List<MyDTO> dtos = restClient.getDTOs();
                    for (MyDTO myDTO : dtos) {
                        observer.onNext(myDTO );
                    }
                    observer.onCompleted();
                }
            } catch (Exception e) {
                observer.onError(e);
            }
        }
    });
}

Is it possible to achieve this with the ObservableResult construct?

    return new ObservableResult<User>() {
        @Override
        public User invoke() {
            return userResource.getUserById(id);
        }

draxly avatar Jul 22 '15 12:07 draxly

Closing this due to age. If you'd like to continue the discussion, please re-open.

mattrjacobs avatar Oct 15 '15 16:10 mattrjacobs

@mattrjacobs @dmgcodevil my issue here has never been that the consumers of a method annotated with @HystrixCommand couldn't use the results reactively. It's that the producer can't be an Observerable unless it is converted to blocking. I was hoping to use @HystrixCommand as a convenience, but it only seems to be a convenience for wrapping blocking operations.

spencergibb avatar Oct 15 '15 20:10 spencergibb

@spencergibb, @ArekCzarnik, @draxly Hi guys, finally I found a time to ponder about it. Looks like I missed HystrixObservableCommand and implemented this feature using regular HystrixCommand thus I even wasn't aware about this ability, probably it happened because there was't HystrixObservableCommand when I was implementing it, but now I see that the vibe is, so I'll reopen or crate issue and will work on it.

dmgcodevil avatar Oct 15 '15 22:10 dmgcodevil

Thanks @dmgcodevil! I played with a proof of concept here today: https://github.com/spring-cloud/spring-cloud-netflix/commit/78047c4d2265eeffdbc87e29d7d5c2c6909e2076 doing exactly what you just said, if it is a real Observable (not ObservableResult) use HystrixObservableCommand.

spencergibb avatar Oct 15 '15 22:10 spencergibb

@spencergibb thanks, will take a look, but fix shouldn't be hard for javanica though.

dmgcodevil avatar Oct 15 '15 23:10 dmgcodevil

@spencergibb That's right. If the operation wrapped by Hystrix is synchronous (like blocking I/O), you should use a HystrixCommand. If the wrapped operation is asynchronous (like nonblocking I/O), you should use a HystrixObservableCommand.

mattrjacobs avatar Oct 16 '15 15:10 mattrjacobs

Right, so that missed in javanica, that's why it doesn't work properly now, will rise new issue. I have a filling like observable command was added after I implemented it in javanica On 16 Oct 2015 11:53 a.m., "Matt Jacobs" [email protected] wrote:

@spencergibb https://github.com/spencergibb That's right. If the operation wrapped by Hystrix is synchronous (like blocking I/O), you should use a HystrixCommand. If the wrapped operation is asynchronous (like nonblocking I/O), you should use a HystrixObservableCommand.

— Reply to this email directly or view it on GitHub https://github.com/Netflix/Hystrix/issues/729#issuecomment-148752938.

dmgcodevil avatar Oct 16 '15 16:10 dmgcodevil

@HystrixCommand public Future getUserByIdAsync(final String id) { return new AsyncResult() { xyz = userResource.getUserById(id) @Override public User invoke() { return xyz; } }; } Can we do something like above? that is fetching the result outside invoke and just do the return inside invoke()? @dmgcodevil @spencergibb

CoolSuplex avatar Aug 27 '18 20:08 CoolSuplex