需要有关以下代码的一些指导。
期望:
// DB CRUD operation service for Group table
// Group record is unique by groupId and type
@Autowired
final GroupService groupService;
public String newUniqueId(String format, String groupId) {
// try 10 times to generate a unique id
return IntStream.range(0, 10)
.mapToObj(i -> newIdByFormat(format))
.filter(newIdByFormat -> isIdUnique(newIdByFormat, groupId)) // Need some guidence here
.findFirst()
.orElseThrow(() -> new RuntimeException("Could not generate unique id"));
}
private String newIdByFormat(String format) {
// Map<String, Supplier<String>> map;
return map.get(format).get();
}
private Predicate<String> isIdUnique(String groupId) {
// This call returns, Mono<Group> group = groupService.query(id, groupId)
return id -> groupService.query(id, groupId)
.filter(Objects::isNull); // How to return boolen value here?
// tried calling .block() here, but fails with exception
// block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-4
}
public Mono<Group> createGroup(Request) {
return transformRequest(request)
}
private Mono<Group> transformRequest(request) {
String uniqueId = newUniqueId(request.format, request.groupId);
return dbService.save(new Group(uniqueId));
}
感谢您的宝贵时间。
private Mono<Group> transformRequest(request) {
// newUniqueId() must return a Mono.
// otherwise you start to execute the code right in this method, which is not reactive.
// Ex., a non-mono executed even if the result is cancelled.
// Reactive code executed only when it actually needed. Like lazily
Mono<String> uniqueId = newUniqueId(request.format, request.groupId);
return uniqueId.map { id ->
new Group(id)
}.flatMap { group ->
// .save() should be a Mono
dbService.save(group).thenReturn(group)
}
}
public Mono<String> newUniqueId(String format, String groupId) {
Mono.fromCallable {
// Callable executed only when it's actually subscribed
newIdByFormat(format)
}.flatMap { id ->
groupService.query(id, groupId).retry(10)
}
}