java WebFlux:如何在onErrorResume中使用block()方法?

gijlo24d  于 2023-02-07  发布在  Java
关注(0)|答案(1)|浏览(111)

我刚接触React式编程,我希望在处理错误时将对象保存到DB中,但据我所知,调用block()方法不是最佳实践

mailFailureRepository.save(failure).block();

有没有一种方法可以在不中断流的情况下完成此操作?我想我应该重新构建线程链,但我不太明白在我的情况下如何进行
我也会很高兴任何信息资源

public Mono<Object> sendEmail(SendEmailRequest request)
          throws MailTemplateNotSupportedException, ExactTargetException {
        log.debug("Send email process started.");
        return validateRequestAndSendMail(request)
            .onErrorResume(error -> {
              if (error instanceof ExactTargetException ex) {
                MailFailure failure = MailFailure.builder()
                    .templateKey(request.getTemplateKey())
                    .templateParams(request.getTemplateParams() != null ? request.getTemplateParams().toString() : null)
                    .subscriberHash(request.getSubscriberHash())
                    .email(request.getEmail())
                    .responseStatus(ex.getStatus())
                    .responsePayload(ex.getBody())
                    .build();
                mailFailureRepository.save(failure).block();
                return Mono.error(error);
              }
              return Mono.error(error);
            });
xfyts7mz

xfyts7mz1#

为什么不保存,然后返回错误没有阻塞:

public Mono <Object> sendEmail(SendEmailRequest request)
                throws MailTemplateNotSupportedException, ExactTargetException {
            log.debug("Send email process started.");
            return validateRequestAndSendMail(request)
                    .onErrorResume(error -> {
            Mono<Object> errorMono = Mono.error(error);
            if (error instanceof ExactTargetException ex) {
                mailFailureRepository.save(buildFailureEmail(request))
                        .thenReturn(errorMono);
            }
            return errorMono;
            });
        }
        private MailFailure buildFailureEmail(SendEmailRequest request) {
            return MailFailure.builder()
                .templateKey(request.getTemplateKey())
                .templateParams(request.getTemplateParams() != null ? request.getTemplateParams().toString() : null)
                .subscriberHash(request.getSubscriberHash())
                .email(request.getEmail())
                .responseStatus(ex.getStatus())
                .responsePayload(ex.getBody())
                .build();
        }

相关问题