forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
38 changes: 38 additions & 0 deletions
38
rxjava/src/main/java/com/baelding/rxjava/ColdObservableBackpressure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.baelding.rxjava; | ||
|
||
import rx.Observable; | ||
import rx.schedulers.Schedulers; | ||
|
||
public class ColdObservableBackpressure { | ||
public static void main(String[] args) throws InterruptedException { | ||
Observable.range(1, 1_000_000).observeOn(Schedulers.computation()).subscribe(v -> ComputeFunction.compute(v), Throwable::printStackTrace); | ||
|
||
Thread.sleep(10_000); | ||
|
||
// Observable.range(1, 1_000_000) //implementation of reactive pull backpressure on cold observable | ||
// .subscribe(new Subscriber<Integer>() { | ||
// @Override | ||
// public void onStart() { | ||
// request(1); | ||
// } | ||
// | ||
// public void onNext(Integer v) { | ||
// compute(v); | ||
// | ||
// request(1); | ||
// } | ||
// | ||
// @Override | ||
// public void onError(Throwable ex) { | ||
// ex.printStackTrace(); | ||
// } | ||
// | ||
// @Override | ||
// public void onCompleted() { | ||
// System.out.println("Done!"); | ||
// } | ||
// }); | ||
|
||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
rxjava/src/main/java/com/baelding/rxjava/ComputeFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.baelding.rxjava; | ||
|
||
import rx.Observable; | ||
|
||
import java.util.List; | ||
|
||
public class ComputeFunction { | ||
public static void compute(Integer v) { | ||
try { | ||
System.out.println("compute integer v: " + v); | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void compute(List<Integer> v) { | ||
try { | ||
System.out.println("compute integer v: " + v); | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void compute(Observable<Integer> v) { | ||
try { | ||
v.forEach(System.out::println); | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void compute(Long v) { | ||
try { | ||
System.out.println("compute integer v: " + v); | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBatching.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.baelding.rxjava; | ||
|
||
import rx.schedulers.Schedulers; | ||
import rx.subjects.PublishSubject; | ||
|
||
public class HotObservableBackpressureBatching { | ||
public static void main(String[] args) throws InterruptedException { | ||
PublishSubject<Integer> source = PublishSubject.<Integer>create(); | ||
|
||
source.window(500).observeOn(Schedulers.computation()).subscribe(ComputeFunction::compute, Throwable::printStackTrace); | ||
|
||
for (int i = 0; i < 1_000_000; i++) { | ||
source.onNext(i); | ||
} | ||
Thread.sleep(10_000); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBuffering.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.baelding.rxjava; | ||
|
||
import rx.schedulers.Schedulers; | ||
import rx.subjects.PublishSubject; | ||
|
||
public class HotObservableBackpressureBuffering { | ||
public static void main(String[] args) throws InterruptedException { | ||
PublishSubject<Integer> source = PublishSubject.<Integer>create(); | ||
|
||
source.buffer(1024).observeOn(Schedulers.computation()).subscribe(ComputeFunction::compute, Throwable::printStackTrace); | ||
|
||
for (int i = 0; i < 1_000_000; i++) { | ||
source.onNext(i); | ||
} | ||
Thread.sleep(10_000); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureSkipping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.baelding.rxjava; | ||
|
||
import rx.schedulers.Schedulers; | ||
import rx.subjects.PublishSubject; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class HotObservableBackpressureSkipping { | ||
public static void main(String[] args) throws InterruptedException { | ||
PublishSubject<Integer> source = PublishSubject.<Integer>create(); | ||
|
||
source.sample(100, TimeUnit.MILLISECONDS) | ||
// .throttleFirst(100, TimeUnit.MILLISECONDS) | ||
.observeOn(Schedulers.computation()).subscribe(ComputeFunction::compute, Throwable::printStackTrace); | ||
|
||
for (int i = 0; i < 1_000_000; i++) { | ||
source.onNext(i); | ||
} | ||
Thread.sleep(10_000); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
rxjava/src/main/java/com/baelding/rxjava/HotObservableOnBackpressure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.baelding.rxjava; | ||
|
||
import rx.BackpressureOverflow; | ||
import rx.Observable; | ||
import rx.schedulers.Schedulers; | ||
|
||
public class HotObservableOnBackpressure { | ||
public static void main(String[] args) throws InterruptedException { | ||
Observable.range(1, 1_000_000).onBackpressureBuffer(16, () -> { | ||
}, BackpressureOverflow.ON_OVERFLOW_DROP_OLDEST).observeOn(Schedulers.computation()).subscribe(e -> { | ||
}, Throwable::printStackTrace); | ||
|
||
Observable.range(1, 1_000_000).onBackpressureDrop().observeOn(Schedulers.io()).doOnNext(ComputeFunction::compute).subscribe(v -> { | ||
}, Throwable::printStackTrace); | ||
Thread.sleep(10_000); | ||
|
||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
rxjava/src/main/java/com/baelding/rxjava/HotObservableWithoutBackpressure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.baelding.rxjava; | ||
|
||
|
||
import rx.schedulers.Schedulers; | ||
import rx.subjects.PublishSubject; | ||
|
||
public class HotObservableWithoutBackpressure { | ||
public static void main(String[] args) throws InterruptedException { | ||
PublishSubject<Integer> source = PublishSubject.<Integer>create(); | ||
|
||
source.observeOn(Schedulers.computation()) | ||
.subscribe(ComputeFunction::compute, Throwable::printStackTrace); | ||
|
||
|
||
for (int i = 0; i < 1_000_000; i++) { | ||
source.onNext(i); | ||
} | ||
Thread.sleep(10_000); | ||
} | ||
} |