PHP速学视频免费教程(入门到精通)
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
在响应式流中,错误不再通过抛出异常来处理,而是通过错误信号(error signal)在流中传播。Mono和Flux已经内置了错误处理的概念。因此,在响应式上下文中,应避免直接抛出运行时异常,而是使用特定的操作符来处理错误。
Project Reactor提供了以下核心错误处理操作符:
特别注意: 永远不要使用onErrorContinue,因为它可能会导致难以调试的副作用和状态不一致。
在传统命令式编程中,finally块通常用于确保某些代码(如资源释放、状态保存)无论是否发生异常都会执行。在响应式流中,这种“无论成功或失败都执行”的逻辑需要巧妙地融入到流的链式操作中。这意味着你需要将“finally”逻辑分别放置在成功路径和错误处理路径上。
考虑以下场景:在处理完一个请求后,无论业务逻辑成功还是失败,都需要将某个existingData对象的状态保存回数据库。
原始问题中的非响应式尝试(伪代码):
public Mono<Response> process(Request request) { // ... 业务逻辑 ... try { var response = hitAPI(existingData); // 假设 hitAPI 是一个阻塞操作 } catch(ServerException serverException) { log.error(""); throw serverException; // 在响应式方法中抛出阻塞异常 } finally { repository.save(existingData); // 阻塞操作 } return convertToResponse(existingData, response); }
上述代码在响应式环境中存在严重问题:
响应式解决方案:
以下是符合响应式范式且能有效处理“finally”逻辑的改进代码:
import reactor.core.publisher.Mono; import org.slf4j.Logger; import org.slf4j.LoggerFactory; // 假设这些是响应式接口 interface Repository { Mono<Data> find(String id); Mono<Data> save(Data data); } interface Request { String getId(); } enum State { PENDING, COMPLETED, FAILED } class Data { String id; State state; // ... 其他字段 ... public String getId() { return id; } public State getState() { return state; } public void setState(State state) { this.state = state; } } class Response { // ... 响应字段 ... } class ServerException extends RuntimeException { public ServerException(String message) { super(message); } } public class ReactiveProcessService { private static final Logger log = LoggerFactory.getLogger(ReactiveProcessService.class); private final Repository repository; public ReactiveProcessService(Repository repository) { this.repository = repository; } // 假设 hitAPI 是一个可能阻塞的外部调用 private Response hitAPI(Data existingData) throws ServerException { // 模拟外部API调用,可能抛出 ServerException if (Math.random() < 0.3) { // 模拟30%的失败率 throw new ServerException("External API call failed for data: " + existingData.getId()); } // 模拟成功响应 return new Response(); } private Data convertToData(Request request) { Data data = new Data(); data.id = request.getId(); data.state = State.PENDING; // 初始状态 return data; } private Response convertToResponse(Data data, Response apiResponse) { // 根据数据和API响应生成最终响应 return apiResponse; // 简化处理 } public Mono<Response> process(Request request) { return repository.find(request.getId()) .flatMap(existingData -> { // 1. 检查现有数据状态,不符合条件则发出错误信号 if (existingData.getState() != State.PENDING) { return Mono.error(new RuntimeException("Data state is not PENDING. Current state: " + existingData.getState())); } else { // 2. 如果状态符合,则返回现有数据,或者更新并保存(这里简化为直接返回) // 实际情况可能需要一个 Mono.just(existingData) return Mono.just(existingData); } }) .switchIfEmpty( // 3. 如果 find 结果为空,则保存新数据 repository.save(convertToData(request)) ) .flatMap(existingData -> Mono // 4. 调用可能阻塞的外部API,使用 fromCallable 包裹以确保非阻塞执行 .fromCallable(() -> hitAPI(existingData)) // 5. doOnError: 记录 ServerException 类型的错误,错误会继续传播 .doOnError(ServerException.class, throwable -> log.error("API call failed: {}", throwable.getMessage(), throwable)) // 6. onErrorResume: 当发生任何错误时(包括 ServerException),执行“finally”逻辑(保存数据),然后重新发出原始错误 .onErrorResume(throwable -> repository.save(existingData) // 保存数据(例如,更新状态为失败) .then(Mono.error(throwable)) // 确保原始错误继续向下传播 ) // 7. flatMap (成功路径): 如果API调用成功,执行“finally”逻辑(保存数据),然后映射到最终响应 .flatMap(response -> repository.save(existingData) // 保存数据(例如,更新状态为成功) .map(updatedData -> convertToResponse(updatedData, response)) ) ); } }
代码解析:
通过遵循这些原则,你可以在Project Reactor中构建出真正健壮、高效且符合响应式范式的应用程序。
已抢7566个
抢已抢97315个
抢已抢15251个
抢已抢53924个
抢已抢198234个
抢已抢88311个
抢