{"id":2191,"date":"2022-02-21T09:07:22","date_gmt":"2022-02-21T01:07:22","guid":{"rendered":"https:\/\/199604.com\/?p=2191"},"modified":"2022-02-21T09:07:22","modified_gmt":"2022-02-21T01:07:22","slug":"springboot%e5%bc%82%e6%ad%a5%e6%96%b9%e6%b3%95-async","status":"publish","type":"post","link":"https:\/\/199604.com\/2191","title":{"rendered":"SpringBoot\u5f02\u6b65\u65b9\u6cd5&#8211;Async"},"content":{"rendered":"<h1>SpringBoot\u5f02\u6b65\u65b9\u6cd5&#8211;Async<\/h1>\n<h2>1\u3001\u6211\u4eec\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5f02\u6b65\u65b9\u6cd5<\/h2>\n<p>\u5f02\u6b65\u8c03\u7528\u901a\u5e38\u7528\u5728\u53d1\u77ed\u4fe1\u3001\u53d1\u9001\u90ae\u4ef6\u3001\u6d88\u606f\u63a8\u9001 \u3001\u8fd0\u7ef4\u51cc\u6668\u81ea\u52a8\u5316\u64cd\u4f5c\u7b49\uff0c\u8fd9\u4e9b\u573a\u666f\u5b9e\u65f6\u6027\u8981\u6c42\u4e0d\u9ad8\uff0c\u5927\u591a\u90fd\u662f\u63a8\u5e7f\u7edf\u8ba1\u7b49\u670d\u52a1\u3002<br \/>\n \u6211\u4eec\u91c7\u7528\u5f02\u6b65\u7684\u65b9\u5f0f\u6765\u5904\u7406\u8fd9\u6837<strong>\u8017\u65f6<\/strong> \u6216 <strong>\u5b9e\u65f6\u6027\u8981\u6c42\u4e0d\u9ad8<\/strong>\u7684\u8bf7\u6c42\uff0c\u5de5\u4f5c\u7ebf\u7a0b\u53ef\u4ee5\u8ba9\u540e\u53f0\u7ebf\u7a0b\u6765\u63a5\u624b\uff0c\u81ea\u5df1\u53ef\u4ee5\u53ca\u65f6\u5730\u88ab\u91ca\u653e\u5230\u7ebf\u7a0b\u6c60\u4e2d\u7528\u4e8e\u8fdb\u884c\u540e\u7eed\u8bf7\u6c42\u7684\u5904\u7406\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u6574\u4e2a\u670d\u52a1\u5668\u7684\u541e\u5410\u80fd\u529b\u3002<\/p>\n<h2>springboot\u4e2d\u5982\u4f55\u5b9e\u73b0\u5f02\u6b65\u65b9\u6cd5<\/h2>\n<h6>\u6b65\u9aa4\u4e00\uff1a SpringApplication\u542f\u52a8\u7c7b\u4e0a\u6dfb\u52a0@EnableAsync\u6ce8\u89e3<\/h6>\n<pre><code class=\"language-java \">@SpringBootApplication\n@EnableAsync\npublic class Application {\n\n    public static void main(String[] args) {\n        SpringApplication.run(Application.class, args);\n    }\n\n}\n<\/code><\/pre>\n<h4>\u6b65\u9aa4\u4e8c\uff1a \u914d\u7f6e\u7ebf\u7a0b\u6c60<\/h4>\n<p><strong>\u6ce8\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u662f\u5c11\u91cf\u5f02\u6b65\u4efb\u52a1\u53ef\u4e0d\u8fdb\u884c\u6b64\u6b65\u9aa4\u914d\u7f6e\uff0c\u4fdd\u6301springboot\u9ed8\u8ba4\u914d\u7f6e\u5373\u53ef\u3002<\/li>\n<li>\u5982\u679c\u662f\u6279\u91cf\u5f02\u6b65\u4efb\u52a1\u9700\u8981\u8fdb\u884c\u7ebf\u7a0b\u6c60\u81ea\u5b9a\u4e49\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<p>\u65b9\u6cd5\u4e00\uff1a\u91cd\u5199\u914d\u7f6e\u7c7b\uff1a<\/p>\n<pre><code class=\"language-java \">@Configuration\n@Slf4j\npublic class AsyncConfig implements AsyncConfigurer {\n\n    \/**\n     * \u6838\u5fc3\u7ebf\u7a0b\u6570\u4e3a\u670d\u52a1\u5668\u7684cpu\u6838\u5fc3\u6570\n     *\/\n    private static int corePoolSize = Runtime.getRuntime().availableProcessors();\n    \/**\n     * \u7ebf\u7a0b\u6c60\u4e2d\u5141\u8bb8\u7684\u6700\u5927\u7ebf\u7a0b\u6570\n     *\/\n    private static int maxPoolSize = 2 * corePoolSize + 1;\n    \/**\n     * \u5de5\u4f5c\u961f\u5217\u5927\u5c0f\n     *\/\n    private static int queueCapacity = 5000;\n\n\n    @Override\n    public Executor getAsyncExecutor() {\n        log.info(\"\u6838\u5fc3\u7ebf\u7a0b\u6570: \"+ corePoolSize + \" \u6700\u5927\u7ebf\u7a0b\u6570: \" + maxPoolSize);\n        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();\n        \/\/\u6838\u5fc3\u7ebf\u7a0b\u6570\n        executor.setCorePoolSize(corePoolSize);\n        \/\/\u6700\u5927\u7ebf\u7a0b\u6570\n        executor.setMaxPoolSize(maxPoolSize);\n        \/\/\u961f\u5217\u5927\u5c0f\n        executor.setQueueCapacity(queueCapacity);\n        executor.setWaitForTasksToCompleteOnShutdown(true);\n        executor.setThreadNamePrefix(\"glj-task-\");\n        \/\/ \u62d2\u7edd\u7b56\u7565\n        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());\n        executor.initialize();\n        return executor;\n    }\n}\n<\/code><\/pre>\n<p>\u65b9\u6cd5\u4e8c\uff0c\u91cd\u5199\uff1a<\/p>\n<pre><code class=\"language-java \">@Configuration\n@Slf4j\npublic class AsyncConfig implements AsyncConfigurer {\n\n    \/**\n     * \u6838\u5fc3\u7ebf\u7a0b\u6570\u4e3a\u670d\u52a1\u5668\u7684cpu\u6838\u5fc3\u6570\n     *\/\n    private static int corePoolSize = Runtime.getRuntime().availableProcessors();\n    \/**\n     * \u7ebf\u7a0b\u6c60\u4e2d\u5141\u8bb8\u7684\u6700\u5927\u7ebf\u7a0b\u6570\n     *\/\n    private static int maxPoolSize = 2 * corePoolSize + 1;\n    \/**\n     * \u5de5\u4f5c\u961f\u5217\u5927\u5c0f\n     *\/\n    private static int queueCapacity = 5000;\n\n\n    @Bean\n    public ThreadPoolTaskExecutor taskExecutor(){\n        log.info(\"\u6838\u5fc3\u7ebf\u7a0b\u6570: \"+ corePoolSize + \" \u6700\u5927\u7ebf\u7a0b\u6570: \" + maxPoolSize);\n        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();\n        \/\/\u6838\u5fc3\u7ebf\u7a0b\u6570\n        executor.setCorePoolSize(corePoolSize);\n        \/\/\u6700\u5927\u7ebf\u7a0b\u6570\n        executor.setMaxPoolSize(maxPoolSize);\n        \/\/\u961f\u5217\u5927\u5c0f\n        executor.setQueueCapacity(queueCapacity);\n        executor.setWaitForTasksToCompleteOnShutdown(true);\n        executor.setThreadNamePrefix(\"glj-task-\");\n        \/\/ \u62d2\u7edd\u7b56\u7565\n        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());\n        executor.initialize();\n        return executor;\n    }\n}\n<\/code><\/pre>\n<p>\u65b9\u6cd5\u4e09\uff0c\u5728\u914d\u7f6e\u6587\u4ef6(application.properties)\u914d\u7f6e<\/p>\n<pre><code class=\"language-properties \">#Spring boot \u914d\u7f6e\u7ebf\u7a0b\u6c60\nspring.task.execution.pool.max-size=17\nspring.task.execution.thread-name-prefix=glj-task-\nspring.task.execution.pool.core-size=8\nspring.task.execution.pool.queue-capacity=5000\n<\/code><\/pre>\n<p>ThreadPoolTaskExecutor\u5c5e\u6027\u89e3\u91ca\u3002<\/p>\n<ul>\n<li>poolSize\uff1a \u7ebf\u7a0b\u6c60\u4e2d\u5f53\u524d\u7ebf\u7a0b\u7684\u6570\u91cf\u3002<\/li>\n<li>corePoolSize: \u6838\u5fc3\u7ebf\u7a0b\u6570\u3002<\/li>\n<li>maxPoolSize: \u6700\u5927\u7ebf\u7a0b\u6570\u3002<\/li>\n<li>queueCapacity\uff1a\u4efb\u52a1\u961f\u5217\u5bb9\u91cf\u3002<\/li>\n<\/ul>\n<blockquote><p>\n  1\u3001poolSize&lt;corePoolSize\uff1a\u76f4\u63a5\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\u5904\u7406\u65b0\u6765\u7684\u4efb\u52a1\u3002<br \/>\n   2\u3001poolSize>=corePoolSize &amp;&amp; \u4efb\u52a1\u961f\u5217\u672a\u6ee1\u65f6\uff1a\u65b0\u6765\u7684\u4efb\u52a1\u52a0\u5165\u4efb\u52a1\u961f\u5217\u3002<br \/>\n   3\u3001poolSize>corePoolSize &amp;&amp; \u4efb\u52a1\u961f\u5217\u6ee1\u4e86\u65f6:<br \/>\n   poolSize&lt;maxPoolSize\u521b\u5efa\u65b0\u7684\u7ebf\u7a0b\u5904\u7406\u65b0\u6765\u7684\u4efb\u52a1\u3002<br \/>\n   poolSize=maxPoolSize\u6267\u884c\u62d2\u7edd\u7b56\u7565\n<\/p><\/blockquote>\n<p>waitForTasksToCompleteOnShutdown\uff1a\u5173\u95ed\u65f6\u662f\u5426\u7b49\u5f85\u672a\u5b8c\u6210\u7684\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\uff0c\u9ed8\u8ba4\u662ffalse\u3002<\/p>\n<p>allowCoreThreadTimeout\uff1a\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6838\u5fc3\u7ebf\u7a0b\u4e0d\u4f1a\u9000\u51fa\uff0c\u53ef\u901a\u8fc7\u5c06\u8be5\u53c2\u6570\u8bbe\u7f6e\u4e3atrue\uff0c\u8ba9\u6838\u5fc3\u7ebf\u7a0b\u4e5f\u9000\u51fa\u3002<\/p>\n<p>keepAliveTime: \u8bbe\u7f6e\u7ebf\u7a0b\u53ef\u4ee5\u4fdd\u6301\u7a7a\u95f2\u7684\u65f6\u95f4\uff0c\u8d85\u8fc7\u5c06\u88ab\u7ec8\u6b62\uff0c\u9ed8\u8ba4\u4e3a60s\u3002<\/p>\n<p>threadNamePrefix\uff1a\u8bbe\u7f6e\u7ebf\u7a0b\u524d\u7f00\uff0c\u4fbf\u4e8e\u89c2\u5bdf\u7ebf\u7a0b\u6267\u884c\u60c5\u51b5\u3002<\/p>\n<p>rejectedExecutionHandler\uff1a\u62d2\u7edd\u7b56\u7565\uff0c\u4f7f\u7528\u754c\u961f\u5217\u7684\u65f6\u5019\uff0c\u5982\u679c\u961f\u5217\u6ee1\u4e86\uff0c\u4efb\u52a1\u6dfb\u52a0\u5230\u7ebf\u7a0b\u6c60\u7684\u65f6\u5019\u5c31\u4f1a\u6709\u95ee\u9898\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u62d2\u7edd\u7b56\u7565\uff1a<\/p>\n<blockquote><p>\n  1\u3001AbortPolicy\uff1a\u8be5\u7b56\u7565\u662f\u7ebf\u7a0b\u6c60\u7684\u9ed8\u8ba4\u7b56\u7565\u3002\u4e22\u6389\u65b0\u6765\u7684\u4efb\u52a1\u5e76\u4e14\u629b\u51faRejectedExecutionException\u5f02\u5e38\u3002<br \/>\n   2\u3001DiscardPolicy\uff1a\u5982\u679c\u7ebf\u7a0b\u6c60\u961f\u5217\u6ee1\u4e86\uff0c\u76f4\u63a5\u4e22\u5f03\u4efb\u52a1\u4e0d\u4f1a\u62a5\u9519\u3002<br \/>\n   3\u3001DiscardOldestPolicy\uff1a\u4e22\u5f03\u961f\u5217\u5934\u90e8\u4efb\u52a1\uff0c\u4e5f\u5c31\u662f\u4e22\u5f03\u6700\u65e9\u7684\u8fdb\u5165\u961f\u5217\u7684\u4efb\u52a1\uff0c\u7136\u540e\u5c1d\u8bd5\u52a0\u5165\u65b0\u7684\u4efb\u52a1\u8fdb\u5165\u961f\u5217\u3002<br \/>\n   4\u3001CallerRunsPolicy\uff1a\u7531\u8c03\u7528\u7ebf\u7a0b\uff08\u63d0\u4ea4\u4efb\u52a1\u7684\u7ebf\u7a0b\uff09\u5904\u7406\u8be5\u4efb\u52a1\u3002<br \/>\n   5\u3001\u81ea\u5b9a\u4e49\n<\/p><\/blockquote>\n<h4>\u6b65\u9aa4\u4e09\uff1a \u521b\u5efa\u5f02\u6b65\u65b9\u6cd5\u5e76\u6dfb\u52a0@Async\u6ce8\u89e3<\/h4>\n<pre><code class=\"language-java \">@Service\n@Slf4j\npublic class AsyncService {\n\n\/\/    @Async\n\/\/    public void sendSMS(){\n\/\/        log.info(\"sendSMS...start\");\n\/\/        String result = \"\u77ed\u4fe1\u53d1\u9001\u6210\u529f....\";\n\/\/        System.out.println(result);\n\/\/        log.info(\"sendSMS...end\");\n\/\/    }\n\n    @Async\n    public Future&lt;Object&gt; sendSMS(){\n        log.info(\"sendSMS...start\");\n        String result = \"\u77ed\u4fe1\u53d1\u9001\u6210\u529f....\";\n        System.out.println(result);\n        log.info(\"sendSMS...end\");\n        return new AsyncResult&lt;&gt;(result);\n    }\n}\n<\/code><\/pre>\n<blockquote><p>\n  \u6ce8\u610f\u4e8b\u9879\uff1a\u4ee5\u4e0b\u5185\u5bb9\u9700\u8981\u4e25\u683c\u9075\u5b88<\/p>\n<ul>\n<li>\u5f02\u6b65\u4e0d\u80fd\u65b9\u6cd5\u4f7f\u7528static\u4fee\u9970<\/li>\n<li>\u5f02\u6b65\u7c7b\u9700\u8981\u4f7f\u7528@Component\u6ce8\u89e3\uff08\u6216\u5176\u4ed6\u6ce8\u89e3\uff09<\/li>\n<li>\u5f02\u6b65\u65b9\u6cd5\u4e0d\u80fd\u4e0e\u5b83\u7684\u8c03\u7528\u65b9\u6cd5\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d<\/li>\n<li>\u5728Async\u65b9\u6cd5\u4e0a\u6807\u6ce8@Transactional\u662f\u6ca1\u7528\u7684\u3002 \u5728Async \u65b9\u6cd5\u8c03\u7528\u7684\u65b9\u6cd5\u4e0a\u6807\u6ce8@Transactional \u6709\u6548\u3002<\/li>\n<li>\u5f02\u6b65\u65b9\u6cd5\u8fd4\u56de\u503c\u5fc5\u987b\u662fvoid\u6216Future\u3002<\/li>\n<\/ul>\n<\/blockquote>\n<h4>\u6b65\u9aa4\u56db\uff1a \u521b\u5efa\u8c03\u7528\u8005\u65b9\u6cd5<\/h4>\n<ul>\n<li>\u5982\u679cAsync\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u662fvoid\uff0c\u5219\u8c03\u7528\u8005\u65b9\u6cd5\u4e0e\u666e\u901a\u65b9\u6cd5\u65e0\u5f02\u3002<\/li>\n<li>\u5982\u679cAsync\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u662fFuture\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u5bf9\u7ed3\u679c\u7684\u6536\u96c6\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java \">@RestController\n@Slf4j\npublic class AsyncController {\n\n\n    @Autowired\n    private AsyncService asyncService;\n\n    @GetMapping(\"\/async\")\n    public String async() throws ExecutionException, InterruptedException {\n        log.info(\"start submit\");\n        Future&lt;Object&gt; future = asyncService.sendSMS();\n        log.info(\"end submit\");\n        return \"ok\" + future.get();\n    }\n\n}\n<\/code><\/pre>\n<p><!-- \u6ce8:\u7ed3\u679c\u5f52\u96c6\u8981\u7b49\u5230\u6240\u6709\u5f02\u6b65\u65b9\u6cd5\u90fd\u8fd4\u56de\u540e\u624d\u80fd\u5f80\u4e0b\u5904\u7406\u3002 --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SpringBoot\u5f02\u6b65\u65b9\u6cd5&#8211;Async 1\u3001\u6211\u4eec\u4e3a\u4ec0\u4e48\u8981\u4f7f\u7528\u5f02\u6b65\u65b9\u6cd5 \u5f02\u6b65\u8c03\u7528\u901a\u5e38\u7528\u5728\u53d1\u77ed\u4fe1\u3001\u53d1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[292],"tags":[377,370],"class_list":["post-2191","post","type-post","status-publish","format-standard","hentry","category-java","tag-async","tag-springboot"],"_links":{"self":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/2191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/comments?post=2191"}],"version-history":[{"count":1,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/2191\/revisions"}],"predecessor-version":[{"id":2192,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/2191\/revisions\/2192"}],"wp:attachment":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/media?parent=2191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/categories?post=2191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/tags?post=2191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}