官术网_书友最值得收藏!

Cold Observables

Cold Observables are much like a music CD that can be replayed to each listener, so each person can hear all the tracks at any time. In the same manner, cold Observables will replay the emissions to each Observer, ensuring that all Observers get all the data. Most data-driven Observables are cold, and this includes the Observable.just() and Observable.fromIterable() factories.

In the following example, we have two Observers subscribed to one Observable. The Observable will first play all the emissions to the first Observer and then call onComplete(). Then, it will play all the emissions again to the second Observer and call onComplete(). They both receive the same datasets by getting two separate streams each, which is typical behavior for a cold Observable:

      import io.reactivex.Observable;

public class Launcher {
public static void main(String[] args) {

Observable<String> source =
Observable.just("Alpha","Beta","Gamma","Delta","Epsilon");

//first observer
source.subscribe(s -> System.out.println("Observer 1 Received:
"
+ s));

//second observer
source.subscribe(s -> System.out.println("Observer 2 Received:
"
+ s));

}
}

The output is as follows:

    Observer 1 Received: Alpha
Observer 1 Received: Beta
Observer 1 Received: Gamma
Observer 1 Received: Delta
Observer 1 Received: Epsilon
Observer 2 Received: Alpha
Observer 2 Received: Beta
Observer 2 Received: Gamma
Observer 2 Received: Delta
Observer 2 Received: Epsilon

Even if the second Observer transforms its emissions with operators, it will still get its own stream of emissions. Using operators such as map() and filter() against a cold Observable will still maintain the cold nature of the yielded Observables:

    import io.reactivex.Observable;

public class Launcher {
public static void main(String[] args) {

Observable<String> source =
Observable.just("Alpha","Beta","Gamma","Delta","Epsilon");

//first observer
source.subscribe(s -> System.out.println("Observer 1 Received:
"
+ s));

//second observer
source.map(String::length).filter(i -> i >= 5)
.subscribe(s -> System.out.println("Observer 2 Received: " +
s));

}
}

The output is as follows:

    Observer 1 Received: Alpha
Observer 1 Received: Beta
Observer 1 Received: Gamma
Observer 1 Received: Delta
Observer 1 Received: Epsilon
Observer 2 Received: 5
Observer 2 Received: 5
Observer 2 Received: 5
Observer 2 Received: 7

As stated earlier, Observable sources that emit finite datasets are usually cold.

Here is a more real-world example: Dave Moten's RxJava-JDBC (https://github.com/davidmoten/rxjava-jdbc) allows you to create cold Observables built off of SQL database queries. We will not digress into this library for too long, but if you want to query a SQLite database, for instance, include the SQLite JDBC driver and RxJava-JDBC libraries in your project. You can then query a database table reactively, as shown in the following code snippet:

    import com.github.davidmoten.rx.jdbc.ConnectionProviderFromUrl;
import com.github.davidmoten.rx.jdbc.Database;
import rx.Observable;
import java.sql.Connection;

public class Launcher {
public static void main(String[] args) {
Connection conn =
new ConnectionProviderFromUrl("jdbc:sqlite:/home/thomas
/rexon_metals.db"
).get();

Database db = Database.from(conn);

Observable<String> customerNames =
db.select("SELECT NAME FROM CUSTOMER")
.getAs(String.class);

customerNames.subscribe(s -> System.out.println(s));
}
}

The output is as follows:

    LITE Industrial
Rex Tooling Inc
Re-Barre Construction
Prairie Construction
Marsh Lane Metal Works

This SQL-driven Observable is cold. Many Observables emitting from finite data sources such as databases, text files, or JSON are cold. It is still important to note how the source Observable is architected. RxJava-JDBC will run the query each time for each Observer. This means that if the data changes in between two subscriptions, the second Observer will get different emissions than the first one. But the Observable is still cold since it is replaying the query even if the resulting data changes from the underlying tables.

Again, cold Observables will, in some shape or form, repeat the operation to generate these emissions to each Observer. Next, we will cover hot Observables that resemble events more than data.

主站蜘蛛池模板: 濮阳市| 若羌县| 罗山县| 神农架林区| 藁城市| 鸡西市| 湖州市| 根河市| 根河市| 吴旗县| 德令哈市| 东宁县| 顺平县| 耿马| 德阳市| 墨竹工卡县| 淳化县| 工布江达县| 济源市| 徐州市| 莲花县| 弋阳县| 沙田区| 铜陵市| 海丰县| 利津县| 陈巴尔虎旗| 泸定县| 汤阴县| 堆龙德庆县| 奎屯市| 福鼎市| 桑植县| 久治县| 宁晋县| 靖江市| 淳安县| 沂源县| 浙江省| 蛟河市| 铁岭市|