반응형
3. fromCallable
fromCallable은 java.util.concurrent.Callable 함수형 인터페이스에서 생성한 데이터를 통지하는 연산자로 인자로 지정한 Callable의 반환 값을 데이터로 탐지하고, 작업을 마치면 완료 통지 Flowable/Observable을 생성. 통지하는 데이터는 호출될 때마다 새로 생성.
[코드]
public class Main {
public static void main(String[] args) {
formCallble();
}
static void formCallble() {
Flowable<Long> flowable = Flowable.fromCallable(() -> System.currentTimeMillis());
flowable.subscribe(new PrintSubscriber<>());
}
}
[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 : 1564377950141
main : Complete
반응형
'Language > Java' 카테고리의 다른 글
[RxJava] interval (0) | 2023.04.26 |
---|---|
[RxJava] range, rangeLong (0) | 2023.04.26 |
[RxJava] fromArray, fromIterable (0) | 2023.04.26 |
[RxJava] Just (0) | 2023.04.26 |
[Reactive Programming] RxJava 리소스 관리 (0) | 2023.04.26 |