2

We can execute some code asynchronously in C# Rx, as shown below, using Observable.Start(). I am wondering what is the equivalent in RxJava.

void Main()
{
      AddTwoNumbersAsync (5,4)
      .Subscribe(x=>Console.WriteLine(x));

}
IObservable<int> AddTwoNumbersAsync(int a, int b)
{
      return Observable.Start(() => AddTwoNumbers(a, b));
}
int AddTwoNumbers(int a, int b)
{
  return a + b;
}
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327

2 Answers2

5

You can defer the operation until subscription, and ensure that subscription happens on another thread:

Observable<Integer> sumDeferred = Observable.defer(new Func0<Observable<Integer>>() {
        @Override
        public Observable<Integer> call() {
            return Observable.just(addTwoNumbers(5, 4));
        }
    }).subscribeOn(Schedulers.io());
sumDeferred.subscribe(...);
Adam S
  • 16,144
  • 6
  • 54
  • 81
  • Is this the only way? On this netflix blig, I see them using Executors. Wondering whats the difference in that and this approach. http://techblog.netflix.com/2013/02/rxjava-netflix-api.html – Aravind Yarram Jan 12 '15 at 15:56
  • I doubt this is the only way - using an Executor is simply handling the threading explicitly yourself. This answer is the only way I've found to avoid that, though I'd be eager to see a better solution. – Adam S Jan 12 '15 at 16:05
  • 1
    `Start` has the exact opposite semantics as `Defer` when it comes to temperature. Internally, `Start` starts the operation immediately and uses an `AsyncSubject` to capture the result. – Dave Sexton Jan 12 '15 at 16:28
  • @DaveSexton do you know which operator to use in RxJava? – Aravind Yarram Jan 12 '15 at 16:46
  • No, sorry. When I read your question I searched the RxJava docs briefly but I didn't find anything related; otherwise, I would've answered the question myself :) – Dave Sexton Jan 12 '15 at 17:00
  • As Dave says, I don't believe there's a built-in to do this quite in this way. We're using the deferral method above to implement async observables in a few places in our app where a "hot" async observable would be better suited. This works, though, so it's not high on my priorities to find a better way just yet, unfortunately. – Adam S Jan 12 '15 at 17:04
  • 1
    @AdamS and Dave I found few async operators on the wiki here but i don't see them in the API, https://github.com/ReactiveX/RxJava/wiki/Async-Operators – Aravind Yarram Jan 12 '15 at 17:10
  • 1
    Async Operators is in another project: https://github.com/ReactiveX/RxJavaAsyncUtil because async apis are not stable, RxJava moved them to RxJavaAsyncUtil when it released 1.0. – zsxwing Jan 13 '15 at 02:32
1

I would use the Flowable Object from rxJava.

public static void main(String[] args) throws Exception {
    Flowable.fromCallable(() -> addTwoNumbersAsync(5, 4))
            .subscribe(result -> System.out.println(result));
    Thread.sleep(1000);
}
    
private static int addTwoNumbersAsync(int a, int b) {
    return a + b;
}

The method call and the System print will be in a rxJava Thread, and not in the main Thread. You can specify the threadpool on which the Flowable will operate, by adding .subscribeOn(Schedulers.computation()) before the .subscribe(...)for example.

You can also make a method, which returns the Flowable, which is closer to your original example.

public static void main(String[] args) throws Exception {
    addTwoNumbersAsync(5,4)
            .subscribe(result -> System.out.println(result));
    Thread.sleep(1000);
}
    
private static Flowable<Integer> addTwoNumbersAsync(int a, int b) {
    return Flowable.fromCallable(() -> a+b);
}
Florian G.
  • 11
  • 1