forked from OpenFeign/feign
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserver.java
More file actions
68 lines (65 loc) · 2.1 KB
/
Observer.java
File metadata and controls
68 lines (65 loc) · 2.1 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
package feign;
/**
* An {@code Observer} is asynchronous equivalent to an {@code Iterator}.
* <p/>
* Observers receive results as they are
* {@link feign.codec.IncrementalDecoder decoded} from an
* {@link Response.Body http response body}. {@link #onNext(Object) onNext}
* will be called for each incremental value of type {@code T} until
* {@link feign.Subscription#unsubscribe()} is called or the response is finished.
* <br>
* {@link #onSuccess() onSuccess} or {@link #onFailure(Throwable)} onFailure}
* will be called when the response is finished, but not both.
* <br>
* {@code Observer} can be used as an asynchronous alternative to a
* {@code Collection}, or any other use where iterative response parsing is
* worth the additional effort to implement this interface.
* <br>
* <br>
* Here's an example of implementing {@code Observer}:
* <br>
* <pre>
* Observer<Contributor> counter = new Observer<Contributor>() {
*
* public int count;
*
* @Override public void onNext(Contributor element) {
* count++;
* }
*
* @Override public void onSuccess() {
* System.out.println("found " + count + " contributors");
* }
*
* @Override public void onFailure(Throwable cause) {
* System.err.println("sad face after contributor " + count);
* }
* };
* subscription = github.contributors("netflix", "feign", counter);
* </pre>
*
* @param <T> expected value to decode incrementally from the http response.
*/
public interface Observer<T> {
/**
* Invoked as soon as new data is available. Could be invoked many times or
* not at all.
*
* @param element next decoded element.
*/
void onNext(T element);
/**
* Called when response processing completed successfully.
*/
void onSuccess();
/**
* Called when response processing failed for any reason.
* <br>
* Common failure cases include {@link FeignException},
* {@link java.io.IOException}, and {@link feign.codec.DecodeException}.
* However, the cause could be a {@code Throwable} of any kind.
*
* @param cause the reason for the failure
*/
void onFailure(Throwable cause);
}