DirectAsyncService.java 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package com.zsElectric.boot.common.async;
  2. import com.zsElectric.boot.core.web.Result;
  3. import org.apache.poi.ss.formula.functions.T;
  4. import org.springframework.scheduling.annotation.Async;
  5. import org.springframework.stereotype.Service;
  6. import java.util.concurrent.CompletableFuture;
  7. /**
  8. * 使用@async 进行异步任务处理
  9. */
  10. @Service
  11. public class DirectAsyncService {
  12. /**
  13. * 使用默认线程池执行异步任务
  14. */
  15. @Async
  16. public void processOrderAsync(String orderId) {
  17. // 异步处理订单逻辑
  18. System.out.println("订单处理线程: " + Thread.currentThread().getName());
  19. }
  20. /**
  21. * 使用指定的IO密集型线程池
  22. */
  23. @Async("ioIntensiveTaskExecutor")
  24. public CompletableFuture<String> downloadFileAsync(String fileUrl) {
  25. // 异步下载文件
  26. return CompletableFuture.completedFuture("下载完成");
  27. }
  28. /**
  29. * 有返回值的异步任务
  30. */
  31. @Async("businessTaskExecutor")
  32. public CompletableFuture<Result<String>> heavyCalculationAsync(String data) {
  33. // 复杂的计算任务
  34. return CompletableFuture.completedFuture(Result.success("计算完成"));
  35. }
  36. }