javareactive-programmingrx-javareactive-streams

Observable composed of cold and hot observables


I'm having hard time finding proper way of composing an observable which will emit all items from the given cold observable A and as soon as its completes continue with hot observable B.

This is my specific use case : I have a data collector which in realtime appends data to the append only database (an event stream). And when a request arrives for streaming all the event stream it is expected to start streaming everything from database and as soon as database has no more data it shall start streaming whatever the collector streams... as you see both are available as observables.

I'm new to reactive programming, hence my question may be a bit abstract.

Here is a diagram for this behavior :

B ----B---B---B----B--B---B---B---X------>
                   |  |   |   |   |
                   |  |   |   |   |
R --A---A--A----?--B--B---B---B---X------>
    |   |  |    |
    |   |  |    |
A --A---A--A----X------------------------>

Here R is our result observable and A is the cold one, B is the hot one. R is terminating with B.


Solution

  • If B is hot, then simply A.concat(B) should work, because B will be subscribed to only when A finishes.