java

class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { Flux<String> netFlux = Flux.fromStream(ReactiveJavaTutorial::getVideo) .delayElements(Duration.ofSeconds(2)); // each part will play for 2 seconds // First Subscriber netFlux.subscribe(part -> System.out.println(“Subscriber 1: ” + part)); // wait 5 seconds before next Subscriber joins Thread.sleep(5000); // Seconds Subscriber netFlux.subscribe(part -> System.out.println(“Subscriber 2: ” + part)); Thread.sleep(60000); }…

Read More Hot and Cold Publishers in Project Reactor

class ReactiveJavaTutorial { public static void main(String[] args) { Mono.just(“data1”) .concatWith(Flux.error(new RuntimeException(“Exception occurred.”))) .doOnError(ex -> System.out.println(“LOG: Exception caught: ” + ex)) .retry(3) //retry 3 times in case of an error .log() .subscribe(); } } Output: reactor.Flux.Retry.1 : onSubscribe(FluxRetry.RetrySubscriber) reactor.Flux.Retry.1 : request(unbounded) reactor.Flux.Retry.1 : onNext(data1) LOG: Exception caught: java.lang.RuntimeException: Exception occurred. reactor.Flux.Retry.1 : onNext(data1) LOG: Exception…

Read More RetryFailed Operation in Project Reactor

class ReactiveJavaTutorial { public static void main(String[] args) { Flux.just(2, 7, 10) .concatWith(Flux.error(new RuntimeException(“Exception occurred”))) .concatWith(Mono.just(12)) .log() .subscribe(); } } Output: reactor.Flux.ConcatArray.1 : onSubscribe(FluxConcatArray.ConcatArraySubscriber) reactor.Flux.ConcatArray.1 : request(unbounded) reactor.Flux.ConcatArray.1 : onNext(2) reactor.Flux.ConcatArray.1 : onNext(7) reactor.Flux.ConcatArray.1 : onNext(3) reactor.Flux.ConcatArray.1 : onError(java.lang.RuntimeException: Exception occurred) reactor.Flux.ConcatArray.1 : java.lang.RuntimeException: Exception occurred at com.example.demo.DemoApplication.main(DemoApplication.java:14) ~[classes/:na]   You can see that element…

Read More Handling Exceptions in Project Reactor

class ReactiveJavaTutorial { public static void main(String[] args) { Flux.just(“data1”, “data2”, “data3”) .doOnSubscribe(subscription -> System.out.println(“Subscription: ” + subscription)) .subscribe(); } } Output: Subscription: reactor.core.publisher.FluxArray$ArraySubscription@72bca894 doOnNext() method class ReactiveJavaTutorial { public static void main(String[] args) { Flux.just(“data1”, “data2”, “data3”) .doOnNext(data -> System.out.println(“Data: ” + data)) .subscribe(); } } Output: Data: data1 Data: data2 Data: data3 doOnComplete()…

Read More doOn Callbacks in Project Reactor

class ReactiveJavaTutorial { public static void main(String[] args) { Flux<String> firstFlux = Flux.fromArray(new String[]{“a”, “b”, “c”}); Flux<String> secondFlux = Flux.fromArray(new String[]{“d”, “e”, “f”}); // combine two Flux Publishers Flux<String> combinedFlux = Flux.concat(firstFlux, secondFlux); // subscribe and print the elements of a combined Flux combinedFlux.subscribe(element -> System.out.print(element + ” “)); } } Output: a b c…

Read More Combine Flux and Mono Publishers

class ReactiveJavaTutorial { public static void main(String[] args) { Flux.fromArray(new String[]{“Tom”, “Melissa”, “Steve”, “Megan”}) .map(String::toUpperCase) .subscribe(System.out::println); } } Output: TOM MELISSA STEVE MEGAN class ReactiveJavaTutorial { public static void main(String[] args) { Flux.fromArray(new String[]{“Tom”, “Melissa”, “Steven”, “Megan”}) .filter(name -> name.length() > 5) .map(String::toUpperCase) .subscribe(System.out::println); } } Output: MELISSA STEVEN   Note: Reactive Streams are immutable.…

Read More Transform Flux and Mono Using Operators

Flux<String> cities = Flux.fromIterable( new ArrayList<>(Arrays.asList(“New York”, “London”, “Paris”, “Toronto”, “Rome”))); cities.subscribe(); class ReactiveJavaTutorial { public static void main(String[] args) { Flux<String> cities = Flux.fromIterable( new ArrayList<>(Arrays.asList(“New York”, “London”, “Paris”, “Toronto”, “Rome”))); cities.log().subscribe(); } } Output: INFO 6832 — [ main] reactor.Flux.Iterable.1 : | onSubscribe([Synchronous Fuseable] FluxIterable.IterableSubscription) INFO 6832 — [ main] reactor.Flux.Iterable.1 : |…

Read More How Mono and Flux Work Internally?

class ReactiveJavaTutorial { public static void main(String[] args) { Mono<String> mono = Mono.just(“data”); Flux<String> fluxFromMono = Flux.from(mono); fluxFromMono.subscribe(System.out::println); } } Output: data class ReactiveJavaTutorial { public static void main(String[] args) { // one value Flux<String> flux1 = Flux.just(“data1”); Mono<String> monoFromFlux1 = flux1.next(); // get data from mono monoFromFlux1.subscribe(data -> System.out.println(“monoFromFlux1 data: ” + data)); //…

Read More Convert Mono to Flux and vice versa

class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { Flux<String> flux = Flux.fromArray(new String[]{“data1”, “data2”, “data3”}); flux.subscribe(System.out::println); } } Output: data1 data2 data3   Another way would be using the Reactive Streams operators like onNext, flatMap, etc. class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { Flux<String> flux = Flux.fromArray(new String[]{“data1”,…

Read More Extract data from Flux

class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { // Create a Flux Flux<String> flux = Flux.just(“data1”, “data2”, “data3”); // Subscribe to a Flux and print the elements flux.subscribe(System.out::println); } } Output: data1 data2 data3   When we call the subscribe(), we are telling the Publisher to start sending data. class ReactiveJavaTutorial {…

Read More Subscribe to Flux in Java Reactor

import reactor.core.publisher.Flux; class ReactiveJavaTutorial { public static void main(String[] args) throws InterruptedException { // create an empty Flux Flux flux1 = Flux.just(); // create a Flux that will hold only one value Flux<String> flux2 = Flux.just(“data”); // create a Flux that will hold multiple values Flux<String> flux3 = Flux.just(“data1”, “data2”, “data3”); } } import reactor.core.publisher.Flux;…

Read More Create a Flux in Java Reactor

import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { String dataFromMono = getMono().block(); System.out.println(“Data from Mono: ” + dataFromMono); } private static Mono getMono() { return Mono.fromSupplier(() -> { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return “Hello!”; }); } } Output: Data from Mono: Hello! class ReactiveJavaTutorial { public…

Read More Extract Data from Mono in Java

import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { // create a Mono Mono<String> mono = Mono.just(“Hello”); // subscribe to a Mono mono.subscribe(); } } import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { // create a Mono Mono<String> mono = Mono.just(“Hello”); // subscribe to a Mono mono.subscribe(data -> System.out.println(data)); }…

Read More Subscribe to a Mono in Java Reactor

public static <T> Mono<T> just(T data) import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { Mono<String> mono = Mono.just(“Hello”); } } import org.reactivestreams.Publisher; import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { Publisher<String> mono = Mono.just(“Hello”); } } import reactor.core.publisher.Mono; class ReactiveJavaTutorial { public static void main(String[] args) { // will…

Read More Create a Mono in Java Reactor

In the previous post, we covered a String to Date conversion. In this post, you will learn to convert LocalDate and LocalDateTime to String in Java. LocalDate and LocalDateTime are immutable date-time objects that represent a date and a date and time. Both of the examples below make use of java.time package. For more information about Java packages, check out our…

Read More Convert LocalDate and LocalDateTime to String in Java