Spring Boot(十)使用@Async实现异步调用



项目GitHub地址 :

https://github.com/FrameReserve/TrainingBoot


Spring Boot(十)使用@Async实现异步调用 ,标记地址:

https://github.com/FrameReserve/TrainingBoot/releases/tag/0.0.10



Spring Boot启动类,增加@EnableAsync注解配置:

src/main/java/com/training/SpringBootServlet.java

[java]  view plain  copy
  1. package com.training;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.boot.builder.SpringApplicationBuilder;  
  6. import org.springframework.boot.web.support.SpringBootServletInitializer;  
  7. import org.springframework.scheduling.annotation.EnableAsync;  
  8.   
  9. @SpringBootApplication  
  10. @EnableAsync  
  11. public class SpringBootServlet extends SpringBootServletInitializer {  
  12.   
  13.     // jar启动  
  14.     public static void main(String[] args) {  
  15.         SpringApplication.run(SpringBootServlet.class, args);  
  16.     }  
  17.   
  18.     // tomcat war启动  
  19.     @Override  
  20.     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
  21.         return application.sources(SpringBootServlet.class);  
  22.     }  
  23.   
  24. }  



扫描二维码关注公众号,回复: 2822655 查看本文章

测试:


增加异步方法Service,线程休眠:

[java]  view plain  copy
  1. package com.training.async.service.impl;  
  2.   
  3. import java.util.Random;  
  4. import java.util.concurrent.Future;  
  5.   
  6. import org.springframework.scheduling.annotation.Async;  
  7. import org.springframework.scheduling.annotation.AsyncResult;  
  8. import org.springframework.stereotype.Service;  
  9.   
  10. import com.training.async.service.DemoAsyncService;  
  11.   
  12. @Service  
  13. public class DemoAsyncServiceImpl implements DemoAsyncService {  
  14.   
  15.     public static Random random =new Random();  
  16.   
  17.     @Async  
  18.     public Future<String> doTaskOne() throws Exception {  
  19.         System.out.println("开始做任务一");  
  20.         long start = System.currentTimeMillis();  
  21.         Thread.sleep(random.nextInt(10000));  
  22.         long end = System.currentTimeMillis();  
  23.         System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");  
  24.         return new AsyncResult<>("任务一完成");  
  25.     }  
  26.   
  27.     @Async  
  28.     public Future<String> doTaskTwo() throws Exception {  
  29.         System.out.println("开始做任务二");  
  30.         long start = System.currentTimeMillis();  
  31.         Thread.sleep(random.nextInt(10000));  
  32.         long end = System.currentTimeMillis();  
  33.         System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");  
  34.         return new AsyncResult<>("任务二完成");  
  35.     }  
  36.   
  37.     @Async  
  38.     public Future<String> doTaskThree() throws Exception {  
  39.         System.out.println("开始做任务三");  
  40.         long start = System.currentTimeMillis();  
  41.         Thread.sleep(random.nextInt(10000));  
  42.         long end = System.currentTimeMillis();  
  43.         System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");  
  44.         return new AsyncResult<>("任务三完成");  
  45.     }  
  46.       
  47. }  


调用异步测试测试,查看控制台输出执行顺序:

[java]  view plain  copy
  1. package com.training.async.controller;  
  2.   
  3. import io.swagger.annotations.ApiOperation;  
  4.   
  5. import java.util.concurrent.Future;  
  6.   
  7. import javax.annotation.Resource;  
  8.   
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10. import org.springframework.web.bind.annotation.RequestMethod;  
  11. import org.springframework.web.bind.annotation.ResponseBody;  
  12. import org.springframework.web.bind.annotation.RestController;  
  13.   
  14. import com.training.async.service.DemoAsyncService;  
  15. import com.training.core.dto.ResultDataDto;  
  16.   
  17. @RestController  
  18. @RequestMapping(value="/async")   
  19. public class DemoAsyncController {  
  20.   
  21.     @Resource  
  22.     private DemoAsyncService demoAsyncService;  
  23.   
  24.     /** 
  25.      * 测试异步方法调用顺序 
  26.      */  
  27.     @ApiOperation(value="测试异步方法调用顺序", notes="getEntityById")  
  28.     @RequestMapping(value = "/getTestDemoAsync", method = RequestMethod.GET)  
  29.     public @ResponseBody ResultDataDto getEntityById() throws Exception {  
  30.           
  31.         long start = System.currentTimeMillis();  
  32.   
  33.         Future<String> task1 = demoAsyncService.doTaskOne();  
  34.         Future<String> task2 = demoAsyncService.doTaskTwo();  
  35.         Future<String> task3 = demoAsyncService.doTaskThree();  
  36.   
  37.         while(true) {  
  38.             if(task1.isDone() && task2.isDone() && task3.isDone()) {  
  39.                 // 三个任务都调用完成,退出循环等待  
  40.                 break;  
  41.             }  
  42.             Thread.sleep(1000);  
  43.         }  
  44.   
  45.         long end = System.currentTimeMillis();  
  46.   
  47.         System.out.println("任务全部完成,总耗时:" + (end - start) + "毫秒");  
  48.         return ResultDataDto.addSuccess();  
  49.     }  
  50. }  

项目GitHub地址 :

