我正在嘗試在基於 Project Reactor 的應用程式中的現有反應鏈中非同步執行方法。 doUpdateLayoutInAsync 方法旨在執行繁重的後台任務,但我的方法似乎沒有按預期工作。這是我目前的實作:
public Mono<Boolean> publishPackage(String branchedPackageId) { PackagePublishingMetaDTO publishingMetaDTO = new PackagePublishingMetaDTO(); publishingMetaDTO.setPublishEvent(true); return packageRepository .findById(branchedPackageId, packagePermission.getPublishPermission()) .switchIfEmpty(Mono.error(new AppsmithException( AppsmithError.ACL_NO_RESOURCE_FOUND, FieldName.PACKAGE_ID, branchedPackageId))) .flatMap(originalPackage -> { String nextVersion = PackageUtils.getNextVersion(originalPackage.getVersion()); Package packageToBePublished = constructPackageToBePublished(originalPackage); originalPackage.setVersion(nextVersion); originalPackage.setLastPublishedAt(packageToBePublished.getLastPublishedAt()); publishingMetaDTO.setOriginPackageId(branchedPackageId); publishingMetaDTO.setWorkspaceId(originalPackage.getWorkspaceId()); Mono<Void> unsetCurrentLatestMono = packageRepository.unsetLatestPackageByOriginId(originalPackage.getId(), null); Mono<Package> saveOriginalPackage = packageRepository.save(originalPackage); Mono<Package> savePackageToBePublished = packageRepository.save(packageToBePublished); return unsetCurrentLatestMono .then(Mono.zip(saveOriginalPackage, savePackageToBePublished)) .flatMap(tuple2 -> { Package publishedPackage = tuple2.getT2(); publishingMetaDTO.setPublishedPackage(publishedPackage); return modulePackagePublishableService .publishEntities(publishingMetaDTO) .flatMap(publishedModules -> { if (publishedModules.isEmpty()) { return Mono.error(new AppsmithException( AppsmithError.PACKAGE_CANNOT_BE_PUBLISHED, originalPackage.getUnpublishedPackage().getName())); } return moduleInstancePackagePublishableService .publishEntities(publishingMetaDTO) .then(Mono.defer(() -> newActionPackagePublishableService.publishEntities(publishingMetaDTO)) .then(Mono.defer(() -> actionCollectionPackagePublishableService .publishEntities(publishingMetaDTO)))); }) .then(Mono.defer(() -> autoUpgradeService.handleAutoUpgrade(publishingMetaDTO))); }) .as(transactionalOperator::transactional) .then(Mono.defer(() -> doUpdateLayoutInAsync(publishingMetaDTO))); }); } private Mono<Boolean> doUpdateLayoutInAsync(PackagePublishingMetaDTO publishingMetaDTO) { Mono<List<String>> updateLayoutsMono = Flux.fromIterable(publishingMetaDTO.getAutoUpgradedPageIds()) .flatMap(pageId -> updateLayoutService .updatePageLayoutsByPageId(pageId) .onErrorResume(throwable -> { log.warn("Update layout failed for pageId: {} with error: {}", pageId, throwable.getMessage()); return Mono.just(pageId); })) .collectList(); // Running the updateLayoutsMono task asynchronously updateLayoutsMono.subscribeOn(Schedulers.boundedElastic()).subscribe(); return Mono.just(Boolean.TRUE); }
問題: 我希望 doUpdateLayoutInAsync 在後台運行,而反應鏈的其餘部分完成。然而,該方法似乎是同步執行的,反應鏈並沒有如預期般繼續。
問題:如何確保 doUpdateLayoutInAsync 非同步運作並且不會阻止反應鏈的持續?
以上是如何在 Spring WebFlux 的反應鏈中非同步運作方法?的詳細內容。更多資訊請關注PHP中文網其他相關文章!