摘要:耗时毫秒耗时毫秒耗时毫秒添加异步任务控制器测试异步任务控制器。
SpringCloud(第 047 篇)注解式Async配置异步任务
-
一、大致介绍二、实现步骤 2.1 添加 maven 引用包</>复制代码
1、有时候我们在处理一些任务的时候,需要开启线程去异步去处理,原有逻辑继续往下执行;
2、当遇到这种场景的时候,线程是可以将我们完成,然后在SpringCloud中也有这样的注解来支撑异步任务处理;
</>复制代码
4.0.0
springms-async
1.0-SNAPSHOT
jar
com.springms.cloud
springms-spring-cloud
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
2.2 添加应用配置文件(springms-asyncsrcmainresourcesapplication.yml)
</>复制代码
server:
port: 8345
spring:
application:
name: springms-async #全部小写
2.3 添加异步任务类(springms-asyncsrcmainjavacomspringmscloudtaskAsyncTasks.java)
</>复制代码
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 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 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 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");
}
}
2.4 添加异步任务Web控制器(springms-asyncsrcmainjavacomspringmscloudcontrollerAsyncTaskController.java)
</>复制代码
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 task1 = asyncTasks.doTaskOne();
Future task2 = asyncTasks.doTaskTwo();
Future 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;
}
}
2.5 添加微服务启动类(springms-schedulesrcmainjavacomspringmscloudMsScheduleApplication.java)
</>复制代码
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异步任务微服务 】】】】】】已启动.");
}
}
三、测试
</>复制代码
/****************************************************************************************
一、简单用户链接Mysql数据库微服务(Async实现异步调用):
1、添加注解 EnableAsync、Async 以及任务类上注解 Component ;
2、启动 springms-async 模块服务,启动1个端口;
3、然后在浏览器输入地址 http://localhost:8345/task 然后等待大约10多秒后,成功打印所有信息,一切正常;
总结:说明 Async 异步任务配置生效了;
****************************************************************************************/
四、下载地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接
欢迎关注,您的肯定是对我最大的支持!!!
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/67835.html
摘要:当前时间打印当前时间定时任务触发,操作多个添加数据,事务中任一异常,都可以正常导致数据回滚。当前时间当前时间添加微服务启动类注解式配置定时任务,不支持任务调度。 SpringCloud(第 046 篇)注解式Schedule配置定时任务,不支持任务调度 - 一、大致介绍 1、很多时候我们需要隔一定的时间去执行某个任务,为了实现这样的需求通常最普通的方式就是利用多线程来实现; 2、但是有...
摘要:注意注解能注册到服务上,是因为该注解包含了客户端的注解,该是一个复合注解。包含了客户端注解,同时也包含了断路器模块注解,还包含了网关模块。 SpringCloud(第 027 篇)集成异构微服务系统到 SpringCloud 生态圈中(比如集成 nodejs 微服务) - 一、大致介绍 1、在一些稍微复杂点系统中,往往都不是单一代码写的服务,而恰恰相反集成了各种语言写的系统,并且我们还...
摘要:添加任务成功运行任务名称添加定时任务服务定时任务服务。触发器计划列表添加测试任务类测试任务类被任务调度后执行该任务类。声明一个静态变量保存添加启动类简单微服务,不支持分布式。 SpringCloud(第 009 篇)简单 Quartz 微服务,不支持分布式 - 一、大致介绍 1、本章节仅仅只是为了测试 Quartz 在微服务中的使用情况; 2、其实若只是简单的实现任务调用而言的话,Sp...
摘要:加载配置文件失败加载配置文件失败添加定时调度任务定时调度任务添加定时调度任务定时调度任务执行的张表入数据库添加启动类简单微服务,采用注解配置分布式集群。 SpringCloud(第 054 篇)简单 Quartz-Cluster 微服务,采用注解配置 Quartz 分布式集群 - 一、大致介绍 1、因网友提到有没有采用注解式配置的Quartz例子,因此本人就贴上了这样一个样例; 2、至...
阅读 1752·2021-09-02 15:11
阅读 2049·2019-08-30 14:04
阅读 2648·2019-08-27 10:52
阅读 1650·2019-08-26 11:52
阅读 1277·2019-08-23 15:26
阅读 2727·2019-08-23 15:09
阅读 2716·2019-08-23 12:07
阅读 2304·2019-08-22 18:41