【问题标题】:Project Reactor: Obtain Size of List Contained Within a MonoProject Reactor:获取 Mono 中包含的列表的大小
【发布时间】:2021-12-27 22:03:06
【问题描述】:

我正在尝试在 project reactor 中再次做一些事情,我相信这对你们中的任何一个 project reactor 专家来说都是非常简单的! 我一直在寻找和摸索这个东西有一段时间了,感觉我又一次碰壁了。

我要做的就是确定 Mono 中包含的对象列表是否为空。

这是我目前所拥有的:

private Mono<Boolean> isLastCardForAccount(String accountId) {
    return cardService.getAccountCards(accountId)
        .hasElement();
}

我认为上述方法可能有效,但我很难弄清楚如何提取/访问返回的 Mono 中包含的“布尔值”。我想我必须以某种方式使用“订阅”,对吗? 我已经搞砸了一段时间了,但仍然没有运气。

以下是“getAccountCards”的定义方式:

public Mono<List<Card>> getAccountCards(final String accountId) {
    return cardCrudRepository.getCardsByAccountId(accountId)
        .collectList();
}

来自 CardCrudRepository:

//  @Query("SELECT * FROM card WHERE account_id = :accountId") <-Not sure if I need this
Flux<Card> getCardsByAccountId(String accountId);

最后,我如何使用“isLastCardForAccount”:

public Mono<Void> updateCardStatus(String accountId, String cardId, String cardStatus) {
    return accountService.getAccount(accountId)
        .map(Account::getClientId)
        .map(clientId -> createUpdateCardStatusServiceRequestData(clientId, cardId, cardStatus))
        .flatMap(requestData -> cartaClient.updateCardStatus(requestData)
            .then(Mono.defer(() -> isCardBeingCancelled(cardStatus) ? allCardsCancelledForAccount(accountId) ? removeAccount(accountId) : 
(isLostOrStolen(cardStatus) ? replaceCard(cardId, cardStatus).flatMap(this::updateCardNumber) : Mono.empty()) : Mono.empty())));
}

与往常一样,我们非常感谢任何和所有帮助和见解!

【问题讨论】:

  • isCardBeingCancelled(cardStatus) 返回布尔值或单声道 ??
  • 检查此代码。你会有一些想法Flux&lt;Integer&gt; integerFlux = Flux.just(1,2,3,4,5,6);integerFlux.count().subscribe(System.out::println);integerFlux.collectList().map(list-&gt;list.size()).subscribe(System.out::println);
  • 嗨@Harry。这是回答您问题的代码: private boolean isCardBeingCancelled(String cardStatus) { return isCardDamagedFraudulentOrBeingBlocked(cardStatus); } 所以答案是“布尔值”。
  • 谢谢@AjitSoman。我会看看我是否可以以某种方式使用该代码。
  • 您能用基本的英语解释一下您在then 运算符中尝试实现的逻辑吗?很难跟上

标签: spring spring-boot reactive-programming spring-webflux project-reactor


【解决方案1】:

我不确定这是否能解决问题,但这样你可以尝试编写你的逻辑

 return accountService.getAccount(accountId)
            .map(Account::getClientId)
            .map(clientId -> createUpdateCardStatusServiceRequestData(clientId, cardId, cardStatus))
            .flatMap(requestData -> cartaClient.updateCardStatus(requestData)
                    .then(Mono.defer(() ->
                            Mono.zip( 
                                    Mono.just(isCardBeingCancelled(cardStatus)),
                                    isLastCardForAccount(accountId),
                                    Mono.just( isLostOrStolen(cardStatus) )
                                    ) 
                                    .map(tuple -> {
                                                WRITE YOUR IF ELSE LOGIC 

                                            })

这个想法是使用 zip 然后使用元组来编写逻辑。元组将是 的 Tuple3 类型。我假设 isLostOrStolen(cardStatus) 返回布尔值。

【讨论】:

  • 谢谢@Harry!我去看看!
【解决方案2】:

一种方法是使用filterWhen 运算符,如下所示:

.then(Mono.defer(() -> {
    if (isCardBeingCancelled(cardStatus)) {
        return Mono.just(accountId)
            .filterWhen(this::allCardsCancelledForAccount)
            .flatMap(this::removeAccount);
    } else if (isLostOrStolen(cardStatus)) {
        return replaceCard(cardId, cardStatus).flatMap(this::updateCardNumber);
    }
    return Mono.empty();
}))

在异步过滤的情况下可以使用filterWhen。检查Which operator do I need? 参考的this 部分和这个How to filter Flux asynchronously

附带说明,这不会像您预期的那样工作:

private Mono<Boolean> isLastCardForAccount(String accountId) {
    return cardService.getAccountCards(accountId)
        .hasElement();
}

public Mono<List<Card>> getAccountCards(final String accountId) {
    return cardCrudRepository.getCardsByAccountId(accountId)
        .collectList();
}

如果没有卡,collectList() 将发出一个空的List。我会改用exists 查询:

public Mono<Boolean> isLastCardForAccount(final String accountId) {
    return cardCrudRepository.existsByAccountId(accountId);
}

【讨论】:

  • 谢谢!我将通过相应地更新代码来尝试这个!将在某个时候在这里报告!
猜你喜欢
  • 1970-01-01
  • 2019-01-07
  • 2021-06-12
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 2017-07-29
相关资源
最近更新 更多