├── README.md
└── Reactive-Programming
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── basicsstrong
│ └── reactive
│ ├── section1
│ ├── Book.java
│ ├── CallBack.java
│ ├── CallBackDemo.java
│ ├── EndUser.java
│ ├── Observer.java
│ ├── ObserverDesignPattern.java
│ └── SubjectLibrary.java
│ ├── section2
│ └── HelloRxJava.java
│ ├── section3
│ ├── ColdObservables.java
│ ├── ConnectableObservable.java
│ ├── CreatingObservable.java
│ ├── CreatingObserver.java
│ ├── Disposing.java
│ ├── ObservableAndObserver.java
│ └── Variants.java
│ ├── section4
│ ├── Employee.java
│ ├── Operators.java
│ └── OperatorsInAction.java
│ ├── section5
│ ├── Ambiguous.java
│ ├── FlatMapConcatMap.java
│ ├── Grouping.java
│ ├── MergeAndConcat.java
│ └── ZipAndCombineLatest.java
│ ├── section6
│ ├── Caching.java
│ ├── Demo.java
│ ├── Replaying.java
│ ├── SubjectTypes.java
│ └── Subjects.java
│ ├── section7
│ ├── ComputationScheduler.java
│ ├── ConcurrencyInRxJava.java
│ ├── CustomScheduler.java
│ ├── IOScheduler.java
│ ├── NewThreadScheduler.java
│ ├── SingleScheduler.java
│ ├── SubscribeOn.java
│ └── TheFlatMap.java
│ ├── section8
│ ├── Buffering.java
│ ├── Switching.java
│ └── Throttling.java
│ └── section9
│ ├── Backpressure.java
│ ├── Conversion.java
│ └── FlowableCreation.java
└── test
└── java
└── Reactive_Programming
└── Reactive_Programming
└── AppTest.java
/README.md:
--------------------------------------------------------------------------------
1 | # reactive-programming
--------------------------------------------------------------------------------
/Reactive-Programming/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | Reactive-Programming
6 | Reactive-Programming
7 | 0.0.1-SNAPSHOT
8 | jar
9 |
10 | Reactive-Programming
11 | http://maven.apache.org
12 |
13 |
14 | UTF-8
15 |
16 |
17 |
18 |
19 | junit
20 | junit
21 | 3.8.1
22 | test
23 |
24 |
25 |
26 | io.reactivex.rxjava3
27 | rxjava
28 | 3.0.0-RC9
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section1/Book.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section1;
2 |
3 | import java.util.ArrayList;
4 |
5 |
6 | public class Book implements SubjectLibrary {
7 |
8 | private String name;
9 | private String type;
10 | private String author;
11 | private double price;
12 | private String inStock;
13 | private ArrayList obsList = new ArrayList();
14 |
15 | public Book(String name, String type, String author, double price, String inStock) {
16 | this.name = name;
17 | this.type = type;
18 | this.author = author;
19 | this.price = price;
20 | this.inStock = inStock;
21 | }
22 |
23 | public String getName() {
24 | return name;
25 | }
26 |
27 | public void setName(String name) {
28 | this.name = name;
29 | }
30 |
31 | public String getType() {
32 | return type;
33 | }
34 |
35 | public void setType(String type) {
36 | this.type = type;
37 | }
38 |
39 | public String getAuthor() {
40 | return author;
41 | }
42 |
43 | public void setAuthor(String author) {
44 | this.author = author;
45 | }
46 |
47 | public double getPrice() {
48 | return price;
49 | }
50 |
51 | public void setPrice(double price) {
52 | this.price = price;
53 | }
54 |
55 | public String getInStock() {
56 | return inStock;
57 | }
58 |
59 | public void setInStock(String inStock) {
60 | this.inStock = inStock;
61 | System.out.println("Availability changed from Sold out to Back in stock \n");
62 | notifyObserver();
63 | }
64 |
65 | public ArrayList getObsList() {
66 | return obsList;
67 | }
68 |
69 | public void setObsList(ArrayList obsList) {
70 | this.obsList = obsList;
71 | }
72 |
73 | @Override
74 | public void subscribeObserver(Observer ob) {
75 |
76 | obsList.add(ob);
77 | }
78 |
79 | @Override
80 | public void unsubscribeObserver(Observer ob) {
81 | obsList.remove(ob);
82 |
83 | }
84 |
85 | @Override
86 | public void notifyObserver() {
87 | System.out.println(
88 | "Book name : " +this.name +
89 | ",Book Type : "+ this.type +
90 | ",Price : " + this.price+
91 | ",Author : "+ this.author+
92 | ", is now "+this.inStock+". So, please notify all users.\n"
93 | );
94 |
95 | for(Observer o : obsList)
96 | {
97 | o.update(this.inStock);
98 | }
99 |
100 | }
101 |
102 | }
103 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section1/CallBack.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section1;
2 |
3 | public interface CallBack {
4 |
5 | void pushData(String data);
6 |
7 | void pushComplete();
8 |
9 | void pushError(Exception ex);
10 | }
11 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section1/CallBackDemo.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section1;
2 |
3 | public class CallBackDemo {
4 |
5 | public static void main(String[] args) throws InterruptedException {
6 |
7 | System.out.println("Main Thread is running");
8 |
9 | Runnable r = new Runnable() {
10 |
11 | @Override
12 | public void run() {
13 | new CallBackDemo().runningAsync(new CallBack() {
14 |
15 | @Override
16 | public void pushData(String data) {
17 | System.out.println("CallBack data :" + data);
18 | }
19 |
20 | @Override
21 | public void pushComplete() {
22 | System.out.println("Callback done !");
23 |
24 | }
25 |
26 | @Override
27 | public void pushError(Exception ex) {
28 | System.out.println("Callback Error called, Got an Exception :" + ex);
29 | }
30 |
31 |
32 |
33 | });
34 | }
35 |
36 | };
37 |
38 | Thread t = new Thread(r);
39 | t.start();
40 |
41 | Thread.sleep(2000);
42 |
43 | System.out.println("Main thread Completed!");
44 |
45 |
46 | }
47 |
48 | public void runningAsync(CallBack callback) {
49 | System.out.println("I am running in saperate thread");
50 | sleep(1000);
51 | callback.pushData("Data1");
52 | callback.pushData("Data2");
53 | callback.pushData("Data3");
54 |
55 | callback.pushError(new RuntimeException("Oops!"));
56 | callback.pushComplete();
57 |
58 | }
59 |
60 | private void sleep(int duration) {
61 | try {
62 | Thread.sleep(duration);
63 | } catch (InterruptedException e) {
64 | e.printStackTrace();
65 | }
66 |
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section1/EndUser.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section1;
2 |
3 | public class EndUser implements Observer {
4 |
5 | String name;
6 |
7 | EndUser(String name, SubjectLibrary subject) {
8 | this.name = name;
9 | subject.subscribeObserver(this);
10 | }
11 |
12 | public String getName() {
13 | return name;
14 | }
15 | public void setName(String name) {
16 | this.name = name;
17 | }
18 |
19 |
20 |
21 | @Override
22 | public void update(String avail) {
23 | System.out.println("Hello "+ name + "! we are glad to notify you that your book is now "+ avail);
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section1/Observer.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section1;
2 |
3 | public interface Observer {
4 |
5 | public void update(String avail);
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section1/ObserverDesignPattern.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section1;
2 |
3 | public class ObserverDesignPattern {
4 |
5 | public static void main(String[] args) {
6 |
7 | Book book = new Book("Goosebumps", "Horror", "Xyz", 200, "SoldOut");
8 |
9 | EndUser user1 = new EndUser("Bob", book);
10 | EndUser user2 = new EndUser("Rob", book);
11 |
12 | System.out.println(book.getInStock());
13 |
14 | book.setInStock("In Stock");
15 |
16 | }
17 |
18 | }
19 |
20 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section1/SubjectLibrary.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section1;
2 |
3 | public interface SubjectLibrary {
4 |
5 | public void subscribeObserver(Observer ob);
6 |
7 | public void unsubscribeObserver(Observer ob);
8 |
9 | public void notifyObserver();
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section2/HelloRxJava.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section2;
2 |
3 | import io.reactivex.rxjava3.core.Observable;
4 |
5 | public class HelloRxJava {
6 |
7 | public static void main(String[] args) {
8 |
9 | Observable source = Observable.create(
10 |
11 | e -> {
12 | e.onNext("Hello");
13 | e.onNext("RxJava");
14 | }
15 | );
16 |
17 |
18 | source.subscribe(e -> System.out.println("Observer 1 :"+e + " Thread Name : "+ Thread.currentThread().getName()));
19 |
20 | source.subscribe(e -> System.out.println("Observer 2 :"+e + " Thread Name : "+ Thread.currentThread().getName()));
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section3/ColdObservables.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section3;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import io.reactivex.rxjava3.core.Observable;
7 |
8 | public class ColdObservables {
9 |
10 | public static void main(String[] args) {
11 |
12 | List list = new ArrayList<>();
13 |
14 | list.add(16);
15 | list.add(17);
16 | list.add(18);
17 |
18 | Observable source = Observable.fromIterable(list);
19 |
20 | source.subscribe(System.out::println);
21 |
22 | list = getData(list);
23 |
24 |
25 | source.subscribe(System.out::println);
26 |
27 | }
28 |
29 | private static List getData(List list) {
30 | list.add(19);
31 | return list;
32 | }
33 |
34 |
35 | //Hot
36 |
37 |
38 |
39 | }
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section3/ConnectableObservable.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section3;
2 |
3 | import java.util.concurrent.TimeUnit;
4 |
5 | import io.reactivex.rxjava3.annotations.NonNull;
6 | import io.reactivex.rxjava3.core.Observable;
7 |
8 | public class ConnectableObservable {
9 |
10 | public static void main(String[] args) throws InterruptedException {
11 |
12 | io.reactivex.rxjava3.observables.@NonNull ConnectableObservable<@NonNull Long> source = Observable.interval(1,TimeUnit.SECONDS).publish();
13 |
14 | source.connect();
15 |
16 | source.subscribe(System.out::println);
17 |
18 | Thread.sleep(10000);
19 |
20 | source.subscribe(System.out::println);
21 |
22 | Thread.sleep(10000);
23 |
24 | }
25 | }
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section3/CreatingObservable.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section3;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | import io.reactivex.rxjava3.annotations.NonNull;
7 | import io.reactivex.rxjava3.core.Observable;
8 |
9 | public class CreatingObservable {
10 |
11 | public static void main(String[] args) {
12 |
13 | //create()
14 |
15 | Observable source = Observable.create(
16 |
17 | e ->{
18 | e.onNext(10);
19 | e.onNext(11);
20 | e.onNext(12);
21 | e.onComplete();
22 | }
23 | );
24 |
25 |
26 | source.subscribe(System.out::println);
27 |
28 |
29 | //just()
30 |
31 |
32 | Observable just = Observable.just(1,2,3);
33 |
34 | just.subscribe(System.out::println);
35 |
36 |
37 | //fromIterable
38 |
39 | //List list = List.of("Ram", "Shyam", "Mike");
40 | List list = new ArrayList<>();
41 | list.add("Ram");
42 | list.add("Shyam");
43 |
44 | @NonNull
45 | Observable fromIterable = Observable.fromIterable(list);
46 |
47 |
48 | list.add("Rahin");
49 |
50 | fromIterable.subscribe(System.out::println);
51 |
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section3/CreatingObserver.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section3;
2 |
3 | import io.reactivex.rxjava3.core.Observable;
4 |
5 | public class CreatingObserver {
6 |
7 | public static void main(String[] args) {
8 |
9 |
10 | Observable source = Observable.just("Orange", "Black", "Red");
11 |
12 | source.subscribe(i -> System.out.println(i), Throwable :: printStackTrace, () -> System.out.println("Completed!"));
13 |
14 | System.out.println();
15 |
16 | source.subscribe(i -> System.out.println(i), Throwable :: printStackTrace);
17 |
18 | System.out.println();
19 |
20 | source.subscribe(i -> System.out.println(i));
21 |
22 |
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section3/Disposing.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section3;
2 |
3 | import java.util.concurrent.TimeUnit;
4 |
5 | import io.reactivex.rxjava3.annotations.NonNull;
6 | import io.reactivex.rxjava3.core.Observable;
7 | import io.reactivex.rxjava3.disposables.CompositeDisposable;
8 | import io.reactivex.rxjava3.disposables.Disposable;
9 |
10 | public class Disposing {
11 |
12 | private static final CompositeDisposable disp = new CompositeDisposable();
13 |
14 | public static void main(String[] args) throws InterruptedException {
15 |
16 |
17 | @NonNull
18 | Observable src = Observable.interval(1, TimeUnit.SECONDS);
19 |
20 | @NonNull
21 | Disposable d1 = src.subscribe(e -> System.out.println("Observer 1 : "+ e));
22 | Disposable d2 = src.subscribe(e -> System.out.println("Observer 2 : "+ e));
23 |
24 | Thread.sleep(5000);
25 |
26 | disp.addAll(d1, d2);
27 |
28 | disp.dispose();
29 |
30 | Thread.sleep(10000);
31 |
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section3/ObservableAndObserver.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section3;
2 |
3 | import io.reactivex.rxjava3.annotations.NonNull;
4 | import io.reactivex.rxjava3.core.Observable;
5 | import io.reactivex.rxjava3.core.ObservableEmitter;
6 | import io.reactivex.rxjava3.core.ObservableOnSubscribe;
7 | import io.reactivex.rxjava3.core.Observer;
8 | import io.reactivex.rxjava3.disposables.Disposable;
9 | import io.reactivex.rxjava3.internal.operators.observable.ObservableCreate;
10 |
11 | public class ObservableAndObserver {
12 |
13 | public static void main(String[] args) {
14 |
15 | Observable source = new ObservableCreate(new ObservableOnSubscribe() {
16 |
17 | @Override
18 | public void subscribe(@NonNull ObservableEmitter emitter) throws Throwable {
19 | try {
20 | emitter.onNext(10);
21 | emitter.onNext(11);
22 | emitter.onComplete();
23 | }catch(Throwable t) {
24 | emitter.onError(t);
25 | }
26 |
27 | }
28 | });
29 |
30 |
31 | Observer observer = new Observer() {
32 |
33 | @Override
34 | public void onSubscribe(@NonNull Disposable d) {
35 | System.out.println(" Subscribed ");
36 |
37 | }
38 |
39 | @Override
40 | public void onNext(@NonNull Integer t) {
41 | System.out.println("On Next : " + t);
42 |
43 | }
44 |
45 | @Override
46 | public void onError(@NonNull Throwable e) {
47 | e.printStackTrace();
48 |
49 | }
50 |
51 | @Override
52 | public void onComplete() {
53 | System.out.println("Completed !");
54 |
55 | }
56 | };
57 |
58 |
59 | source.subscribe(observer);
60 |
61 |
62 | }
63 |
64 | }
65 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section3/Variants.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section3;
2 |
3 | import java.util.LinkedList;
4 | import java.util.Queue;
5 |
6 | import io.reactivex.rxjava3.core.Completable;
7 | import io.reactivex.rxjava3.core.Observable;
8 | import io.reactivex.rxjava3.core.Single;
9 |
10 | public class Variants {
11 |
12 | public static void main(String[] args) {
13 |
14 | Observable source = Observable.just("Alex", "Justin", "Jack");
15 | Observable source1 = Observable.empty();
16 |
17 |
18 |
19 |
20 | source1
21 | .first("Name")
22 | .subscribe(System.out::println);
23 |
24 | Single.just("Alex")
25 | .subscribe(System.out::println);
26 |
27 |
28 | source1
29 | .firstElement()
30 | .subscribe(System.out::println, e-> e.printStackTrace(), () -> System.out.println("Completed"));
31 |
32 | Completable completable = Completable.complete();
33 |
34 | System.out.println();
35 |
36 | completable.subscribe(() -> System.out.println("Completed"));
37 |
38 | Completable
39 | .fromRunnable(() -> System.out.println("Some process executing"))
40 | .subscribe(()-> System.out.println("The process executed succesfully"));
41 |
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section4/Employee.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section4;
2 |
3 | public class Employee {
4 |
5 | private Integer id;
6 | private String name;
7 | double salary,rating;
8 |
9 | public Employee(Integer id, String name, double salary, double rating) {
10 | this.id = id;
11 | this.name = name;
12 | this.salary = salary;
13 | this.rating = rating;
14 | }
15 | public Integer getId() {
16 | return id;
17 | }
18 | public void setId(Integer id) {
19 | this.id = id;
20 | }
21 | public String getName() {
22 | return name;
23 | }
24 | public void setName(String name) {
25 | this.name = name;
26 | }
27 | public double getSalary() {
28 | return salary;
29 | }
30 | public void setSalary(double salary) {
31 | this.salary = salary;
32 | }
33 | public double getRating() {
34 | return rating;
35 | }
36 | public void setRating(double rating) {
37 | this.rating = rating;
38 | }
39 | @Override
40 | public String toString() {
41 | return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + ", rating=" + rating + "]";
42 | }
43 |
44 |
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section4/Operators.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section4;
2 |
3 | import io.reactivex.rxjava3.core.Observable;
4 |
5 | public class Operators {
6 |
7 | public static void main(String[] args) {
8 |
9 | Observable.just(60,57,89,90,23, 100, 98)
10 | .filter(e -> e > 75)
11 | .sorted()
12 | .subscribe(e -> System.out.println("Grade A with "+ e));
13 |
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section4/OperatorsInAction.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section4;
2 |
3 | import java.util.List;
4 |
5 | import io.reactivex.rxjava3.core.Observable;
6 |
7 | public class OperatorsInAction {
8 |
9 | public static void main(String[] args) {
10 |
11 | Observable obs = Observable.just(
12 | new Employee(101,"Alexa",60000,4.0),
13 | new Employee(123,"Dhwanit",94000,4.7),
14 | new Employee(236,"Mike",65000,4.0),
15 | new Employee(155,"Ella",85000,4.4),
16 | new Employee(443,"George",50000,3.6),
17 | new Employee(127,"Shreeja",85000,4.5),
18 | new Employee(509,"Daniel",60000,4.0),
19 | new Employee(344,"Lucy",94000,4.7),
20 | new Employee(509,"Harry",75000,4.3),
21 | new Employee(344,"Emma",55000,3.7)
22 | );
23 |
24 | obs
25 | .filter(e -> e.getRating() > 4.0)
26 | .sorted((e1, e2) -> Double.compare(e2.getRating(), e1.getRating()))
27 | .map(e -> e.getName())
28 | .take(4)
29 | //.toList()
30 | .subscribe(System.out::println);
31 |
32 |
33 | List expenses = List.of(200,500,300,340,129,234,999,1030,3400,890,996,789);
34 |
35 | Observable.fromIterable(expenses)
36 | .reduce((a,b)-> a + b)
37 | .subscribe(System.out::println);
38 |
39 |
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section5/Ambiguous.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section5;
2 |
3 | import java.util.Arrays;
4 | import java.util.concurrent.TimeUnit;
5 |
6 | import io.reactivex.rxjava3.core.Observable;
7 |
8 | public class Ambiguous{
9 |
10 | public static void main(String[] args) throws InterruptedException {
11 |
12 | Observable ob1 = Observable.interval(1, TimeUnit.SECONDS).take(10)
13 | .map(e -> "Ob 1 :"+e);
14 | Observable ob2 = Observable.interval(10, TimeUnit.MILLISECONDS).take(10)
15 | .map(e -> "Ob 2 :"+e);
16 |
17 | Observable.amb(Arrays.asList(ob1,ob2))
18 | .subscribe(System.out::println);
19 |
20 | Thread.sleep(11000);
21 |
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section5/FlatMapConcatMap.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section5;
2 |
3 | import java.util.List;
4 |
5 | import io.reactivex.rxjava3.core.Observable;
6 |
7 | public class FlatMapConcatMap {
8 |
9 | public static void main(String[] args) {
10 |
11 | List list = List.of("Hello","Reactive","Programming");
12 |
13 | Observable.fromIterable(list)
14 | .concatMap(e -> Observable.fromArray(e.split("")))
15 | .subscribe(System.out::println);
16 |
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section5/Grouping.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section5;
2 |
3 | import com.basicsstrong.reactive.section4.Employee;
4 |
5 | import io.reactivex.rxjava3.core.Observable;
6 |
7 | public class Grouping {
8 |
9 | public static void main(String[] args) {
10 |
11 | Observable obs = Observable.just(
12 | new Employee(101,"Alexa",60000,4.0),
13 | new Employee(123,"Dhwanit",94000,4.7),
14 | new Employee(236,"Mike",65000,4.0),
15 | new Employee(155,"Ella",85000,4.4),
16 | new Employee(443,"George",50000,3.6),
17 | new Employee(127,"Shreeja",85000,4.5),
18 | new Employee(509,"Daniel",60000,4.0),
19 | new Employee(344,"Lucy",94000,4.7),
20 | new Employee(509,"Harry",75000,4.3),
21 | new Employee(344,"Emma",55000,3.7)
22 | );
23 |
24 | obs.groupBy(e -> e.getRating())
25 | .flatMapSingle(e -> e.toList())
26 | .subscribe(System.out::println);
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section5/MergeAndConcat.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section5;
2 |
3 | import java.util.concurrent.TimeUnit;
4 |
5 | import io.reactivex.rxjava3.core.Observable;
6 |
7 | public class MergeAndConcat {
8 |
9 | public static void main(String[] args) throws InterruptedException {
10 |
11 | //
12 | // Observable src1 = Observable.just("Ella","Alexa","Lily");
13 | // Observable src2 = Observable.just("Priya","Chloe");
14 |
15 | Observable src1 = Observable.interval(1, TimeUnit.SECONDS).map(e -> "src1 : "+e);
16 | Observable src2 = Observable.interval(1, TimeUnit.SECONDS).map(e -> "src2 : "+e);
17 | // Observable.concat(src1, src2)
18 | // .subscribe(e -> System.out.println("Received : "+ e));
19 |
20 | src1.concatWith(src2)
21 | .subscribe(e -> System.out.println("Received : "+ e));
22 |
23 | Thread.sleep(20000);
24 |
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section5/ZipAndCombineLatest.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section5;
2 |
3 | import java.util.concurrent.TimeUnit;
4 |
5 | import io.reactivex.rxjava3.core.Observable;
6 |
7 | public class ZipAndCombineLatest {
8 |
9 | public static void main(String[] args) throws InterruptedException {
10 |
11 | Observable src1 = Observable.interval(200, TimeUnit.MILLISECONDS).take(10);
12 |
13 | Observable src2 = Observable.interval(1, TimeUnit.SECONDS).take(10);
14 |
15 | Observable.combineLatest(src1, src2, (e1, e2) -> "Source 1 : "+e1+" Source 2: " + e2)
16 | .subscribe(System.out::println);
17 |
18 | Thread.sleep(20000);
19 |
20 |
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section6/Caching.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section6;
2 |
3 | import java.util.concurrent.TimeUnit;
4 |
5 | import io.reactivex.rxjava3.annotations.NonNull;
6 | import io.reactivex.rxjava3.core.Observable;
7 |
8 | public class Caching {
9 |
10 | public static void main(String[] args) throws InterruptedException {
11 |
12 | @NonNull
13 | Observable<@NonNull Long> src = Observable.interval(1, TimeUnit.SECONDS)
14 | .cache();
15 |
16 | src.subscribe(e -> System.out.println("Observer 1 : "+e));
17 |
18 | Thread.sleep(5000);
19 |
20 | src.subscribe(e -> System.out.println("Observer 2 : "+e));
21 |
22 | Thread.sleep(3000);
23 |
24 | }
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section6/Demo.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section6;
2 |
3 | import io.reactivex.rxjava3.annotations.NonNull;
4 | import io.reactivex.rxjava3.subjects.PublishSubject;
5 | import io.reactivex.rxjava3.subjects.Subject;
6 |
7 | public class Demo {
8 |
9 | public static void main(String[] args) {
10 |
11 | @NonNull
12 | PublishSubject subject = PublishSubject.create();
13 | @NonNull
14 | Subject serialized = subject.toSerialized(); //Thread safe
15 |
16 |
17 |
18 | serialized.subscribe(System.out::println);
19 | serialized.subscribe(e -> System.out.println("Observer 2 "+ e));
20 |
21 | serialized.onNext("Hello");
22 | serialized.onNext("BasicsStrong");
23 | serialized.onComplete();
24 | serialized.onNext("BasicsStrong");
25 |
26 |
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section6/Replaying.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section6;
2 |
3 | import java.util.concurrent.TimeUnit;
4 |
5 | import io.reactivex.rxjava3.annotations.NonNull;
6 | import io.reactivex.rxjava3.core.Observable;
7 |
8 | public class Replaying {
9 |
10 | public static void main(String[] args) throws InterruptedException {
11 |
12 |
13 | @NonNull
14 | Observable<@NonNull Long> src = Observable.interval(1, TimeUnit.SECONDS)
15 | .replay(2,4,TimeUnit.SECONDS)
16 | .autoConnect();
17 |
18 | src.subscribe(e -> System.out.println("Observer 1 : "+e));
19 |
20 | Thread.sleep(5000);
21 |
22 | src.subscribe(e -> System.out.println("Observer 2 : "+e));
23 |
24 | Thread.sleep(3000);
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section6/SubjectTypes.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section6;
2 |
3 | import java.util.concurrent.TimeUnit;
4 |
5 | import io.reactivex.rxjava3.core.Observable;
6 | import io.reactivex.rxjava3.subjects.Subject;
7 | import io.reactivex.rxjava3.subjects.UnicastSubject;
8 |
9 | public class SubjectTypes {
10 |
11 | public static void main(String[] args) throws InterruptedException {
12 |
13 | Subject subject = UnicastSubject.create();
14 |
15 | Observable.interval(500, TimeUnit.MILLISECONDS)
16 | .subscribe(subject);
17 |
18 | Thread.sleep(2000);
19 |
20 |
21 | subject.subscribe(e -> System.out.println("Subscriber 1: "+ e));
22 |
23 | Thread.sleep(2000);
24 |
25 | // subject.onNext("a");
26 | // subject.onNext("b");
27 | // subject.onNext("c");
28 | //
29 | // //subject.subscribe(e -> System.out.println("Subscriber 2: "+ e));
30 | //
31 | // subject.onNext("d");
32 | // subject.onNext("e");
33 | // subject.onComplete();
34 |
35 |
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/Reactive-Programming/src/main/java/com/basicsstrong/reactive/section6/Subjects.java:
--------------------------------------------------------------------------------
1 | package com.basicsstrong.reactive.section6;
2 |
3 | import io.reactivex.rxjava3.annotations.NonNull;
4 | import io.reactivex.rxjava3.core.Observable;
5 | import io.reactivex.rxjava3.schedulers.Schedulers;
6 | import io.reactivex.rxjava3.subjects.PublishSubject;
7 | import io.reactivex.rxjava3.subjects.Subject;
8 |
9 | public class Subjects {
10 |
11 | public static void main(String[] args) throws InterruptedException {
12 |
13 | @NonNull
14 | Observable src1 = Observable.just(5,10,15,20)
15 | .subscribeOn(Schedulers.computation());
16 |
17 | Observable src2 = Observable.just(50,100,150,200)
18 | .subscribeOn(Schedulers.computation());
19 |
20 | // src1.subscribe(e -> System.out.println(e));
21 | // src2.subscribe(e -> System.out.println(e));
22 |
23 | @NonNull
24 | Subject