Martin Erlic
Martin Erlic

Reputation: 5665

ReactiveX Java process finishing early?

public class ReactiveExample {

    public static void main(String[] args) throws InterruptedException, NumberFormatException, UnsupportedEncodingException {

        Observable.range(1, 5).subscribe(
                System.out::println,
                error -> System.out.println("error"),
                () -> System.out.println("completed")
        );

    }

}

The result that is printed out is rx.internal.util.ScalarSynchronousObservable@2fad386b

After the observable is printed I get:

Process finished with exit code 0.

I just started out with ReactiveX and have been following a few tutorials. My assumption was that the code above will continuously stream data as such:

1-2-3-4-5-1-2-3-4-5... and continue to print the value. Why does my program stop immediately? Does it just end after the first 5 digits have been observed? How can I change this to continuously stream values and print them as those values are looped through? Also, how do I actually print the values instead of the observable object reference?

Upvotes: 0

Views: 37

Answers (1)

Heikki Vesalainen
Heikki Vesalainen

Reputation: 66

range: Returns an Observable that emits a sequence of Integers within a specified range.

So your assumption is wrong. range does not repeat anything. For that you need to use repeat

Observable.range(1, 5).repeat().subscribe(
  System.out::println,
  error -> System.out.println("error"),
  () -> System.out.println("completed")
);

Upvotes: 1

Related Questions