BEAL-572 rx project structure, hot cold observables

This commit is contained in:
Tomasz Lelek 2017-02-01 10:58:07 +01:00
parent d07136065a
commit 694931a190
2 changed files with 65 additions and 0 deletions

35
rxjava/pom.xml Normal file
View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>rxjava</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.reactivex</groupId>
<artifactId>rxjava</artifactId>
<version>${rx.java.version}</version>
</dependency>
</dependencies>
<properties>
<rx.java.version>1.2.5</rx.java.version>
</properties>
</project>

View File

@ -0,0 +1,30 @@
package com.baelding.rxjava;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;
public class HotObservableBackPressure {
public static void main(String[] args) throws InterruptedException {
PublishSubject<Integer> source = PublishSubject.create();
source
.observeOn(Schedulers.computation())
.subscribe(v -> compute(v), Throwable::printStackTrace);
for (int i = 0; i < 1_000_000; i++) {
source.onNext(i);
}
Thread.sleep(10_000);
}
private static void compute(Integer v) {
try {
System.out.println("compute integer v: "+ v);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}