springboot實(shí)現(xiàn)異步調(diào)用@Async的示例
在后端開(kāi)發(fā)中經(jīng)常遇到一些耗時(shí)或者第三方系統(tǒng)調(diào)用的情況,我們知道Java程序一般的執(zhí)行流程是順序執(zhí)行(不考慮多線程并發(fā)的情況),但是順序執(zhí)行的效率肯定是無(wú)法達(dá)到我們的預(yù)期的,這時(shí)就期望可以并行執(zhí)行,常規(guī)的做法是使用多線程或線程池,需要額外編寫(xiě)代碼實(shí)現(xiàn)。在spring3.0后引入了@Async注解,使用該注解可以達(dá)到線程池的執(zhí)行效果,而且在開(kāi)發(fā)上非常簡(jiǎn)單。
一、概述
springboot是基于spring框架的,在springboot環(huán)境下演示@Async注解的使用方式。先看下該注解的定義,
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Async { /** * A qualifier value for the specified asynchronous operation(s). * <p>May be used to determine the target executor to be used when executing this * method, matching the qualifier value (or the bean name) of a specific * {@link java.util.concurrent.Executor Executor} or * {@link org.springframework.core.task.TaskExecutor TaskExecutor} * bean definition. * <p>When specified on a class level {@code @Async} annotation, indicates that the * given executor should be used for all methods within the class. Method level use * of {@code Async#value} always overrides any value set at the class level. * @since 3.1.2 */ String value() default ""; }
可以看到該注解只有一個(gè)屬性,那就是value,從注釋上知道value指定的是執(zhí)行該任務(wù)的線程池,也就是說(shuō)我們可以使用子定義的線程池執(zhí)行我們的任務(wù),而不是系統(tǒng)默認(rèn)的。在看該注解上的注解,
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented
也就是說(shuō)該注解可以用在方法和類(lèi)上。標(biāo)記在類(lèi)上表示類(lèi)中的所有方法都以異步方式執(zhí)行,也就是提交到線程池執(zhí)行。
二、詳述
上面簡(jiǎn)單對(duì)@Async注解進(jìn)行了解釋?zhuān)旅婵从梅ā?/p>
1、@EnableAsync注解
在springboot中要使用@Async注解必須在springboot啟動(dòng)類(lèi)上使用@EnableAsync注解,開(kāi)啟@Async注解的自動(dòng)配置,如下,
package com.example.demo; import com.example.demo.properties.ApplicationPro; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableConfigurationProperties({ApplicationPro.class}) //開(kāi)啟@Async注解的自動(dòng)配置 @EnableAsync public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
只有在啟動(dòng)類(lèi)上使用@EnableAsync注解,@Async注解才會(huì)生效。
2、@Async注解
上面使用@EnableAsync注解已經(jīng)開(kāi)啟了對(duì)@Async注解的配置,下面看具體的異步調(diào)用類(lèi),
package com.example.demo.service; import com.example.demo.Student; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; import java.util.concurrent.Future; @Service @Async public class AsyncService { public Future<Student> get(){ Student stu=new Student("1","3"); try { Thread.sleep(10000l); } catch (InterruptedException e) { e.printStackTrace(); } return AsyncResult.forValue(stu); } public void executeRemote(){ try { Thread.sleep(10000l); } catch (InterruptedException e) { e.printStackTrace(); } } }
首先,要使該類(lèi)讓spring管理必須使用@Service注解(或其他注解也可以),然后在類(lèi)上標(biāo)記@Async注解,前面說(shuō)過(guò)@Async注解可以在方法或類(lèi)上使用,在類(lèi)上使用則表示類(lèi)中的所有方法均使用異步執(zhí)行的方式。異步執(zhí)行類(lèi)中有兩個(gè)方法,每個(gè)方法為了演示執(zhí)行的耗時(shí)操作均睡眠10s。這兩個(gè)方法一個(gè)是有返回值的,另一個(gè)是無(wú)返回值的,重點(diǎn)看有返回值的,
public Future<Student> get(){ Student stu=new Student("1","3"); try { Thread.sleep(10000l); } catch (InterruptedException e) { e.printStackTrace(); } return AsyncResult.forValue(stu); }
為什么方法的返回值是Future,在@Async注釋上有下面這句話,
從上面的注解正好可以說(shuō)明返回Future是沒(méi)問(wèn)題,但是我們的方法就是一個(gè)普通的方法,要怎么才能返回Future類(lèi)那,不慌,spring針對(duì)@Async注解提供了AsyncResult類(lèi),從類(lèi)名就知道該類(lèi)就是為了@Async注解準(zhǔn)備的,
使用其中的forValue方法,便可以返回一個(gè)帶有泛型的Future類(lèi)了。
看下測(cè)試類(lèi),
package com.example.demo.controller; import com.example.demo.Student; import com.example.demo.service.AsyncService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @Controller @RequestMapping("async") public class ControllerAsyncTest { @Autowired private AsyncService asyncService; @RequestMapping("/test") @ResponseBody public Student get(){ try { long start=System.currentTimeMillis(); //調(diào)用帶有返回值的get方法 Future<Student> result=asyncService.get(); //調(diào)用無(wú)返回值的executeRemote方法 asyncService.executeRemote(); Student student=result.get(); long end=System.currentTimeMillis(); System.out.println("執(zhí)行時(shí)間:"+(end-start)); return student; } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } return null; } }
測(cè)試類(lèi)就是一個(gè)簡(jiǎn)單的controller,調(diào)用了get和executeRemote方法,這兩個(gè)方法分別會(huì)睡眠10s,而且get會(huì)有返回值,下面看是否可以拿到get的返回值,并看下調(diào)用這兩個(gè)方法的時(shí)間,
可以成功拿到返回值,看執(zhí)行時(shí)間,
2020-12-12 21:37:43.556 INFO 11780 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
執(zhí)行時(shí)間:10006
執(zhí)行時(shí)間是10006ms,也就是10s多,按照上面的分析兩個(gè)方法分別睡眠了10s,如果同步執(zhí)行那肯定是20s,把@Async注解去掉看執(zhí)行時(shí)間,
2020-12-12 21:41:07.840 INFO 11584 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
執(zhí)行時(shí)間:20001
執(zhí)行時(shí)間是20001ms,算上兩個(gè)方法睡眠的時(shí)間,和測(cè)試類(lèi)本身的時(shí)間,20001ms是沒(méi)錯(cuò)的。從這里可以看出@Async注解的作用,把每個(gè)方法當(dāng)作任務(wù)提交給了線程池,提高了任務(wù)執(zhí)行的時(shí)間。
另外,在獲取異步的執(zhí)行結(jié)果使用了下面的方法,
Future<Student> result=asyncService.get(); asyncService.executeRemote(); //獲得執(zhí)行結(jié)果 Student student=result.get();
由于在主線程要獲得任務(wù)的執(zhí)行結(jié)果,使用Future類(lèi)的get方法獲得結(jié)果,該結(jié)果需要等到任務(wù)執(zhí)行完以后才可以獲得。
三、總結(jié)
本文講解了異步調(diào)用@Async注解的使用,
1、使用@EnableAsync注解開(kāi)啟對(duì)@Async注解的支持;
2、在類(lèi)或方法上使用@Async注解;
到此這篇關(guān)于springboot實(shí)現(xiàn)異步調(diào)用@Async的示例的文章就介紹到這了,更多相關(guān)springboot 異步調(diào)用@Async內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 簡(jiǎn)述Springboot @Async 異步方法
- SpringBoot用@Async注解實(shí)現(xiàn)異步任務(wù)
- 詳解springboot使用異步注解@Async獲取執(zhí)行結(jié)果的坑
- SpringBoot異步使用@Async的原理以及線程池配置詳解
- springboot?@Async?注解如何實(shí)現(xiàn)方法異步
- SpringBoot使用@Async注解處理異步事件的方法
- Springboot如何使用@Async實(shí)現(xiàn)異步任務(wù)
- SpringBoot使用@Async注解實(shí)現(xiàn)異步調(diào)用
- springboot異步@Async的使用及失效場(chǎng)景介紹
相關(guān)文章
SpringBoot多數(shù)據(jù)源配置方式以及報(bào)錯(cuò)問(wèn)題的解決
這篇文章主要介紹了SpringBoot多數(shù)據(jù)源配置方式以及報(bào)錯(cuò)問(wèn)題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07springboot2如何集成ElasticSearch6.4.3
這篇文章主要介紹了springboot2如何集成ElasticSearch6.4.3問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07淺談springboot之JoinPoint的getSignature方法
這篇文章主要介紹了springboot之JoinPoint的getSignature方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06淺析javax.servlet.Servlet,ServletContext接口
本篇文章是對(duì)javax.servlet.Servlet,ServletContext接口進(jìn)行了纖細(xì)的分析介紹,需要的朋友參考下2013-07-07MyBatis中RowBounds實(shí)現(xiàn)內(nèi)存分頁(yè)
RowBounds是MyBatis提供的一種內(nèi)存分頁(yè)方式,適用于小數(shù)據(jù)量的分頁(yè)場(chǎng)景,本文就來(lái)詳細(xì)的介紹一下,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12JDK12的新特性之CompactNumberFormat詳解
這篇文章主要介紹了JDK12的新特性之CompactNumberFormat,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05