| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package com.zsElectric.boot.common.async;
- import com.zsElectric.boot.core.web.Result;
- import org.apache.poi.ss.formula.functions.T;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.stereotype.Service;
- import java.util.concurrent.CompletableFuture;
- /**
- * 使用@async 进行异步任务处理
- */
- @Service
- public class DirectAsyncService {
- /**
- * 使用默认线程池执行异步任务
- */
- @Async
- public void processOrderAsync(String orderId) {
- // 异步处理订单逻辑
- System.out.println("订单处理线程: " + Thread.currentThread().getName());
- }
- /**
- * 使用指定的IO密集型线程池
- */
- @Async("ioIntensiveTaskExecutor")
- public CompletableFuture<String> downloadFileAsync(String fileUrl) {
- // 异步下载文件
- return CompletableFuture.completedFuture("下载完成");
- }
- /**
- * 有返回值的异步任务
- */
- @Async("businessTaskExecutor")
- public CompletableFuture<Result<String>> heavyCalculationAsync(String data) {
- // 复杂的计算任务
- return CompletableFuture.completedFuture(Result.success("计算完成"));
- }
- }
|