我在Webflux中使用SpringBoot和反应式编程。我想重复一些对端点的调用,直到数据可用(将返回一些内容)。
我想调用commandControllerApi.findById,直到displayCommand返回状态为== SUCCESS为止。如何告诉Webflux我的链的这一部分应该被调用5次,因为我的数据库中的数据应该在5-10秒后出现……
我认为当前的代码导致再次调用整个链,而不仅仅是我的链的适当部分(.flatMap(commandResponse -> commandControllerApi.findById(commandResponse.getCommandId())))
我的代码:
public Mono<Boolean> validateCredentials(FlowConfCredentials flowCredentials, UUID agentId) {
return securityService
.getUser()
.flatMap(
user -> {
Command command = new Command ();
command.setAgentId(agentId.toString());
command.setCommandType(COMMAND_TYPE);
command.setArguments(createArguments());
command.setCreatedBy(
user.getEmail());
return commandControllerApi.saveCommand(command);
})
// .retryWhen(Retry.fixedDelay(5, Duration.ofSeconds(5)))
.flatMap(commandResponse -> commandControllerApi.findById(commandResponse.getCommandId()))
.filter(displayCommand -> displayCommand.getStatus().equals(OaCommandStatus.SUCCESS))
.retryWhen(Retry.fixedDelay(5, Duration.ofSeconds(5)))
// .repeatWhenEmpty(
// Repeat.onlyIf(repeatContext -> true)
// .exponentialBackoff(Duration.ofSeconds(5), Duration.ofSeconds(5))
// .timeout(Duration.ofSeconds(30)))
.filter(
commandResponse ->
commandResponse.getStatus() != null
&& commandResponse.getStatus().equals(CommandStatus.SUCCESS))
.map(commandResponse -> true)
.switchIfEmpty(Mono.just(false));
}下面是调用上述metohd的方法:
public Flux<CredConfiguration> saveCredentials(
Mono<FlowConfCredentials> flowCredentials, UUID agentId) {
return flowCredentials
.filterWhen(cred -> validationService.validateCredentials(cred, agentId))
.flatMapMany(
flowConfCredentials -> {
if (condition1()) {
return caveCredentials(flowConfCredentials);
}
if (condition2()) {
return saveCredentialsForUser(flowConfCredentials.getExistingCredentials());
}
return Flux.error(new EmptyConfigurationException(CREDENTIALS_MESSAGE));
});
}发布于 2020-06-30 00:33:56
要仅重复订阅findById返回的单声道,而不重新订阅上游的saveCommand/getUser,请将过滤器/repeatWhenEmpty移动到调用findById的flatMap中。
public Mono<Boolean> validateCredentials(FlowConfCredentials flowCredentials, UUID agentId) {
return securityService
.getUser()
.flatMap(
user -> {
Command command = new Command();
command.setAgentId(agentId.toString());
command.setCommandType(COMMAND_TYPE);
command.setArguments(createArguments());
command.setCreatedBy(
user.getEmail());
return commandControllerApi.saveCommand(command);
})
.flatMap(saveResponse -> commandControllerApi.findById(saveResponse.getCommandId())
.filter(findResponse -> findResponse.getStatus().equals(OaCommandStatus.SUCCESS))
.repeatWhenEmpty(
Repeat.onlyIf(repeatContext -> true)
.exponentialBackoff(Duration.ofSeconds(5), Duration.ofSeconds(5))
.timeout(Duration.ofSeconds(30))))
.hasElement();
}https://stackoverflow.com/questions/62641880
复制相似问题