一、有时候咱们在处理一些任务的时候,须要开启线程去异步去处理,原有逻辑继续往下执行; 二、当遇到这种场景的时候,线程是能够将咱们完成,而后在SpringCloud中也有这样的注解来支撑异步任务处理;
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>springms-async</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.springms.cloud</groupId> <artifactId>springms-spring-cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <!-- web模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
server: port: 8345 spring: application: name: springms-async #所有小写
package com.springms.cloud.task; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Random; import java.util.concurrent.Future; /** * Async实现异步调用。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/10/19 * */ @Component public class AsyncTasks { public static Random random = new Random(); @Async public Future<String> doTaskOne() throws Exception { System.out.println("Async, taskOne, Start..."); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); System.out.println("Async, taskOne, End, 耗时: " + (end - start) + "毫秒"); return new AsyncResult<>("AsyncTaskOne Finished"); } @Async public Future<String> doTaskTwo() throws Exception { System.out.println("Async, taskTwo, Start"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); System.out.println("Async, taskTwo, End, 耗时: " + (end - start) + "毫秒"); return new AsyncResult<>("AsyncTaskTwo Finished"); } @Async public Future<String> doTaskThree() throws Exception { System.out.println("Async, taskThree, Start"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(5000)); long end = System.currentTimeMillis(); System.out.println("Async, taskThree, End, 耗时: " + (end - start) + "毫秒"); return new AsyncResult<>("AsyncTaskThree Finished"); } }
package com.springms.cloud.controller; import com.springms.cloud.task.AsyncTasks; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.Future; /** * 测试异步任务Web控制器。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/10/19 * */ @RestController public class AsyncTaskController { @Autowired AsyncTasks asyncTasks; /** * 测试异步任务。 * * @return * @throws Exception */ @GetMapping("/task") public String task() throws Exception { long start = System.currentTimeMillis(); Future<String> task1 = asyncTasks.doTaskOne(); Future<String> task2 = asyncTasks.doTaskTwo(); Future<String> task3 = asyncTasks.doTaskThree(); while(true) { if(task1.isDone() && task2.isDone() && task3.isDone()) { // 三个任务都调用完成,退出循环等待 break; } Thread.sleep(1000); } long end = System.currentTimeMillis(); String result = "任务所有完成,总耗时:" + (end - start) + "毫秒"; return result; } }
package com.springms.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; /** * 注解式Async配置异步任务; * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/10/19 * */ @SpringBootApplication @EnableAsync public class MsAsyncApplication { public static void main(String[] args) { SpringApplication.run(MsAsyncApplication.class, args); System.out.println("【【【【【【 Async异步任务微服务 】】】】】】已启动."); } }
/**************************************************************************************** 1、简单用户连接Mysql数据库微服务(Async实现异步调用): 一、添加注解 EnableAsync、Async 以及任务类上注解 Component ; 二、启动 springms-async 模块服务,启动1个端口; 三、而后在浏览器输入地址 http://localhost:8345/task 而后等待大约10多秒后,成功打印全部信息,一切正常; 总结:说明 Async 异步任务配置生效了; ****************************************************************************************/
https://gitee.com/ylimhhmily/SpringCloudTutorial.gitjava
SpringCloudTutorial交流QQ群: 235322432git
SpringCloudTutorial交流微信群: 微信沟通群二维码图片连接web
欢迎关注,您的确定是对我最大的支持!!!spring