반응형
1. just()
인자(arguments)의 데이터를 통지하는 Flowable/Observable 생성하는 연산자로 최대 10개 까지 정할 수 있고, 모든 데이터를 통지하면 완료(onComplete)를 통지Flowable/Observable을 생성한다.
[소스코드]
import io.reactivex.Flowable;
public class Main {
public static void main(String[] args) {
just();
}
static void just() {
Flowable<String> flowable = Flowable.just("A", "B", "C", "D", "E");
flowable.subscribe(new PrintSubscriber<String>());
}
}
[Subscribe 코드]
import io.reactivex.subscribers.DisposableSubscriber;
public class PrintSubscriber<T> extends DisposableSubscriber<T> {
private String label;
public PrintSubscriber() {
}
public PrintSubscriber(String label) {
this.label = label;
}
@Override
public void onNext(T data) {
String threadName = Thread.currentThread().getName();
String resultMsg = label == null ? (threadName + " : " + data) : (threadName + " : "+ label + " : "+ data);
System.out.println(resultMsg);
}
@Override
public void onError(Throwable throwable) {
String threadName = Thread.currentThread().getName();
String resultMsg = label == null ? (threadName + " : Exception=" + throwable) : (threadName + " : "+ label + " : "+ throwable);
System.out.println(resultMsg);
}
@Override
public void onComplete() {
String threadName = Thread.currentThread().getName();
String resultMsg = label == null ? (threadName + " : Complete") : (threadName + " : "+ label + " : Complete");
System.out.println(resultMsg);
}
}
[결과]
main : A
main : B
main : C
main : D
main : E
main : Complete
반응형
'Language > Java' 카테고리의 다른 글
[RxJava] fromCallable (0) | 2023.04.26 |
---|---|
[RxJava] fromArray, fromIterable (0) | 2023.04.26 |
[Reactive Programming] RxJava 리소스 관리 (0) | 2023.04.26 |
[Reactive Programming] RxJava 예외 처리 (0) | 2023.04.26 |
[Reactive Programming] RxJava 비동기 처리 (0) | 2023.04.26 |