{"id":870,"date":"2023-03-09T21:42:58","date_gmt":"2023-03-09T13:42:58","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=870"},"modified":"2023-04-29T16:44:01","modified_gmt":"2023-04-29T08:44:01","slug":"spring-boot-implement-asynchronous-call-through-async-annotation","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/09\/spring-boot-implement-asynchronous-call-through-async-annotation\/","title":{"rendered":"Spring Boot\u901a\u8fc7@Async\u6ce8\u89e3\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528"},"content":{"rendered":"<p><code>@Aysnc<\/code>\u5176\u5b9e\u662fSpring\u5185\u7684\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u5b8c\u6210\u5bf9\u7c7b\u5185\u5355\u4e2a\u6216\u8005\u591a\u4e2a\u65b9\u6cd5\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\uff0c\u8fd9\u6837\u53ef\u4ee5\u5927\u5927\u7684\u8282\u7701\u7b49\u5f85\u8017\u65f6\u3002\u5185\u90e8\u5b9e\u73b0\u673a\u5236\u662f\u7ebf\u7a0b\u6c60\u4efb\u52a1ThreadPoolTaskExecutor\uff0c\u901a\u8fc7\u7ebf\u7a0b\u6c60\u6765\u5bf9\u914d\u7f6e<code>@Async<\/code>\u7684\u65b9\u6cd5\u6216\u8005\u7c7b\u505a\u51fa\u6267\u884c\u52a8\u4f5c\u3002<\/p>\n<h2>\u7ebf\u7a0b\u4efb\u52a1\u6c60\u914d\u7f6e<\/h2>\n<p><!-- more --><\/p>\n<p>\u6211\u4eec\u521b\u5efa\u4e00\u4e2aListenerAsyncConfiguration\uff0c\u5e76\u4e14\u4f7f\u7528<code>@EnableAsync<\/code>\u6ce8\u89e3\u5f00\u542f\u652f\u6301\u5f02\u6b65\u5904\u7406\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code class=\"language-java\">@Configuration\n@EnableAsync\npublic class ListenerAsyncConfiguration implements AsyncConfigurer {\n    \/**\n     * \u83b7\u53d6\u5f02\u6b65\u7ebf\u7a0b\u6c60\u6267\u884c\u5bf9\u8c61\n     * @return\n     *\/\n    @Override\n    public Executor getAsyncExecutor() {\n        \/\/\u4f7f\u7528Spring\u5185\u7f6e\u7ebf\u7a0b\u6c60\u4efb\u52a1\u5bf9\u8c61\n        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();\n        \/\/\u8bbe\u7f6e\u7ebf\u7a0b\u6c60\u53c2\u6570\n        taskExecutor.setCorePoolSize(5);\n        taskExecutor.setMaxPoolSize(10);\n        taskExecutor.setQueueCapacity(25);\n        taskExecutor.initialize();\n        return taskExecutor;\n    }\n\n    @Override\n    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {\n        return null;\n    }\n}<\/code><\/pre>\n<h2>\u5f02\u6b65\u8c03\u7528\u6d4b\u8bd5<\/h2>\n<p>\u4efb\u52a1\u7c7b<\/p>\n<pre><code class=\"language-java\">@Slf4j\n@Component\npublic class AsyncTask {\n    @Async\n    public void doTask1() {\n        try {\n            long start = System.currentTimeMillis();\n            Thread.sleep(3000);\n            long end = System.currentTimeMillis();\n            log.info(&quot;\u4efb\u52a11\u7ed3\u675f, \u8017\u65f6: &quot; + (end-start));\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    @Async\n    public void doTask2() {\n        try {\n            long start = System.currentTimeMillis();\n            Thread.sleep(3000);\n            long end = System.currentTimeMillis();\n            log.info(&quot;\u4efb\u52a12\u7ed3\u675f, \u8017\u65f6: &quot; + (end-start));\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n\n    @Async\n    public void doTask3() {\n        try {\n            long start = System.currentTimeMillis();\n            Thread.sleep(5000);\n            long end = System.currentTimeMillis();\n            log.info(&quot;\u4efb\u52a13\u7ed3\u675f, \u8017\u65f6: &quot; + (end-start));\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}<\/code><\/pre>\n<p>\u63a7\u5236\u5668<\/p>\n<pre><code class=\"language-java\">@Slf4j\n@RestController\npublic class AsyncController {\n    @Autowired\n    private AsyncTask asyncTask;\n\n    @RequestMapping(&quot;\/async&quot;)\n    public String async() {\n        long start = System.currentTimeMillis();\n        asyncTask.doTask1();\n        asyncTask.doTask2();\n        asyncTask.doTask3();\n        long end = System.currentTimeMillis();\n        log.info(&quot;\u8bf7\u6c42\u8017\u65f6: &quot; + (end-start));\n        return &quot;SUCCESS&quot;;\n    }\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c<\/p>\n<pre><code>\u8bf7\u6c42\u8017\u65f6: 4\n\u4efb\u52a12\u7ed3\u675f, \u8017\u65f6: 3001\n\u4efb\u52a11\u7ed3\u675f, \u8017\u65f6: 3001\n\u4efb\u52a13\u7ed3\u675f, \u8017\u65f6: 5001<\/code><\/pre>\n<h2>\u5e26\u6709\u8fd4\u56de\u503c\u7684\u65b9\u6cd5\u5982\u4f55\u4f7f\u7528@Async\u6ce8\u89e3<\/h2>\n<blockquote>\n<p>\u4f7f\u7528<code>@Async<\/code>\u6ce8\u89e3\u7684\u65b9\u6cd5\u8fd4\u56de\u503c\u4e3a<code>java.util.concurrent.Future<\/code>\u7684\u5b9e\u73b0\u7c7b<code>org.springframework.scheduling.annotation.AsyncResult<\/code>\u7c7b\u578b<\/p>\n<\/blockquote>\n<p>\u4efb\u52a1\u7c7b<\/p>\n<pre><code class=\"language-java\">@Slf4j\n@Component\npublic class AsyncTaskWithResult {\n    @Async\n    public Future&lt;String&gt; doTask1() {\n        try {\n            long start = System.currentTimeMillis();\n            Thread.sleep(3000);\/\/\u9759\u9759\u7684\u6c89\u77613\u79d2\u949f\n            long end = System.currentTimeMillis();\n            log.info(&quot;\u4efb\u52a11\u7ed3\u675f, \u8017\u65f6: &quot; + (end-start));\n            return new AsyncResult&lt;String&gt;(&quot;Aysnc task 1 is done&quot;);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n        return null;\n    }\n\n    @Async\n    public Future&lt;String&gt; doTask2() {\n        try {\n            long start = System.currentTimeMillis();\n            Thread.sleep(3000);\/\/\u9759\u9759\u7684\u6c89\u77613\u79d2\u949f\n            long end = System.currentTimeMillis();\n            log.info(&quot;\u4efb\u52a12\u7ed3\u675f, \u8017\u65f6: &quot; + (end-start));\n            return new AsyncResult&lt;String&gt;(&quot;Aysnc task 2 is done&quot;);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n        return null;\n    }\n\n    @Async\n    public Future&lt;String&gt; doTask3() {\n        try {\n            long start = System.currentTimeMillis();\n            Thread.sleep(5000);\/\/\u9759\u9759\u7684\u6c89\u77615\u79d2\u949f\n            long end = System.currentTimeMillis();\n            log.info(&quot;\u4efb\u52a13\u7ed3\u675f, \u8017\u65f6: &quot; + (end-start));\n            return new AsyncResult&lt;String&gt;(&quot;Aysnc task 3 is done&quot;);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n        return null;\n    }\n}<\/code><\/pre>\n<p>\u63a7\u5236\u5668<\/p>\n<pre><code class=\"language-java\">@Slf4j\n@RestController\npublic class AsyncController {\n    @Autowired\n    private AsyncTaskWithResult asyncTaskWithResult;\n\n    @RequestMapping(&quot;\/async_return&quot;)\n    public String asyncReturn() {\n        long start = System.currentTimeMillis();\n        Future&lt;String&gt; async1 = asyncTaskWithResult.doTask1();\n        Future&lt;String&gt; async2 = asyncTaskWithResult.doTask2();\n        Future&lt;String&gt; async3 = asyncTaskWithResult.doTask3();\n        try {\n            while (true) {\n                if (async1 != null &amp;&amp; async1.isDone()) {\n                    log.info(&quot;\u4efb\u52a11\u8fd4\u56de: &quot; + async1.get());\n                }\n                if (async2 != null &amp;&amp; async2.isDone()) {\n                    log.info(&quot;\u4efb\u52a12\u8fd4\u56de: &quot; + async2.get());\n                }\n                if (async3 != null &amp;&amp; async3.isDone()) {\n                    log.info(&quot;\u4efb\u52a13\u8fd4\u56de: &quot; + async3.get());\n                }\n                if ((async1 == null || async1.isDone()) &amp;&amp;\n                    (async2 == null || async2.isDone()) &amp;&amp;\n                    (async3 == null || async3.isDone())) {\n                    break;\n                }\n                Thread.sleep(1000);\n            }\n        } catch (Exception e) {\n\n        }\n        long end = System.currentTimeMillis();\n        log.info(&quot;\u8bf7\u6c42\u8017\u65f6: &quot; + (end-start));\n        return &quot;SUCCESS&quot;;\n    }\n}<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c<\/p>\n<pre><code>\u4efb\u52a12\u7ed3\u675f, \u8017\u65f6: 3000\n\u4efb\u52a11\u7ed3\u675f, \u8017\u65f6: 3000\n\u4efb\u52a11\u8fd4\u56de: Aysnc task 1 is done\n\u4efb\u52a12\u8fd4\u56de: Aysnc task 2 is done\n\u4efb\u52a13\u7ed3\u675f, \u8017\u65f6: 5001\n\u4efb\u52a11\u8fd4\u56de: Aysnc task 1 is done\n\u4efb\u52a12\u8fd4\u56de: Aysnc task 2 is done\n\u4efb\u52a13\u8fd4\u56de: Aysnc task 3 is done\n\u8bf7\u6c42\u8017\u65f6: 5017<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>@Aysnc\u5176\u5b9e\u662fSpring\u5185\u7684\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u5b8c\u6210\u5bf9\u7c7b\u5185\u5355\u4e2a\u6216\u8005\u591a\u4e2a\u65b9\u6cd5\u5b9e\u73b0\u5f02\u6b65\u8c03\u7528\uff0c\u8fd9\u6837\u53ef\u4ee5\u5927\u5927\u7684\u8282\u7701\u7b49\u5f85 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-870","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=870"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/870\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}