Remark : 처음 시작하기
1. gradle
1 2 3 |
implementation 'io.reactivex.rxjava2:rxjava:2.2.21' |
2.소스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.example.demo; import io.reactivex.Observable; import org.apache.catalina.LifecycleException; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.IOException; @SpringBootApplication public class DemoApplication { public static void main(String[] args) throws IOException, LifecycleException { //SpringApplication.run(DemoApplication.class, args); //System.out.println("test"); DemoApplication demo = new DemoApplication(); demo.emit(); } public void emit() { Observable.just("Hello", "RxJava :2.2.21!!") .subscribe(System.out::println); } } |
1 2 3 4 |
Hello RxJava :2.2.21!! |
예제2
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 69 70 71 |
package com.example.demo; import io.reactivex.Observable; import io.reactivex.ObservableEmitter; import io.reactivex.ObservableOnSubscribe; import io.reactivex.annotations.NonNull; import io.reactivex.functions.Action; import io.reactivex.functions.Consumer; import org.apache.catalina.LifecycleException; import org.json.simple.JSONObject; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.io.IOException; @SpringBootApplication public class DemoApplication { public static void main(String[] args) throws IOException, LifecycleException { //SpringApplication.run(DemoApplication.class, args); DemoApplication demo = new DemoApplication(); demo.emit(); } public void emit() { //Observable 생성 Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<Integer>() { @Override public void subscribe(@NonNull ObservableEmitter<Integer> emitter) { // 데이터 흐름 정의 emitter.onNext(1); emitter.onNext(2); emitter.onNext(3); emitter.onNext(4); emitter.onNext(5); emitter.onComplete(); // onComplete() 이후의 데이터는 발행되지 않음 emitter.onNext(10); } }); // subscribe 함수를 통해 실제로 데이터를 발행하여 소비함 observable.subscribe( // onNext new Consumer<Integer>() { @Override public void accept(Integer integer) { System.out.println("onNext : " + integer); } }, // onError new Consumer<Throwable>() { @Override public void accept(Throwable throwable) { System.out.println("onError : " + throwable); } }, // onComplete new Action() { @Override public void run() { System.out.println("onComplete"); } } ); } } |
1 2 3 4 5 6 7 8 |
onNext : 1 onNext : 2 onNext : 3 onNext : 4 onNext : 5 onComplete |
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 |
public void emit() { Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .subscribe(System.out::println); } // 10씩 곱하기 public void emit() { Observable.just(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) .map(x -> x * 10) .subscribe(System.out::println); } //직접 onNext, onComplete, onError를 호출 public void emit() { Observable.create(emitter -> { emitter.onNext(1); emitter.onNext(2); emitter.onNext(3); emitter.onComplete(); }).subscribe(System.out::println); } public void emit() { Integer [] array = {1, 2, 3, 4, 5}; Observable.fromArray(array) .subscribe(System.out::println); } //import java.util.ArrayList; public void emit() { ArrayList<Integer> arrayList = new ArrayList<>(); arrayList.add(1); arrayList.add(2); arrayList.add(3); Observable.fromIterable(arrayList) .subscribe(System.out::println); } |
1 2 3 4 5 6 7 8 9 |
1 2 3 10 20 30 |