Rx java 如何在反应式编程中使用增量id在db中创建条目?
我不熟悉反应式编程。需要帮助来理解这种行为 我想要实现什么 我想创建50个条目,它们的id应该是递增的。如果db中存在id为1的条目,则应创建id为2的条目 我目前的实施情况如下:Rx java 如何在反应式编程中使用增量id在db中创建条目?,rx-java,reactive-programming,spring-webflux,project-reactor,reactive,Rx Java,Reactive Programming,Spring Webflux,Project Reactor,Reactive,我不熟悉反应式编程。需要帮助来理解这种行为 我想要实现什么 我想创建50个条目,它们的id应该是递增的。如果db中存在id为1的条目,则应创建id为2的条目 我目前的实施情况如下: // create entry 50 times void createEntries() { LOGGER.info("going to create 50 entries); Flux.range(1, 50) .flatMap(i ->
// create entry 50 times
void createEntries() {
LOGGER.info("going to create 50 entries);
Flux.range(1, 50)
.flatMap(i -> createEntry(5))
.subscribe();
}
//method to create an entry in db with incremental id
private Mono<Integer> createEntry(long retryInterval) {
return (customRepository.findAllEntry()) //-->A db call which returns all entries flux<Entrys>
.map(entry -> entry.getEntryId())
.sort()
//get last existing entry id
.last(0)
//try to create the entry with new incremented id
.flatMap(id -> createEntry(id + 1, retryInterval));
}
private Mono<? extends Integer> createEntry(int newEntryId, long retryInterval) {
return saveEntry(newEntryId) //--> return Mono<Boolean> true if saved false if id already exists
.doOnNext(applied -> LOGGER.info("Successfully created entry with id: {} ? {} ", newEntryId, applied)) //--> Why this is called multiple times??
.flatMap(applied -> !applied
//applied false shows id already exists, so try again recursively with new incremented id
? createEntry(newEntryId + 1, retryInterval)
: Mono.just(newEntryId))
.doOnError(e -> LOGGER.warn("Error creating entry with id {} ? {} : ", newEntryId, e));
.retryWhen(Retry.anyOf(RuntimeException.class)
.exponentialBackoff(Duration.ofSeconds(retryInterval), Duration.ofSeconds(retryInterval))); //-->retry on creation if any exception
}
// create entry 50 times
void createEntries() {
LOGGER.info("going to create 50 entries);
createEntry(5).subscribe();
}
//method to create an entry in db with incremental id
private Flux<? extends Integer> createEntry(long retryInterval) {
return (customRepository.findAllEntry()) //-->A db call which returns all entries flux<Entrys>
.map(entry -> entry.getEntryId())
.sort()
//get last existing entry id
.last(0)
//try to create the entry with new incremented id
.flatMap(id -> createEntry(id + 1, retryInterval))
.repeat(49); //-->This fixes my issue will only be invoked 49 times again onComplete(). And hence will create 50 entries
}
private Mono<? extends Integer> createEntry(int newEntryId, long retryInterval) {
return saveEntry(newEntryId) //--> return Mono<Boolean> true if saved false if id already exists
.doOnNext(applied -> LOGGER.info("Successfully created entry with id: {} ? {} ", newEntryId, applied))
.flatMap(applied -> !applied
//applied false shows id already exists, so try again recursively with new incremented id
? createEntry(newEntryId + 1, retryInterval)
: Mono.just(newEntryId))
.doOnError(e -> LOGGER.warn("Error creating entry with id {} ? {} : ", newEntryId, e));
.retryWhen(Retry.anyOf(RuntimeException.class)
.exponentialBackoff(Duration.ofSeconds(retryInterval), Duration.ofSeconds(retryInterval))); //-->retry on creation if any exception
}
//创建条目50次
void createEntries(){
LOGGER.info(“将创建50个条目);
流量范围(1,50)
.flatMap(i->createEntry(5))
.subscribe();
}
//方法在数据库中创建具有增量id的项
私有Mono createEntry(长重试间隔){
return(customRepository.findAllEntry())/-->返回所有条目的db调用
.map(entry->entry.getEntryId())
.sort()
//获取最后一个现有条目id
.最后(0)
//尝试使用新的递增id创建条目
.flatMap(id->createEntry(id+1,retryInterval));
}
private Mono最后我在代码中找到了问题所在。问题出现在下面的代码片段中
// create entry 50 times
void createEntries() {
LOGGER.info("going to create 50 entries);
Flux.range(1, 50)
.flatMap(i -> createEntry(5))
.subscribe();
}
这是在saveEntry(newEntryId)
完成之前50次调用该方法。我通过使用repeat
api修复了该问题,如下所示:
// create entry 50 times
void createEntries() {
LOGGER.info("going to create 50 entries);
Flux.range(1, 50)
.flatMap(i -> createEntry(5))
.subscribe();
}
//method to create an entry in db with incremental id
private Mono<Integer> createEntry(long retryInterval) {
return (customRepository.findAllEntry()) //-->A db call which returns all entries flux<Entrys>
.map(entry -> entry.getEntryId())
.sort()
//get last existing entry id
.last(0)
//try to create the entry with new incremented id
.flatMap(id -> createEntry(id + 1, retryInterval));
}
private Mono<? extends Integer> createEntry(int newEntryId, long retryInterval) {
return saveEntry(newEntryId) //--> return Mono<Boolean> true if saved false if id already exists
.doOnNext(applied -> LOGGER.info("Successfully created entry with id: {} ? {} ", newEntryId, applied)) //--> Why this is called multiple times??
.flatMap(applied -> !applied
//applied false shows id already exists, so try again recursively with new incremented id
? createEntry(newEntryId + 1, retryInterval)
: Mono.just(newEntryId))
.doOnError(e -> LOGGER.warn("Error creating entry with id {} ? {} : ", newEntryId, e));
.retryWhen(Retry.anyOf(RuntimeException.class)
.exponentialBackoff(Duration.ofSeconds(retryInterval), Duration.ofSeconds(retryInterval))); //-->retry on creation if any exception
}
// create entry 50 times
void createEntries() {
LOGGER.info("going to create 50 entries);
createEntry(5).subscribe();
}
//method to create an entry in db with incremental id
private Flux<? extends Integer> createEntry(long retryInterval) {
return (customRepository.findAllEntry()) //-->A db call which returns all entries flux<Entrys>
.map(entry -> entry.getEntryId())
.sort()
//get last existing entry id
.last(0)
//try to create the entry with new incremented id
.flatMap(id -> createEntry(id + 1, retryInterval))
.repeat(49); //-->This fixes my issue will only be invoked 49 times again onComplete(). And hence will create 50 entries
}
private Mono<? extends Integer> createEntry(int newEntryId, long retryInterval) {
return saveEntry(newEntryId) //--> return Mono<Boolean> true if saved false if id already exists
.doOnNext(applied -> LOGGER.info("Successfully created entry with id: {} ? {} ", newEntryId, applied))
.flatMap(applied -> !applied
//applied false shows id already exists, so try again recursively with new incremented id
? createEntry(newEntryId + 1, retryInterval)
: Mono.just(newEntryId))
.doOnError(e -> LOGGER.warn("Error creating entry with id {} ? {} : ", newEntryId, e));
.retryWhen(Retry.anyOf(RuntimeException.class)
.exponentialBackoff(Duration.ofSeconds(retryInterval), Duration.ofSeconds(retryInterval))); //-->retry on creation if any exception
}
//创建条目50次
void createEntries(){
LOGGER.info(“将创建50个条目);
createEntry(5.subscribe();
}
//方法在数据库中创建具有增量id的项
私人流量