Reactive programming

Flux

https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/Flux

  1. Return an empty Flux

    1
    2
    3
    Flux<String> emptyFlux() {
    return Flux.empty();
    }
  2. Return a Flux that contains 2 values “foo” and “bar” without using an array or a collection

    1
    2
    3
    Flux<String> fooBarFluxFromValues() {
    return Flux.just("foo", "bar");
    }
  3. Create a Flux from a List that contains 2 values “foo” and “bar”

1
2
3
4
Flux<String> fooBarFluxFromList() {

return Flux.fromIterable(Arrays.asList("foo", "bar"));
}
  1. Reactive Streams defines the onError signal to deal with exceptions. Note that such an event is terminal: this is the last event the Flux will produce.

Last event임을 반드시 알고 넘어가자

1
2
3
Flux<String> errorFlux() {
return Flux.error(new IllegalStateException());
}

무슨 에러 인지 읽어보기 테스트

1
2
3
4

Flux.error(new Exception("룰루랄라"))
.doOnError(System.out::println)
.subscribe();

Mono

  1. Return an empty Mono

    1
    2
    3
    4

    Mono<String> emptyMono() {
    return Mono.empty();
    }
  2. Return a Mono that contains a “foo” value

1
2
3
Mono<String> fooMono() {
return Mono.just("foo");
}
  1. Create a Mono that emits an IllegalStateException
1
2
3
Mono<String> errorMono() {
return Mono.error(new IllegalStateException());
}

StepVerifier

  • Test에 쓸수 있어요!! nonblocking디버깅에 유용
  1. Use StepVerifier to check that the flux parameter emits “foo” and “bar” elements then completes successfully.
1
2
3
4
5
6
7
8
9
10
11

void expectFooBarComplete(Flux<String> flux) {


Flux<String> just = Flux.just("foo", "bar");

StepVerifier.create(just)
.expectNext("foo")
.expectNext("bar")
.verifyComplete();
}
  1. Use StepVerifier to check that the flux parameter emits “foo” and “bar” elements then a RuntimeException error.
1
2
3
4
5
6
7
8

void expectFooBarError(Flux<String> flux) {

StepVerifier.create(flux)
.expectNext("foo")
.expectNext("bar")
.verifyError(RuntimeException.class);
}