Skip to content

Commit 4151646

Browse files
author
Kaushik Gopal
committed
feat: add Timeout example to README
1 parent 9a5f53b commit 4151646

File tree

5 files changed

+30
-11
lines changed

5 files changed

+30
-11
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ I've also been giving talks about Learning Rx using many of the examples listed
2626
13. [Networking with Volley](https://github.com/kaushikgopal/RxJava-Android-Samples/blob/master/README.md#13-networking-with-volley)
2727
14. [Pagination with Rx (using Subjects)](https://github.com/kaushikgopal/RxJava-Android-Samples/blob/master/README.md#14-pagination-with-rx-using-subjects)
2828
15. [Orchestrating Observable: make parallel network calls, then combine the result into a single data point (using flatmap & zip)](https://github.com/kaushikgopal/RxJava-Android-Samples/blob/master/README.md#15-orchestrating-observable-make-parallel-network-calls-then-combine-the-result-into-a-single-data-point-using-flatmap--zip)
29+
16. [Simple Timeout example (using timeout)]()
2930

3031
## Description
3132

@@ -215,6 +216,12 @@ The below ascii diagram expresses the intention of our next example with panache
215216

216217
The code for this example has already been written by one Mr.skehlet in the interwebs. Head over to [the gist](https://gist.github.com/skehlet/9418379) for the code. It's written in pure Java (6) so it's pretty comprehensible if you've understood the previous examples. I'll flush it out here again when time permits or I've run out of other compelling examples.
217218

219+
### 16. Simple Timeout example (using timeout)
220+
221+
This is a simple example demonstrating the use of the `.timeout` operator. Button 1 will complete the task before the timeout constraint, while Button 2 will force a timeout error.
222+
223+
Notice how we can provide a custom Observable that indicates how to react under a timeout Exception.
224+
218225
## Work in Progress:
219226

220227
_Examples that I would like to have here, but haven't found the time yet to flush out_

app/src/main/java/com/morihacky/android/rxjava/fragments/MainFragment.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ void demoTimerIntervalDelays() {
8282
clickedOn(new TimingDemoFragment());
8383
}
8484

85+
@OnClick(R.id.btn_demo_timeout)
86+
void demoTimeout() {
87+
clickedOn(new TimeoutDemoFragment());
88+
}
89+
8590
@OnClick(R.id.btn_demo_exponential_backoff)
8691
void demoExponentialBackoff() {
8792
clickedOn(new ExponentialBackoffFragment());

app/src/main/java/com/morihacky/android/rxjava/fragments/TimeoutDemoFragment.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public void onStart2sTask() {
6464
_disposable = _getEventCompletionObserver();
6565

6666
_getObservableTask_2sToComplete()
67+
.timeout(3, TimeUnit.SECONDS)
68+
.subscribeOn(Schedulers.computation())
6769
.observeOn(AndroidSchedulers.mainThread())
6870
.subscribe(_disposable);
6971
}
@@ -72,8 +74,8 @@ public void onStart2sTask() {
7274
public void onStart5sTask() {
7375
_disposable = _getEventCompletionObserver();
7476

75-
_getObservableFor5sTask()
76-
.timeout(2, TimeUnit.SECONDS, _getTimeoutObservable())
77+
_getObservableTask_5sToComplete()
78+
.timeout(3, TimeUnit.SECONDS, _onTimeoutObservable())
7779
.subscribeOn(Schedulers.computation())
7880
.observeOn(AndroidSchedulers.mainThread())
7981
.subscribe(_disposable);
@@ -82,14 +84,14 @@ public void onStart5sTask() {
8284
// -----------------------------------------------------------------------------------
8385
// Main Rx entities
8486

85-
private Observable<String> _getObservableFor5sTask() {
87+
private Observable<String> _getObservableTask_5sToComplete() {
8688
return Observable.create(new ObservableOnSubscribe<String>() {
8789
@Override
8890
public void subscribe(ObservableEmitter<String> subscriber) throws Exception {
8991
_log(String.format("Starting a 5s task"));
9092
subscriber.onNext("5 s");
9193
try {
92-
Thread.sleep(1200);
94+
Thread.sleep(5_000);
9395
} catch (InterruptedException e) {
9496
e.printStackTrace();
9597
}
@@ -106,24 +108,22 @@ public void subscribe(ObservableEmitter<String> subscriber) throws Exception {
106108
_log(String.format("Starting a 2s task"));
107109
subscriber.onNext("2 s");
108110
try {
109-
Thread.sleep(2000);
111+
Thread.sleep(2_000);
110112
} catch (InterruptedException e) {
111113
e.printStackTrace();
112114
}
113115
subscriber.onComplete();
114116
}
115-
})
116-
.subscribeOn(Schedulers.computation())
117-
.timeout(3, TimeUnit.SECONDS);
117+
});
118118
}
119119

120-
private Observable<? extends String> _getTimeoutObservable() {
120+
private Observable<? extends String> _onTimeoutObservable() {
121121
return Observable.create(new ObservableOnSubscribe<String>() {
122122

123123
@Override
124124
public void subscribe(ObservableEmitter<String> subscriber) throws Exception {
125125
_log("Timing out this task ...");
126-
subscriber.onComplete();
126+
subscriber.onError(new Throwable("Timeout Error"));
127127
}
128128
});
129129
}

app/src/main/res/layout/fragment_main.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@
8686
android:text="@string/btn_demo_timing"
8787
/>
8888

89+
<Button
90+
android:id="@+id/btn_demo_timeout"
91+
android:layout_height="wrap_content"
92+
android:layout_width="match_parent"
93+
android:text="@string/btn_demo_timeout"
94+
/>
95+
8996
<Button
9097
android:id="@+id/btn_demo_exponential_backoff"
9198
android:layout_height="wrap_content"

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<string name="btn_demo_schedulers">bg work (schedulers &amp; concurrency)</string>
1010
<string name="btn_demo_buffer">accumulate calls (buffer)</string>
1111
<string name="btn_demo_debounce">search text listener(debounce)</string>
12-
<string name="btn_demo_timeout">delayed jobs (timeout)</string>
12+
<string name="btn_demo_timeout">Timeout long running jobs</string>
1313
<string name="btn_demo_retrofit">Retrofit + RxJava</string>
1414
<string name="btn_demo_double_binding_textview">Double binding (PublishSubject)</string>
1515
<string name="btn_demo_polling">Polling with RxJava</string>

0 commit comments

Comments
 (0)