forked from politrons/reactive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HotObservable.java
240 lines (213 loc) · 9.82 KB
/
HotObservable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package rx.observables.connectable;
import akka.dispatch.ExecutionContexts;
import akka.dispatch.Futures;
import akka.dispatch.OnComplete;
import org.junit.Test;
import rx.Observable;
import rx.Subscription;
import rx.observables.ConnectableObservable;
import rx.subjects.AsyncSubject;
import rx.subjects.PublishSubject;
import rx.subjects.ReplaySubject;
import rx.subjects.Subject;
import scala.Function1;
import scala.concurrent.ExecutionContextExecutorService;
import scala.concurrent.Future;
import scala.runtime.AbstractFunction1;
import scala.runtime.BoxedUnit;
import scala.util.Try;
import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.Executors.*;
/**
* Hot Observables are Observables that allow start emitting when we connect to the publisher
* or features as retry items emitted through the pipeline to new observers subscribed.
*/
public class HotObservable {
/**
* This example we can see how constantClass third observer subscribe to hot Observable once this one has start emitting items,
* Since the hot observable was created with publish he miss the items already emitted
*
* @throws InterruptedException
*/
@Test
public void testHotObservablesMissingItems() throws InterruptedException {
Observable<Long> interval = Observable.interval(100L, TimeUnit.MILLISECONDS);
ConnectableObservable<Long> published = interval.publish();
subscribeToObservable(published, "First");
subscribeToObservable(published, "Second");
published.connect();
subscribeToObservableWithDelay(published);
}
/**
* This example we can see how constantClass third observer subscribe to hot Observable once start emitting items, and because the hot
* observable was created with replay, it replay to the third observer all missed items.
*
* @throws InterruptedException
*/
@Test
public void testHotObservablesReplayingMissItems() throws InterruptedException {
Observable<Long> interval = Observable.interval(100L, TimeUnit.MILLISECONDS);
ConnectableObservable<Long> published = interval.replay();
subscribeToObservable(published, "First");
subscribeToObservable(published, "Second");
published.connect();
subscribeToObservableWithDelay(published);
}
/**
* In this example we see how using hot observables PublishSubject we can emit an item on broadcast to all the observers(subscribers).
*
* @throws InterruptedException
*/
@Test
public void testHotObservableUsingPublishSubject() throws InterruptedException {
Observable<Long> interval = Observable.interval(100L, TimeUnit.MILLISECONDS);
Subject<Long, Long> publishSubject = PublishSubject.create();
interval.subscribe(publishSubject);
subscribeToObservable(publishSubject, "First");
subscribeToObservable(publishSubject, "Second");
try {
Thread.sleep(300L);
publishSubject.onNext(555L);
subscribeToObservable(publishSubject, "Third");
Thread.sleep(500L);
} catch (InterruptedException e) {
}
}
/**
* In this example we see how using hot observables ConnectableObservables we can start emitting items not when we subscribe, but when we connect.
*
* @throws InterruptedException
*/
@Test
public void testHotObservableConnectableObservables() throws InterruptedException {
Long startTime = System.currentTimeMillis();
Observable<String> observable = Observable.just("Hot observable");
ConnectableObservable<String> published = observable.publish();
published.subscribe(s -> System.out.println(String.format("Item %s Emitted after: %s seconds", s, (System.currentTimeMillis() - startTime) / 1000)),
e -> System.out.println(e.getMessage()));
Thread.sleep(1000);
published.connect();
}
/**
* In this example we see how using hot observables PublishSubject we can start emitting items not when we subscribe,
* but when we subscribe the observer to the observable.
*
* @throws InterruptedException
*/
@Test
public void testHotObservablePublishSubject() throws InterruptedException {
Long startTime = System.currentTimeMillis();
Observable<String> observable = Observable.just("Hot observable");
PublishSubject<String> publishSubject = PublishSubject.create();
publishSubject.subscribe(s -> System.out.println(
String.format("Item %s Emitted in publish subject after: %s seconds", s, (System.currentTimeMillis() - startTime) / 1000)));
Thread.sleep(1000);
observable.subscribe(publishSubject);
}
/**
* In this example we see how using hot observables ReplaySubject we can emit an item on broadcast to all the observers(subscribers).
*
* @throws InterruptedException
*/
@Test
public void testHotObservableUsingAsyncSubject() throws InterruptedException {
Observable<Long> interval = Observable.interval(100L, TimeUnit.MILLISECONDS);
Subject<Long, Long> publishSubject = AsyncSubject.create();
interval.subscribe(publishSubject);
Thread.sleep(1000L);
subscribeToObservable(publishSubject, "First");
subscribeToObservable(publishSubject, "Second");
subscribeToObservable(publishSubject, "Third");
}
/**
* In this example we see how using hot observables ReplaySubject we can emit an item on broadcast to all the observers(subscribers).
*
* @throws InterruptedException
*/
@Test
public void testHotObservableUsingReplaySubject() throws InterruptedException {
Observable<Long> interval = Observable.interval(100L, TimeUnit.MILLISECONDS);
Subject<Long, Long> publishSubject = ReplaySubject.create(1);
interval.subscribe(publishSubject);
Thread.sleep(1000L);
subscribeToObservable(publishSubject, "First");
subscribeToObservable(publishSubject, "Second");
subscribeToObservable(publishSubject, "Third");
}
/**
* In this example we see how using hot observables ReplaySubject we can emit an item on broadcast to all the observers(subscribers).
*
* @throws InterruptedException
*/
@Test
public void testHotObservableUsingReplaySubject2() throws InterruptedException {
Observable<Long> interval = Observable.interval(100L, TimeUnit.MILLISECONDS);
Subject<Long, Long> publishSubject = ReplaySubject.create(1);
interval.subscribe(publishSubject);
Thread.sleep(1000L);
publishSubject.subscribe(System.out::println, (e) -> System.err.println(e.getMessage()), System.out::println);
}
private Subscription subscribeToObservableWithDelay(ConnectableObservable<Long> published) {
Subscription sub3 = null;
try {
Thread.sleep(500L);
sub3 = subscribeToObservable(published, "Third");
Thread.sleep(500L);
} catch (InterruptedException e) {
}
return sub3;
}
Subscription subscribeToObservable(Observable<Long> observable, String name) {
return observable.subscribe((v) -> System.out.println(name + " : " + v), (e) -> {
System.err.println("Error from " + name + ":");
System.err.println(e.getMessage());
}, () -> System.out.println(name + " ended!"));
}
/**
* In this example we see how using hot observables ReplaySubject we can emit an item on broadcast to all the observers(subscribers).
*
* @throws InterruptedException
*/
@Test
public void chainObservables() throws InterruptedException {
Observable<Long> interval = Observable.just(1l);
Subject<Long, Long> publishSubject = ReplaySubject.create(1);
Subject<Long, Long> publishSubject2 = ReplaySubject.create(1);
interval.subscribe(publishSubject);
publishSubject.subscribe(publishSubject2);
publishSubject2.subscribe(System.out::println, (e) -> System.err.println(e.getMessage()), System.out::println);
}
@Test
public void createObservableFromFuture() throws InterruptedException {
Observable<String> observableFuture = getObservableFuture();
Observable<String> observableFuture1 = getObservableFuture();
Observable.zip(observableFuture, observableFuture1, (o, o1) -> o.concat(":").concat(o1))
.subscribe(value -> System.out.println("Result:" + value),
System.out::println, () -> System.out.println("end"));
}
private Observable<String> getObservableFuture() {
ExecutionContextExecutorService executionContextExecutorService = ExecutionContexts.fromExecutorService(newCachedThreadPool());
ReplaySubject<String> publishSubject = ReplaySubject.create(1);
Future<String> future = Futures.<String>promise().success("works").future();
future.onComplete(getFunction(publishSubject), executionContextExecutorService);
return publishSubject.first().single();
}
private OnComplete getFunction(ReplaySubject<String> publishSubject) {
return new OnComplete() {
@Override
public void onComplete(Throwable failure, Object success) throws Throwable {
Observable<String> observable = Observable.just((String) success);
observable.subscribe(publishSubject);
}
};
}
@Test
public void chainObservablesWithSubject() throws InterruptedException {
Observable<Long> observable = Observable.from(Arrays.asList(1l, 2l, 3l, 4l));
Subject<Long, Long> chainObservable = ReplaySubject.create(1);
observable.subscribe(chainObservable);
chainObservable.subscribe(System.out::println, (e) -> System.err.println(e.getMessage()), System.out::println);
}
}