Spring Webflux - 反应式存储库 saveAll(可迭代) <S>vs saveAll(Publisher<S>)



关于webflux反应式存储库的小问题,特别是关于方法saveAll-Flux saveAll(Iterable var1(;相对于Flux saveAll(发布服务器var1(;

为了进行比较,我写了以下内容:

@Controller
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Autowired
private SomeReactiveRepository someReactiveRepository;
@PostMapping(path = "/saveListInsideMono", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<QuestionResponse> saveListInsideMono(@RequestBody Mono<QuestionRequest> questionRequestMono) {
//just doing some business transformation on the list inside the mono
Mono<List<String>> enhancedStringListMono = questionRequestMono.map(questionRequest -> enhance(questionRequest));
//take the pojo inside the mono and map it to a saveAllAndConvertToResponse method (see next method)
Mono<QuestionResponse> questionResponseMono = enhancedStringListMono.map(enhancedStringList -> saveAllAndConvertToResponse(enhancedStringList));
return questionResponseMono;
}
private QuestionResponse saveAllAndConvertToResponse(List<String> enhancedStringList) {
// use the repository <S extends T> Flux<S> saveAll(Iterable<S> var1); + subscribe
return someReactiveRepository.saveAll(enhancedStringList).thenReturn(new QuestionResponse(enhancedStringList));
//this also works but not good to subscribe
//someReactiveRepository.saveAll(enhancedStringList).subscribe();
//return new QuestionResponse(enhancedStringList);
}
@PostMapping(path = "/saveFlux", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<QuestionResponse> saveFlux(@RequestBody Mono<QuestionRequest> questionRequestMono) {
//just doing some business transformation on the list inside the mono
Mono<List<String>> enhancedStringListMono = questionRequestMono.map(questionRequest -> enhance(questionRequest));
// use the repository <S extends T> Flux<S> saveAll(Publisher<S> var1); to save the flatMapMany + fromIterable directly
Flux<String> enhancedStringFlux = someReactiveRepository.saveAll(enhancedStringListMono.flatMapMany(Flux::fromIterable));
Mono<QuestionResponse> questionResponseMono = enhancedStringFlux.collectList().map(enhancedString -> convertToResponse(enhancedString));
return questionResponseMono;
}
private QuestionResponse convertToResponse(List<String> enhancedStringList) {
//return the object needed
return new QuestionResponse(enhancedStringList);
}
private static List<String> enhance(QuestionRequest questionRequest) {
//dummy business transformation logic
List<String> baseList = questionRequest.getList();
List<String> enhancedList = baseList.stream().map(oneString -> "enhanced" + oneString).collect(Collectors.toList());
return enhancedList;
}
public class QuestionRequest {
private List<String> list;
public List<String> getList() {
return list;
}
}
public class QuestionResponse {
private List<String> enhancedList;
public QuestionResponse(List<String> enhancedList) {
this.enhancedList = enhancedList;
}
}
}

就";正确性";两个代码都在做预期的事情。一切都成功地持久化了。

但就性能、反应式范式、IO对DB的利用率、Netty Core的利用率而言,什么是";最好的";解决方案,为什么请?

谢谢

这一切都取决于您当前拥有的对象。如果您有一个对象的Flux,请使用带有Publisher的saveAll方法。如果您有对象的实际Collection,请使用saveAll方法,该方法采用Iterable

例如,如果您查看实现SimpleReactiveCassandraRepository,则saveAll的实现(采用Iterable(仅将其封装在Flux中,并委托给接受Flux的saveAll方法

public <S extends T> Flux<S> saveAll(Iterable<S> entities) {
Assert.notNull(entities, "The given Iterable of entities must not be null");
return saveAll(Flux.fromIterable(entities));
}

因此,在IO利用率或净核心利用率方面应该没有差异。此外,两者都遵循反应范式。

SimpleReactiveCassandra存储库代码

相关内容

最新更新