자세한 설명은 생략한다
package com.learnreactiveprogramming.service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
public class FluxAndMonoGeneratorService {
public Flux<String> namesFlux() {
return Flux.fromIterable(List.of("alex","ben","chloe")).log(); // db or remote service
}
public Mono<String> nameMono() {
return Mono.just("alex").log(); // db or remote service
}
public Flux<String> namesFlux_map(int stringLength) {
return Flux.fromIterable(List.of("alex","ben","chloe"))
.map(String::toUpperCase)
.filter(s -> s.length() > stringLength)
.map(s->s.length() + "-" + s)
.log(); // db or remote service
}
public Flux<String> namesFlux_flatmap(int stringLength) {
return Flux.fromIterable(List.of("alex","ben","chloe"))
.map(String::toUpperCase)
.filter(s -> s.length() > stringLength)
// ALEX, CHLOE -> A, L, E, X, C, H, L, O, E
.flatMap(this::splitString)
.log(); // db or remote service
}
public Flux<String> namesFlux_flatmap_async(int stringLength) {
return Flux.fromIterable(List.of("alex","ben","chloe"))
.map(String::toUpperCase)
.filter(s -> s.length() > stringLength)
// ALEX, CHLOE -> A, L, E, X, C, H, L, O, E
.flatMap(this::splitString_withDelay)
.log(); // db or remote service
}
public Flux<String> namesFlux_concatmap(int stringLength) {
return Flux.fromIterable(List.of("alex","ben","chloe"))
.map(String::toUpperCase)
.filter(s -> s.length() > stringLength)
// ALEX, CHLOE -> A, L, E, X, C, H, L, O, E
.concatMap(this::splitString_withDelay)
.log(); // db or remote service
}
public Flux<String> splitString(String name) {
var charArray = name.split("");
return Flux.fromArray(charArray);
}
public Flux<String> splitString_withDelay(String name) {
var charArray = name.split("");
// var delay = new Random().nextInt(1000);
var delay = 1000;
return Flux.fromArray(charArray)
.delayElements(Duration.ofMillis(delay));
}
public Flux<String> namesFlux_immutablity() {
var namesFlux = Flux.fromIterable(List.of("alex","ben","chloe"));
namesFlux.map(String::toUpperCase);
return namesFlux;
}
public Mono<List<String>> namesMono_flatMap(int stringLength) {
return Mono.just("alex")
.map(String::toUpperCase)
.filter(s -> s.length() > stringLength)
.flatMap(this::splitStringMono)
.log();
}
public Flux<String> namesMono_flatMapMany(int stringLength) {
return Mono.just("alex")
.map(String::toUpperCase)
.filter(s -> s.length() > stringLength)
.flatMapMany(this::splitString)
.log();
}
private Mono<List<String>> splitStringMono(String s) {
var charArray = s.split("");
var charList = List.of(charArray);
return Mono.just(charList);
}
public Flux<String> namesFlux_transform(int stringLength) {
Function<Flux<String>, Flux<String>> filtermap = name -> name.map(String::toUpperCase)
.filter(s -> s.length() > stringLength);
// Flux.empty()
return Flux.fromIterable(List.of("alex","ben","chloe"))
.transform(filtermap)
.flatMap(this::splitString)
.defaultIfEmpty("default")
.log(); // db or remote service
}
public Flux<String> namesFlux_transform_switchifEmpty(int stringLength) {
Function<Flux<String>, Flux<String>> filtermap = name ->
name.map(String::toUpperCase)
.filter(s -> s.length() > stringLength)
.flatMap(this::splitString);
var defaultFlux = Flux.just("default")
.transform(filtermap);
// Flux.empty()
return Flux.fromIterable(List.of("alex","ben","chloe"))
.transform(filtermap)
.switchIfEmpty(defaultFlux)
// .defaultIfEmpty("default")
.log(); // db or remote service
}
public Flux<String> explore_concat() {
var abcFlux = Flux.just("A", "B", "C");
var defFlux = Flux.just("D", "E", "F");
return Flux.concat(abcFlux, defFlux)
.log();
}
public Flux<String> explore_concatWith() {
var abcFlux = Flux.just("A", "B", "C");
var defFlux = Flux.just("D", "E", "F");
return abcFlux.concatWith(defFlux)
.log();
}
public Flux<String> explore_concatWith_mono() {
var aMono = Mono.just("A");
var bMono = Mono.just("B");
return aMono.concatWith(bMono).log();
}
public Flux<String> explore_merge() {
var abcFlux = Flux.just("A", "B", "C")
.delayElements(Duration.ofMillis(100));
var defFlux = Flux.just("D", "E", "F")
.delayElements(Duration.ofMillis(125));
return Flux.merge(abcFlux, defFlux)
.log();
}
public Flux<String> explore_mergeWith() {
var abcFlux = Flux.just("A", "B", "C")
.delayElements(Duration.ofMillis(100));
var defFlux = Flux.just("D", "E", "F")
.delayElements(Duration.ofMillis(125));
return abcFlux.mergeWith(defFlux)
.log();
}
public Flux<String> explore_mergeWith_mono() {
var aMono = Mono.just("A");
var bMono = Mono.just("B");
return aMono.mergeWith(bMono).log();
}
public Flux<String> explore_mergeSequential() {
var abcFlux = Flux.just("A", "B", "C")
.delayElements(Duration.ofMillis(100));
var defFlux = Flux.just("D", "E", "F")
.delayElements(Duration.ofMillis(95));
return Flux.mergeSequential(abcFlux, defFlux)
.log();
}
public Flux<String> explore_zip() {
var abcFlux = Flux.just("A", "B", "C");
var defFlux = Flux.just("D", "E", "F");
return Flux.zip(abcFlux, defFlux, (first, second) -> first + second)
.log();
}
public Flux<String> explore_zip_1() {
var abcFlux = Flux.just("A", "B", "C");
var defFlux = Flux.just("D", "E", "F");
var _123flux = Flux.just("1", "2", "3");
var _456flux = Flux.just("4", "5", "6");
return Flux.zip(abcFlux, defFlux,_123flux,_456flux)
.map(t4 -> t4.getT1() + t4.getT2() + t4.getT3() + t4.getT4())
.log();
}
public Flux<String> explore_zipWith() {
var abcFlux = Flux.just("A", "B", "C");
var defFlux = Flux.just("D", "E", "F");
return abcFlux.zipWith(defFlux, (first, second) -> first + second)
.log();
}
public Mono<String> explore_mergeZipWith_mono() {
var aMono = Mono.just("A");
var bMono = Mono.just("B");
return aMono.zipWith(bMono)
.map(t2 -> t2.getT1() + t2.getT2())
.log();
}
public static void main(String[] args) {
FluxAndMonoGeneratorService fluxAndMonoGeneratorService = new FluxAndMonoGeneratorService();
fluxAndMonoGeneratorService.namesFlux()
.subscribe(name -> System.out.println("Name is " + name));
fluxAndMonoGeneratorService.nameMono()
.subscribe(name -> {
System.out.println("Mono Name is " + name);
});
}
}
package com.learnreactiveprogramming.service;
import org.junit.jupiter.api.Test;
import reactor.test.StepVerifier;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class FluxAndMonoGeneratorServiceTest {
FluxAndMonoGeneratorService fluxAndMonoGeneratorService = new FluxAndMonoGeneratorService();
@Test
void namesFlux() {
var namesFlux = fluxAndMonoGeneratorService.namesFlux();
StepVerifier.create(namesFlux)
.expectNext("alex")
.expectNext("ben")
.expectNext("chloe")
.verifyComplete();
}
@Test
void namesFlux_map() {
int stringLength = 3;
var namesFlux = fluxAndMonoGeneratorService.namesFlux_map(stringLength);
StepVerifier.create(namesFlux)
.expectNext("4-ALEX", "5-CHLOE")
.verifyComplete();
}
@Test
void namesFlux_immutablity() {
var namesFlux = fluxAndMonoGeneratorService.namesFlux_immutablity();
StepVerifier.create(namesFlux)
.expectNext("alex")
.expectNext("ben")
.expectNext("chloe")
.verifyComplete();
}
@Test
void namesFlux_flatmap() {
int stringLength = 3;
var namesFlux = fluxAndMonoGeneratorService.namesFlux_flatmap(stringLength);
StepVerifier.create(namesFlux)
.expectNext("A", "L", "E", "X", "C", "H", "L", "O", "E")
.verifyComplete();
}
@Test
void namesFlux_flatmap_async() {
int stringLength = 3;
var namesFlux = fluxAndMonoGeneratorService.namesFlux_flatmap_async(stringLength);
StepVerifier.create(namesFlux)
// .expectNext("A", "L", "E", "X", "C", "H", "L", "O", "E")
.expectNextCount(9)
.verifyComplete();
}
@Test
void namesFlux_concatmap() {
int stringLength = 3;
var namesFlux = fluxAndMonoGeneratorService.namesFlux_concatmap(stringLength);
StepVerifier.create(namesFlux)
.expectNext("A", "L", "E", "X", "C", "H", "L", "O", "E")
// .expectNextCount(9)
.verifyComplete();
}
@Test
void namesMono_flatMap() {
int stringLength = 3;
var value = fluxAndMonoGeneratorService.namesMono_flatMap(stringLength);
StepVerifier.create(value)
.expectNext(List.of("A","L","E","X"))
.verifyComplete();
}
@Test
void namesMono_flatMapMany() {
int stringLength = 3;
var value = fluxAndMonoGeneratorService.namesMono_flatMapMany(stringLength);
StepVerifier.create(value)
.expectNext("A","L","E","X")
.verifyComplete();
}
@Test
void namesFlux_transform() {
int stringLength = 3;
var value = fluxAndMonoGeneratorService.namesFlux_transform(stringLength);
StepVerifier.create(value)
.expectNext("A","L","E","X","C","H","L","O","E")
.verifyComplete();
}
@Test
void namesFlux_transform_1() {
int stringLength = 6;
var value = fluxAndMonoGeneratorService.namesFlux_transform(stringLength);
StepVerifier.create(value)
// .expectNext("A","L","E","X","C","H","L","O","E")
.expectNext("default")
.verifyComplete();
}
@Test
void namesFlux_transform_switchifEmpty() {
int stringLength = 6;
var value = fluxAndMonoGeneratorService.namesFlux_transform_switchifEmpty(stringLength);
StepVerifier.create(value)
// .expectNext("A","L","E","X","C","H","L","O","E")
.expectNext("D","E","F","A","U","L","T")
.verifyComplete();
}
@Test
void explore_concat() {
var value = fluxAndMonoGeneratorService.explore_concat();
StepVerifier.create(value)
.expectNext("A","B","C","D","E","F")
.verifyComplete();
}
@Test
void explore_concatWith() {
var value = fluxAndMonoGeneratorService.explore_concat();
StepVerifier.create(value)
.expectNext("A","B","C","D","E","F")
.verifyComplete();
}
@Test
void explore_merge() {
var value = fluxAndMonoGeneratorService.explore_merge();
StepVerifier.create(value)
.expectNext("A","D","B","E","C","F")
// .expectNextCount(6)
.verifyComplete();
}
@Test
void explore_mergeWith() {
var value = fluxAndMonoGeneratorService.explore_merge();
StepVerifier.create(value)
.expectNext("A","D","B","E","C","F")
// .expectNextCount(6)
.verifyComplete();
}
@Test
void explore_mergeSequential() {
var value = fluxAndMonoGeneratorService.explore_mergeSequential();
StepVerifier.create(value)
.expectNext("A","B","C","D","E","F")
.verifyComplete();
}
@Test
void explore_zip() {
var value = fluxAndMonoGeneratorService.explore_zip();
StepVerifier.create(value)
.expectNext("AD","BE","CF")
.verifyComplete();
}
@Test
void explore_zip_1() {
var value = fluxAndMonoGeneratorService.explore_zip_1();
StepVerifier.create(value)
.expectNext("AD14","BE25","CF36")
.verifyComplete();
}
}