springboot 异步任务
Spring Boot 揭秘与实战(七) 实用技术篇 - 异步任务
拓展阅读: http://www.jianshu.com/p/86e915d616c4
发表于 2017-01-06 | Spring框架 | SpringBoot
Spring 对异步任务具有很好的支持。这篇文章,我们透过 Spring Boot 来讲解下单发服务模式和请求应答模式。
Spring Boot 集成异步任务
在 Spring Boot 中使用 @EnableAsync 开启异步支持。
- @Configuration
- @EnableAsync
- public class AsyncConfig {
- }
现在,我们在 Spring Boot 中,我们只需要通过使用 @Async 注解就能将原来的同步方法变为异步方法。
单发服务模式
多个服务之间逻辑上不存在相互依赖关系,执行先后顺序没有严格的要求,逻辑上可以被并行执行。对于单发服务只有请求,没有应答,很容易设计成异步的。发起服务调用后。立即返回,不需要同步阻塞等待应答。
- @Service
- public class MsgServer {
- @Async
- public void sendA() throws Exception {
- System.out.println("send A");
- Long startTime = System.currentTimeMillis();
- Thread.sleep(2000);
- Long endTime = System.currentTimeMillis();
- System.out.println("耗时:" + (endTime - startTime));
- }
- @Async
- public void sendB() throws Exception {
- System.out.println("send B");
- Long startTime = System.currentTimeMillis();
- Thread.sleep(2000);
- Long endTime = System.currentTimeMillis();
- System.out.println("耗时:" + (endTime - startTime));
- }
- }
此时,因为我们添加了 @Async 注解,它就变成了异步方法。
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = WebMain.class)
- public class AsyncTest {
- @Autowired
- private MsgServer msgServer;
- @Test
- public void test() throws Exception {
- msgServer.sendA();
- msgServer.sendB();
- }
- }
请求应答模式
对于,请求的内容,需要应答,例如我们需要在多个方法调用都完成后,才进行接下来的操作,此时我们可以利用 Java 的 Future-Listener 机制来实现异步服务调用。
- @Service
- public class MsgFutureServer {
- public static Random random = new Random();
- @Async
- public Future<String> sendA() throws Exception {
- System.out.println("send A");
- Long startTime = System.currentTimeMillis();
- Thread.sleep(2000);
- Long endTime = System.currentTimeMillis();
- System.out.println("耗时:" + (endTime - startTime));
- return new AsyncResult<String>("success");
- }
- @Async
- public Future<String> sendB() throws Exception {
- System.out.println("send B");
- Long startTime = System.currentTimeMillis();
- Thread.sleep(2000);
- Long endTime = System.currentTimeMillis();
- System.out.println("耗时:" + (endTime - startTime));
- return new AsyncResult<String>("success");
- }
- }
下面的单元测试,在等待完成两个异步任务后,再统计具体耗时时长。
- @RunWith(SpringJUnit4ClassRunner.class)
- @SpringApplicationConfiguration(classes = WebMain.class)
- public class AsyncFutureTest {
- @Autowired
- private MsgFutureServer msgFutureServer;
- @Test
- public void test() throws Exception {
- long startTime = System.currentTimeMillis();
- Future<String> task1 = msgFutureServer.sendA();
- Future<String> task2 = msgFutureServer.sendB();
- while(true) {
- if(task1.isDone() && task2.isDone() ) {
- break;
- }
- }
- long endTime = System.currentTimeMillis();
- System.out.println("总耗时:" + (endTime - startTime));
- }
- }
源代码
相关示例完整代码: springboot-action
springboot 异步任务的更多相关文章
- 新手也能看懂的 SpringBoot 异步编程指南
本文已经收录自 springboot-guide : https://github.com/Snailclimb/springboot-guide (Spring Boot 核心知识点整理. 基于 S ...
- springboot异步线程(二)
前言 本篇文章针对上篇文章springboot异步线程,有一位大佬在评论中提出第一点是错误的,当时看到了这个问题,最近刚好有空,针对第一点的问题去搜索了不少的文章: 问题 我在文章中第一点去验证:Sc ...
- springboot异步线程
前言 最近项目中出现了一个问题,发现自己的定时器任务在线上没有执行,但是在线下测试时却能执行,最后谷歌到了这篇文章SpringBoot踩坑日记-定时任务不定时了?; 本篇文章主要以自己在项目中遇到的问 ...
- SpringBoot异步编程
异步调用:当我们执行一个方法时,假如这个方法中有多个耗时的任务需要同时去做,而且又不着急等待这个结果时可以让客户端立即返回然后,后台慢慢去计算任务.当然你也可以选择等这些任务都执行完了,再返回给客户端 ...
- SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数
SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...
- SpringBoot 异步 定时任务 邮件
springboot异步 一: 在 MyConfiguration.java 中开启注解 @Configuration//指明当前类是一个配置类:就是来替代之前的Spring配置文件@EnableAs ...
- 带着新人学springboot的应用09(springboot+异步任务)
本来想说说检索的,不过不知道什么鬼,下载ElasticSearch太慢了,还是放一下,后面有机会再补上!今天就说个简单的东西,来说说任务. 什么叫做任务呢?其实就是类中实现了一个什么功能的方法.常见的 ...
- springboot 异步调用Async使用方法
引言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在spring 3. ...
- SpringBoot 异步输出 Logback 日志
一.介绍 1.1 Logback Logback是由log4j创始人设计的另一个开源日志组件,它分为下面下个模块: logback-core:其它两个模块的基础模块 logback-classic:它 ...
随机推荐
- Mysql配置文件my.ini详解
以下是Mysql数据库服务器配置文件my.ini的详细配置.应用场合是InnoDB引擎,2核CPU, 32位SUSE. [client] #password = your_password port ...
- 【资料】wod旗帜,纹章
物品 徽章 旗帜 掉落地点 备注 火焰纹章 法师与怪物 火焰魔法.魔法攻防 雄鹰纹章 受诅咒的遗迹 弩系相关 盗贼纹章 捉迷藏 偷袭.匕首.割喉.近攻防 守夜人的纹章 酒馆里平静的一天 钝器.双打.旋 ...
- .NET:为什么不能在子类或外部发布C#事件
背景 一个朋友问了一个问题:“为什么不能在子类或外部发布C#事件?”,我说我不知道,要看看生产的IL代码,下面我们看看. 测试 代码 using System; using System.Collec ...
- JavaScript 中 Object ,Prototype 相关的属性和方法
span { font-family: 'Consolas'; font-size: 10pt; color: #ffffff; } .sc0 { } .sc2 { color: #c0c0c0; } ...
- 数学图形(1.40)T_parameter
不记得在哪搞了个数学公式生成的图形. vertices = t = to (*PI) r = 2.0 x = r*(*cos(t) - cos(*t)) y = r*(*sin(t) - sin(*t ...
- Windows2003 Webshell默认权限
0×00 前言 0×01 Windows2003默认配置 0×02 Windows2003典型配置的权限 0×03 cmd运行的条件 0×00 前言 这一章主要讲解关于我们刚拿到webshell的 ...
- C#使用DirectShow播放视频文件 [转]
原文网址:http://blog.csdn.net/openzpc/article/details/48442751 最近在开发一个视频播放软件,主要要求就是循环播放多个视频文件,并且要求两个视频文件 ...
- 8个使用JavaScript展示图片解决方案
1. JonDesign’s SmoothGallery 2.0 SmoothGallery demo 2. (E)2 Photo Gallery (E)2 Photo Gallery demo 3. ...
- JS中常用坐标offset、scroll、client的区别
在IE中scrollWidth:获取对象的滚动宽度scrollHeight: 获取对象的滚动高度.scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离scrollTop ...
- win10 修改cmd终端编码格式为utf8
最近在使用ssh 连接服务器时,好多中文显示为乱码,查明原因,是因为自己cmd终端编码给是为gbk,而服务器编码格式为utf8,所以需要修改cmd终端编码格式为utf8,但是网上看到好多解决方案是 1 ...