Reactor Mono/Flux - 根据条件遍历数组和 return 结果
Reactor Mono/Flux - iterating over array and return result by condition
这个问题看起来很基础,但我只是想学习响应式编程,无法找出最佳答案。
我有以下同步代码,它只是遍历提供者并找到第一个用户:
public interface PrincipalProvider {
public User findUser(String name);
}
public class PrincipalProviderComposite implements PrincipalProvider {
private final List<PrincipalProvider> providers;
@Override
public User findUser(String name) {
User user = null;
for(PrincipalProvider provider: providers) {
user = provider.findUser(name);
if(user != null)
break;
}
return user;
}
}
现在我需要使此代码异步,提供者可能 return 用户或可能 return Mono.empty()
.
public interface PrincipalProvider {
public Mono<User> findUser(String name);
}
public Mono<User> findUser(String name) {
return Flux.fromIterable(providers)
.concatMap(provider -> provider.findUser(name))
.next();
}
这个问题看起来很基础,但我只是想学习响应式编程,无法找出最佳答案。
我有以下同步代码,它只是遍历提供者并找到第一个用户:
public interface PrincipalProvider {
public User findUser(String name);
}
public class PrincipalProviderComposite implements PrincipalProvider {
private final List<PrincipalProvider> providers;
@Override
public User findUser(String name) {
User user = null;
for(PrincipalProvider provider: providers) {
user = provider.findUser(name);
if(user != null)
break;
}
return user;
}
}
现在我需要使此代码异步,提供者可能 return 用户或可能 return Mono.empty()
.
public interface PrincipalProvider {
public Mono<User> findUser(String name);
}
public Mono<User> findUser(String name) {
return Flux.fromIterable(providers)
.concatMap(provider -> provider.findUser(name))
.next();
}