学习如何转换异步数据
学习如何转换异步数据
描述
Reactor 附带多个可用于转换数据的操作符。
public class Part04Transform {
//========================================================================================
// TODO Capitalize the user username, firstname and lastname
public Mono<User> capitalizeOne(Mono<User> mono) {
return mono.map(user -> new User(user.getUsername().toUpperCase(), user.getFirstname().toUpperCase(), user.getLastname().toUpperCase()));
}
//========================================================================================
// TODO Capitalize the users username, firstName and lastName
public Flux<User> capitalizeMany(Flux<User> flux) {
return flux.map(user -> new User(user.getUsername().toUpperCase(), user.getFirstname().toUpperCase(), user.getLastname().toUpperCase()));
}
//========================================================================================
// TODO Capitalize the users username, firstName and lastName using #asyncCapitalizeUser
public Flux<User> asyncCapitalizeMany(Flux<User> flux) {
return flux.flatMap(this::asyncCapitalizeUser);
}
Mono<User> asyncCapitalizeUser(User u) {
return Mono.just(new User(u.getUsername().toUpperCase(), u.getFirstname().toUpperCase(), u.getLastname().toUpperCase()));
}
}
@Test
public void TestTransform() {
Part04Transform t = new Part04Transform();
// 测试单个
System.out.println("单个测试");
Mono<User> mono = t.capitalizeOne(Mono.just(User.JESSE));
mono.subscribe(System.out::println);
// 测试多个
System.out.println("多个测试");
Flux<User> flux = t.capitalizeMany(Flux.just(User.JESSE, User.SAUL));
flux.subscribe(System.out::println);
// 测试异步
System.out.println("异步测试");
Flux<User> flux2 = t.asyncCapitalizeMany(Flux.just(User.WALTER, User.SAUL));
flux2.subscribe(System.out::println);
}
本文来自博客园,作者:bigroc,转载请注明原文链接:https://www.cnblogs.com/bigroc/p/19210873
blog:http://www.bigroc.cn 博客园:https://www.cnblogs.com/bigroc
浙公网安备 33010602011771号