[Reactor Practice] Other Operations

kimchj·2024년 3월 7일

Reactor

목록 보기
9/11

1. zip(t1, t2, ...)

zip 연산자는 여러 publisher를 모아서 변환할 때 사용, 모여진 pulbisher는 Tuple 로 전달

Flux.zip(Flux.just("1", "2", "3"), Flux.just("4", "5", "6")).map(t -> t.getT1() + t.getT2());

2. firstWithValue(t1, t2, ...)

firstWithValue 는 여러 publisher중 가장 먼저 값이 전달된 publisher 사용

3. ignoreElements()

주로 작업 성공 또는 실패만 관심있을 때 사용

4. justOrEmpty(t)

Reactive Streams이 onNext에서 null 을 허용하지 않기 때문에 t의 값이 null인 경우 empty Publisher 전달

5. switchIfEmpty(publisher)

publisher가 empty 한 경우 지정한 대체 publisher 전달

6. collectList()

flux를 List로 변환하고 Mono<List< T >> 형태로 반환

Other Operations Reactor Practice

package io.pivotal.literx;

//generic imports to help with simpler IDEs (ie tech.io)
import java.util.*;
import java.util.function.*;
import java.time.*;

import io.pivotal.literx.domain.User;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * Learn how to use various other operators.
 *
 * @author Sebastien Deleuze
 */
public class Part08OtherOperations {

//========================================================================================

	// TODO Create a Flux of user from Flux of username, firstname and lastname.
	Flux<User> userFluxFromStringFlux(Flux<String> usernameFlux, Flux<String> firstnameFlux, Flux<String> lastnameFlux) {
		return Flux.zip(usernameFlux, firstnameFlux, lastnameFlux)
        .map(t -> new User(t.getT1(), t.getT2(), t.getT3()));
	}

//========================================================================================

	// TODO Return the mono which returns its value faster
	Mono<User> useFastestMono(Mono<User> mono1, Mono<User> mono2) {
		return Mono.firstWithValue(mono1, mono2);
	}

//========================================================================================

	// TODO Return the flux which returns the first value faster
	Flux<User> useFastestFlux(Flux<User> flux1, Flux<User> flux2) {
		return Flux.firstWithValue(flux1, flux2);
	}

//========================================================================================

	// TODO Convert the input Flux<User> to a Mono<Void> that represents the complete signal of the flux
	Mono<Void> fluxCompletion(Flux<User> flux) {
		return flux.ignoreElements().then();
	}

//========================================================================================

	// TODO Return a valid Mono of user for null input and non null input user (hint: Reactive Streams do not accept null values)
	Mono<User> nullAwareUserToMono(User user) {
		return Mono.justOrEmpty(user);
	}

//========================================================================================

	// TODO Return the same mono passed as input parameter, expect that it will emit User.SKYLER when empty
	Mono<User> emptyToSkyler(Mono<User> mono) {
		return mono.switchIfEmpty(Mono.just(User.SKYLER));
	}

//========================================================================================

	// TODO Convert the input Flux<User> to a Mono<List<User>> containing list of collected flux values
	Mono<List<User>> fluxCollection(Flux<User> flux) {
		return flux.collectList();
	}

}
profile
ㅎㅇ

0개의 댓글