├── 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 subject = PublishSubject.create(); 25 | 26 | subject.subscribe(e -> System.out.println(e)); //Observer 1 27 | 28 | subject.onNext("Hello"); 29 | subject.onComplete(); 30 | subject.onNext("BasicsStrong"); 31 | 32 | //subject.subscribe(e -> System.out.println("The element is "+ e)); //Observer 2 33 | 34 | src1.subscribe(subject); //source 1 35 | src2.subscribe(subject); //source 2 36 | 37 | Thread.sleep(9000); 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section7/ComputationScheduler.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section7; 2 | 3 | import io.reactivex.rxjava3.core.Observable; 4 | import io.reactivex.rxjava3.schedulers.Schedulers; 5 | 6 | public class ComputationScheduler { 7 | 8 | public static void main(String[] args) throws InterruptedException { 9 | 10 | Observable src = Observable.just("Pasta", "Pizza", "Fries", "Curry", "Chow mein") 11 | .subscribeOn(Schedulers.computation()); 12 | 13 | src.subscribe(e -> compute()); 14 | src.subscribe(e -> compute()); 15 | src.subscribe(e -> compute()); 16 | src.subscribe(e -> compute()); 17 | src.subscribe(e -> compute()); 18 | 19 | Thread.sleep(50000); 20 | 21 | } 22 | 23 | 24 | public static void compute() throws InterruptedException { 25 | 26 | Thread.sleep(1000); 27 | System.out.println("Computation Done By : "+ Thread.currentThread().getName()); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section7/ConcurrencyInRxJava.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section7; 2 | 3 | import io.reactivex.rxjava3.core.Observable; 4 | 5 | public class ConcurrencyInRxJava { 6 | 7 | public static void main(String[] args) { 8 | 9 | Observable source = Observable.create( 10 | 11 | e -> { 12 | new Thread( () -> 13 | { 14 | e.onNext("Hello"); 15 | e.onNext("RxJava"); 16 | } 17 | ).start(); 18 | } 19 | ); 20 | 21 | source 22 | .subscribe(e -> System.out.println("Observer 1 :"+e + " Thread is :" +Thread.currentThread().getName())); 23 | 24 | source 25 | .subscribe(e -> System.out.println("Observer 2 :"+e+ " Thread is :" +Thread.currentThread().getName())); 26 | 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section7/CustomScheduler.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section7; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | import io.reactivex.rxjava3.annotations.NonNull; 7 | import io.reactivex.rxjava3.core.Observable; 8 | import io.reactivex.rxjava3.core.Scheduler; 9 | import io.reactivex.rxjava3.schedulers.Schedulers; 10 | 11 | public class CustomScheduler { 12 | 13 | public static void main(String[] args) throws InterruptedException { 14 | 15 | 16 | ExecutorService executor = Executors.newFixedThreadPool(20); 17 | 18 | @NonNull 19 | Scheduler scheduler = Schedulers.from(executor); 20 | 21 | 22 | Observable src = Observable.just("Pasta", "Pizza", "Fries", "Curry", "Chow mein") 23 | .subscribeOn(scheduler) 24 | .doFinally(executor :: shutdown); 25 | 26 | src.subscribe(e -> compute()); 27 | src.subscribe(e -> compute()); 28 | src.subscribe(e -> compute()); 29 | src.subscribe(e -> compute()); 30 | src.subscribe(e -> compute()); 31 | src.subscribe(e -> compute()); 32 | 33 | } 34 | 35 | public static void compute() throws InterruptedException { 36 | 37 | Thread.sleep(1000); 38 | System.out.println("Computation Done By : " + Thread.currentThread().getName()); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section7/IOScheduler.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section7; 2 | 3 | import io.reactivex.rxjava3.core.Observable; 4 | import io.reactivex.rxjava3.schedulers.Schedulers; 5 | 6 | public class IOScheduler { 7 | 8 | public static void main(String[] args) throws InterruptedException { 9 | 10 | Observable src = Observable.just("Pasta", "Pizza", "Fries", "Curry", "Chow mein") 11 | .subscribeOn(Schedulers.io()); 12 | 13 | src.subscribe(e -> ioOeration()); 14 | 15 | Thread.sleep(6000); 16 | 17 | src.subscribe(e -> ioOeration()); 18 | src.subscribe(e -> ioOeration()); 19 | src.subscribe(e -> ioOeration()); 20 | src.subscribe(e -> ioOeration()); 21 | 22 | Thread.sleep(500000); 23 | } 24 | 25 | 26 | public static void ioOeration() throws InterruptedException { 27 | 28 | Thread.sleep(1000); 29 | System.out.println("Computation Done By : "+ Thread.currentThread().getName()); 30 | } 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section7/NewThreadScheduler.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section7; 2 | 3 | import io.reactivex.rxjava3.core.Observable; 4 | import io.reactivex.rxjava3.schedulers.Schedulers; 5 | 6 | public class NewThreadScheduler { 7 | 8 | public static void main(String[] args) throws InterruptedException { 9 | 10 | Observable src = Observable.just("Pasta", "Pizza", "Fries", "Curry", "Chow mein") 11 | .subscribeOn(Schedulers.newThread()); 12 | 13 | src.subscribe(e -> task()); 14 | 15 | Thread.sleep(6000); 16 | src.subscribe(e -> task()); 17 | src.subscribe(e -> task()); 18 | src.subscribe(e -> task()); 19 | src.subscribe(e -> task()); 20 | 21 | Thread.sleep(500000); 22 | } 23 | 24 | 25 | public static void task() throws InterruptedException { 26 | 27 | Thread.sleep(1000); 28 | System.out.println("Computation Done By : "+ Thread.currentThread().getName()); 29 | } 30 | 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section7/SingleScheduler.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section7; 2 | 3 | import io.reactivex.rxjava3.core.Observable; 4 | import io.reactivex.rxjava3.schedulers.Schedulers; 5 | 6 | public class SingleScheduler { 7 | 8 | public static void main(String[] args) throws InterruptedException { 9 | 10 | Observable src = Observable.just("Pasta", "Pizza", "Fries", "Curry", "Chow mein") 11 | .subscribeOn(Schedulers.single()); 12 | 13 | src.subscribe(e -> sensitiveTask()); 14 | src.subscribe(e -> sensitiveTask()); 15 | src.subscribe(e -> sensitiveTask()); 16 | src.subscribe(e -> sensitiveTask()); 17 | src.subscribe(e -> sensitiveTask()); 18 | src.subscribe(e -> sensitiveTask()); 19 | 20 | Thread.sleep(500000); 21 | } 22 | 23 | 24 | public static void sensitiveTask() throws InterruptedException { 25 | 26 | Thread.sleep(1000); 27 | System.out.println("Computation Done By : "+ Thread.currentThread().getName()); 28 | } 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section7/SubscribeOn.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section7; 2 | 3 | import io.reactivex.rxjava3.core.Observable; 4 | import io.reactivex.rxjava3.schedulers.Schedulers; 5 | 6 | public class SubscribeOn { 7 | 8 | public static void main(String[] args) throws InterruptedException { 9 | 10 | Observable.just("Pasta", "Pizza", "Fries", "Curry", "Chow mein") 11 | .subscribeOn(Schedulers.computation()) 12 | .map(e -> e.toUpperCase()) 13 | .doOnNext(e -> System.out.println(Thread.currentThread().getName())) 14 | .observeOn(Schedulers.newThread()) 15 | .filter(e -> e.startsWith("P")) 16 | .observeOn(Schedulers.io()) 17 | .subscribe(e -> print(e)); 18 | 19 | 20 | Thread.sleep(6000); 21 | 22 | } 23 | 24 | 25 | public static void print(String element) throws InterruptedException { 26 | 27 | System.out.println( element + " : Printed By : "+ Thread.currentThread().getName()); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section7/TheFlatMap.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section7; 2 | 3 | import java.time.LocalTime; 4 | 5 | import io.reactivex.rxjava3.core.Observable; 6 | import io.reactivex.rxjava3.schedulers.Schedulers; 7 | 8 | public class TheFlatMap { 9 | 10 | public static void main(String[] args) throws InterruptedException { 11 | 12 | Observable.just("Pasta", "Pizza", "Fries", "Curry", "Chow mein") 13 | .flatMap(e -> Observable.just(e) 14 | .subscribeOn(Schedulers.computation()) 15 | .map(str -> compute(str))) 16 | .subscribe(System.out::println); 17 | 18 | Thread.sleep(7000); 19 | 20 | 21 | 22 | } 23 | 24 | public static String compute(String element) throws InterruptedException { 25 | 26 | String s = element + " : Printed By : " + Thread.currentThread().getName() + " at : "+ LocalTime.now(); 27 | return s; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section8/Buffering.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section8; 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 Buffering { 9 | 10 | public static void main(String[] args) throws InterruptedException { 11 | 12 | // Observable.range(1, 30) 13 | // .buffer(4, 7) 14 | // .subscribe(System.out::println); 15 | 16 | // Observable.interval(500, TimeUnit.MILLISECONDS) 17 | // .buffer(1, TimeUnit.SECONDS, 2) 18 | // .subscribe(System.out::println); 19 | 20 | 21 | @NonNull 22 | Observable interval = Observable.interval(500, TimeUnit.MILLISECONDS); 23 | 24 | Observable.interval(1000, TimeUnit.MILLISECONDS) 25 | .window(interval) 26 | .subscribe(System.out::println); 27 | 28 | Thread.sleep(8000); 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section8/Switching.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section8; 2 | 3 | import java.util.concurrent.ThreadLocalRandom; 4 | import java.util.concurrent.TimeUnit; 5 | 6 | import io.reactivex.rxjava3.core.Observable; 7 | 8 | public class Switching { 9 | 10 | public static void main(String[] args) throws InterruptedException { 11 | Observable source = Observable.just("John","Lily","Emma","Reyan","Darshin") 12 | .concatMap( 13 | s -> Observable.just(s) 14 | .delay( 15 | ThreadLocalRandom.current().nextInt(1000), TimeUnit.MILLISECONDS 16 | ) 17 | ); 18 | 19 | 20 | Observable.interval(2, TimeUnit.SECONDS) 21 | .switchMap( s -> source.doOnDispose( 22 | () -> System.out.println("Disposing and starting again!") 23 | ) 24 | ) 25 | .subscribe(System.out::println); 26 | 27 | 28 | Thread.sleep(10000); 29 | 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section8/Throttling.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section8; 2 | 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import io.reactivex.rxjava3.core.Observable; 6 | 7 | public class Throttling { 8 | 9 | public static void main(String[] args) { 10 | 11 | Observable obs = Observable.create(emitter -> { 12 | emitter.onNext("A"); 13 | 14 | Thread.sleep(200); 15 | emitter.onNext("B"); 16 | 17 | Thread.sleep(100); 18 | emitter.onNext("C"); 19 | 20 | Thread.sleep(400); 21 | emitter.onNext("D"); 22 | 23 | Thread.sleep(300); 24 | emitter.onNext("E"); 25 | 26 | Thread.sleep(800); 27 | emitter.onNext("F"); 28 | 29 | Thread.sleep(900); 30 | emitter.onNext("X"); 31 | 32 | Thread.sleep(600); 33 | emitter.onNext("Y"); 34 | 35 | Thread.sleep(1000); 36 | emitter.onNext("Z"); 37 | 38 | emitter.onComplete(); 39 | }); 40 | 41 | 42 | obs 43 | .debounce(700, TimeUnit.MILLISECONDS) 44 | .subscribe( 45 | item -> System.out.println("onNext: " + item), 46 | Throwable::printStackTrace, 47 | () -> System.out.println("onComplete") 48 | ); 49 | 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section9/Backpressure.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section9; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | 5 | import org.reactivestreams.Subscriber; 6 | import org.reactivestreams.Subscription; 7 | 8 | import io.reactivex.rxjava3.core.Flowable; 9 | import io.reactivex.rxjava3.schedulers.Schedulers; 10 | 11 | public class Backpressure { 12 | 13 | public static void main(String[] args) { 14 | 15 | Flowable.range(1, 1000000) 16 | .map(e -> { 17 | System.out.println("Produced item is : "+ e + " : "+ Thread.currentThread().getName()); 18 | return e; 19 | }) 20 | .observeOn(Schedulers.io()) 21 | .subscribe( new Subscriber() { 22 | 23 | Subscription s; 24 | AtomicInteger count = new AtomicInteger(0); 25 | 26 | @Override 27 | public void onSubscribe(Subscription s) { 28 | this.s = s; 29 | System.out.println("Asking for 20 items"); 30 | s.request(20); 31 | 32 | } 33 | 34 | @Override 35 | public void onNext(Integer t) { 36 | 37 | 38 | if(count.getAndIncrement() % 20 ==0) { 39 | System.out.println("Asking for next 20 items "); 40 | s.request(20); 41 | } 42 | 43 | System.out.println("The subscriber consumed : "+ t); 44 | sleep(100); 45 | 46 | } 47 | 48 | @Override 49 | public void onError(Throwable t) { 50 | t.printStackTrace(); 51 | 52 | } 53 | 54 | @Override 55 | public void onComplete() { 56 | System.out.println("Completed"); 57 | 58 | } 59 | } 60 | 61 | 62 | 63 | ); 64 | 65 | sleep(100000000); 66 | 67 | 68 | } 69 | 70 | private static void sleep(long milliseconds) { 71 | 72 | try { 73 | Thread.sleep(milliseconds); 74 | } catch (InterruptedException e) { 75 | e.printStackTrace(); 76 | } 77 | 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section9/Conversion.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section9; 2 | 3 | import java.text.ParseException; 4 | 5 | import io.reactivex.rxjava3.core.Flowable; 6 | import io.reactivex.rxjava3.schedulers.Schedulers; 7 | 8 | public class Conversion { 9 | 10 | public static void main(String[] args) throws InterruptedException, ParseException { 11 | 12 | Flowable.range(1, 1000000) 13 | .toObservable() 14 | .observeOn(Schedulers.io()) 15 | .subscribe(e -> System.out.println(e+ " " +Thread.currentThread().getName() )); 16 | 17 | Thread.sleep(5000); 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Reactive-Programming/src/main/java/com/basicsstrong/reactive/section9/FlowableCreation.java: -------------------------------------------------------------------------------- 1 | package com.basicsstrong.reactive.section9; 2 | 3 | import io.reactivex.rxjava3.core.BackpressureStrategy; 4 | import io.reactivex.rxjava3.core.Flowable; 5 | import io.reactivex.rxjava3.schedulers.Schedulers; 6 | 7 | public class FlowableCreation { 8 | 9 | public static void main(String[] args) throws InterruptedException { 10 | 11 | Flowable.create(emitter -> { 12 | for(int i = 0; i <= 5000; i++ ) { 13 | if(emitter.isCancelled()) 14 | return; 15 | emitter.onNext(i); 16 | } 17 | 18 | emitter.onComplete(); 19 | }, BackpressureStrategy.BUFFER) 20 | .observeOn(Schedulers.io()) 21 | .subscribe(System.out::println); 22 | 23 | 24 | Thread.sleep(2000); 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Reactive-Programming/src/test/java/Reactive_Programming/Reactive_Programming/AppTest.java: -------------------------------------------------------------------------------- 1 | package Reactive_Programming.Reactive_Programming; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | --------------------------------------------------------------------------------