如何使用reactor chain中以前发布者的结果回滚/恢复?

tktrz96b  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(355)

我有一个出版商的链下面。

Mono<Void> validateCost = validateCost(buyRequest);
    Mono<Void> validateBalance = validateBalance(buyRequest);

    Mono.when(validateCost, validateBalance)
             .then(issuePerk(buyRequest))
            .flatMap(issuePerkResponse -> paymentService.doPayment(buyRequest)
                    .map(paymentDetails -> new IssueAndPayResult(issuePerkResponse.getPerkIssueId(),
                            paymentDetails)))
            .flatMap(issueAndPayResult -> saveOrder(issueAndPayResult, buyRequest));

假设我也有

Mono<Void> revertPerkIssue(IssuePerkResponse)
Mono<Void> revertPayment(IssueAndPayResult)

我想要的是
如果issueperk失败,让错误传播。
如果paymentservice.dopayment失败,请调用revertperkissue并让错误传播。
如果saveorder失败,请调用revertperkissue和revertpayment并让错误传播。
我不知道怎么做。我试过使用doonerror,但是如果任何一个mono失败,比如说issueperk,那么两个doonerror都会被调用,这不是我想要的。我也不希望issueperkresponsemono,payresultmono被重新评估,这在doonerror发生。

Mono<Void> validateCost = validateCost(buyRequest);
    Mono<Void> validateBalance = validateBalance(buyRequest);

    Mono<IssuePerkResponse> issuePerkResponseMono = Mono.when(validateCost, validateBalance)
            .then(issuePerk(buyRequest));

    Mono<IssueAndPayResult> payResultMono = issuePerkResponseMono
            .flatMap(issuePerkResponse -> paymentService.doPayment(buyRequest)
                    .map(paymentDetails -> new IssueAndPayResult(issuePerkResponse.getPerkIssueId(),
                            paymentDetails)));

    return payResultMono
            .doOnError(th -> {
                issuePerkResponseMono.flatMap(issuePerkResponse -> revertPerkIssue(issuePerkResponse))
                        .subscribe();
            })
            .flatMap(issueAndPayResult -> saveOrder(issueAndPayResult, buyRequest))
            .doOnError(th -> {
                issuePerkResponseMono.flatMap(issuePerkResponse -> revertPerkIssue(issuePerkResponse))
                        .subscribe();

                payResultMono.flatMap(issueAndPayResult -> revertPayment(issueAndPayResult))
                        .subscribe();
            });

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题