https://github.com/FrameReserve/TrainingBoot


Spring Boot(十)使用@Async实现异步调用 ,标记地址:

https://github.com/FrameReserve/TrainingBoot/releases/tag/0.0.10



Spring Boot启动类,增加@EnableAsync注解配置:

src/main/java/com/training/SpringBootServlet.java

[java]  view plain  copy
  1. package com.training;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.boot.builder.SpringApplicationBuilder;  
  6. import org.springframework.boot.web.support.SpringBootServletInitializer;  
  7. import org.springframework.scheduling.annotation.EnableAsync;  
  8.   
  9. @SpringBootApplication  
  10. @EnableAsync  
  11. public class SpringBootServlet extends SpringBootServletInitializer {  
  12.   
  13.     // jar启动  
  14.     public static void main(String[] args) {  
  15.         SpringApplication.run(SpringBootServlet.class, args);  
  16.     }  
  17.   
  18.     // tomcat war启动  
  19.     @Override  
  20.     protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
  21.         return application.sources(SpringBootServlet.class);  
  22.     }  
  23.   
  24. }  



测试:


增加异步方法Service,线程休眠:

[java]  view plain  copy
  1. package com.training.async.service.impl;  
  2.   
  3. import java.util.Random;  
  4. import java.util.concurrent.Future;  
  5.   
  6. import org.springframework.scheduling.annotation.Async;  
  7. import org.springframework.scheduling.annotation.AsyncResult;  
  8. import org.springframework.stereotype.Service;  
  9.   
  10. import com.training.async.service.DemoAsyncService;  
  11.   
  12. @Service  
  13. public class DemoAsyncServiceImpl implements DemoAsyncService {  
  14.   
  15.     public static Random random =new Random();  
  16.   
  17.     @Async  
  18.     public Future<String> doTaskOne() throws Exception {  
  19.         System.out.println("开始做任务一");  
  20.         long start = System.currentTimeMillis();  
  21.         Thread.sleep(random.nextInt(10000));  
  22.         long end = System.currentTimeMillis();  
  23.         System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");  
  24.         return new AsyncResult<>("任务一完成");  
  25.     }  
  26.   
  27.     @Async  
  28.     public Future<String> doTaskTwo() throws Exception {  
  29.         System.out.println("开始做任务二");  
  30.         long start = System.currentTimeMillis();  
  31.         Thread.sleep(random.nextInt(10000));  
  32.         long end = System.currentTimeMillis();  
  33.         System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");  
  34.         return new AsyncResult<>("任务二完成");  
  35.     }  
  36.   
  37.     @Async  
  38.     public Future<String> doTaskThree() throws Exception {  
  39.         System.out.println("开始做任务三");  
  40.         long start = System.currentTimeMillis();  
  41.         Thread.sleep(random.nextInt(10000));  
  42.         long end = System.currentTimeMillis();  
  43.         System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");  
  44.         return new AsyncResult<>("任务三完成");  
  45.     }  
  46.       
  47. }  


调用异步测试测试,查看控制台输出执行顺序:

[java]  view plain  copy
  1. package com.training.async.controller;  
  2.   
  3. import io.swagger.annotations.ApiOperation;  
  4.   
  5. import java.util.concurrent.Future;  
  6.   
  7. import javax.annotation.Resource;  
  8.   
  9. import org.springframework.web.bind.annotation.RequestMapping;  
  10. import org.springframework.web.bind.annotation.RequestMethod;  
  11. import org.springframework.web.bind.annotation.ResponseBody;  
  12. import org.springframework.web.bind.annotation.RestController;  
  13.   
  14. import com.training.async.service.DemoAsyncService;  
  15. import com.training.core.dto.ResultDataDto;  
  16.   
  17. @RestController  
  18. @RequestMapping(value="/async")   
  19. public class DemoAsyncController {  
  20.   
  21.     @Resource  
  22.     private DemoAsyncService demoAsyncService;  
  23.   
  24.     /** 
  25.      * 测试异步方法调用顺序 
  26.      */  
  27.     @ApiOperation(value="测试异步方法调用顺序", notes="getEntityById")  
  28.     @RequestMapping(value = "/getTestDemoAsync", method = RequestMethod.GET)  
  29.     public @ResponseBody ResultDataDto getEntityById() throws Exception {  
  30.           
  31.         long start = System.currentTimeMillis();  
  32.   
  33.         Future<String> task1 = demoAsyncService.doTaskOne();  
  34.         Future<String> task2 = demoAsyncService.doTaskTwo();  
  35.         Future<String> task3 = demoAsyncService.doTaskThree();  
  36.   
  37.         while(true) {  
  38.             if(task1.isDone() && task2.isDone() && task3.isDone()) {  
  39.                 // 三个任务都调用完成,退出循环等待  
  40.                 break;  
  41.             }  
  42.             Thread.sleep(1000);  
  43.         }  
  44.   
  45.         long end = System.currentTimeMillis();  
  46.   
  47.         System.out.println("任务全部完成,总耗时:" + (end - start) + "毫秒");  
  48.         return ResultDataDto.addSuccess();  
  49.     }  
  50. }  

猜你喜欢

转载自blog.csdn.net/u014398624/article/details/79142125