거꾸로 바라본 세상
Published 2023. 4. 26. 09:20
[RxJava] Just Language/Java
반응형

1. just()

인자(arguments)의 데이터를 통지하는 Flowable/Observable 생성하는 연산자로 최대 10개 까지 정할 수 있고, 모든 데이터를 통지하면 완료(onComplete)를 통지Flowable/Observable을 생성한다.

just

[소스코드]


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
반응형
profile

거꾸로 바라본 세상

@란지에。

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!