java
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public BatchMultisiteQueryRecRetailPriceResponse asyncQueryRecRetailPrice(
BatchMultisiteQueryRecRetailPriceRequest request) {
if (request == null) {
log.error("Request is null, cannot proceed with asyncQueryRecRetailPrice");
throw new IllegalArgumentException("Request cannot be null");
}
if (mmsRecRetailPriceQueryExecutor == null) {
log.error("Executor is null, cannot execute asyncQueryRecRetailPrice");
throw new IllegalStateException("Executor is not properly initialized");
}
CompletableFuture<BatchMultisiteQueryRecRetailPriceResponse> future = CompletableFuture.supplyAsync(
() -> syncQueryRecRetailPrice(request), // 异步执行同步方法
mmsRecRetailPriceQueryExecutor
);
try {
return future.get(LeoConfig.getAsyncQueryRecRetailPriceMaxTimeMs(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 恢复中断状态,这是 Java 最佳实践
log.error("asyncQueryRecRetailPrice was interrupted while processing request: {}", request, e);
throw new RuntimeException("Operation was interrupted", e); // 抛出异常以通知调用者
} catch (ExecutionException e) {
log.error("asyncQueryRecRetailPrice execution failed for request: {}", request, e);
throw new CompletionException("Execution error in async operation", e.getCause()); // 包装异常,便于处理
} catch (TimeoutException e) {
log.error("asyncQueryRecRetailPrice timed out for request: {}", request, e);
throw new RuntimeException("Timeout occurred after " + LeoConfig.getAsyncQueryRecRetailPriceMaxTimeMs() + " ms", e);
} catch (Exception e) { // 捕获其他意外异常
log.error("Unexpected error in asyncQueryRecRetailPrice for request: {}", request, e);
throw new RuntimeException("Unexpected error during async operation", e);
}
}