-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample5.java
More file actions
69 lines (57 loc) · 2.23 KB
/
example5.java
File metadata and controls
69 lines (57 loc) · 2.23 KB
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
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;
/**
* Error handling example by reporting to the observer.
*/
public class example5 {
public static void main(String[] args){
//What and when our source will emmit values.
Observable.OnSubscribe<String> subscription = new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
Subscriber mySubscriber = subscriber;
try{
for(int i = 0 ; i < 50 ; i++){
if(!mySubscriber.isUnsubscribed()){
mySubscriber.onNext("Pushed value " + i);
}
//throw the error after emitting the i'th item.
if(i == 5){
throw new Throwable("Oops! Someone has pooped.");
}
}
if(!subscriber.isUnsubscribed()){
mySubscriber.onCompleted();
}
}catch (Throwable throwable){
mySubscriber.onError(throwable);
}
}
};
//Create an observable by passing our created OnSubscribe instance.
Observable createdObservable = Observable.create(subscription);
//Subscribe to our Observable or source to listen to events.
createdObservable.subscribe(
new Action1<String>() {
@Override
public void call(String s) {
System.out.println(s);
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
System.out.println("Oops! Someone has pooped.");
}
},
new Action0() {
@Override
public void call() {
System.out.println("No more values will be pushed.");
}
}
);
}
}