Is RxJava a good fit for branching workflows?

后端 未结 2 1123
情深已故
情深已故 2021-01-23 13:48

I am using RxJava to process some notifications that we pull from a queue.

RxJava seemed to work fine with a simple workflow, now with new requirements coming in, the fl

2条回答
  •  Happy的楠姐
    2021-01-23 14:27

    Just an idea for another approach which may work for you: Instead of grouping/toMap, you could multicast the source and handle the branches individually.

    Example:

    @Test
    public void multicastingShare() {
        final Observable sharedSource = Observable.range(1, 10)
                .doOnSubscribe(dummy -> System.out.println("subscribed"))
                .share();
        // split by some criteria
        final Observable oddItems = sharedSource
                .filter(n -> n % 2 == 1)
                .map(odd -> "odd: " + odd)
                .doOnNext(System.out::println);
        final Observable evenItems = sharedSource
                .filter(n -> n % 2 == 0)
                .map(even -> "even: " + even)
                .doOnNext(System.out::println);
    
        // recombine the individual streams at some point
        Observable.concat(oddItems, evenItems)
                .subscribe(result -> System.out.println("result: " + result));
    }
    

    This video may be be helpful (at least the first 15 min)

提交回复
热议问题