{"id":4160,"date":"2021-12-18T09:25:05","date_gmt":"2021-12-18T00:25:05","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4160"},"modified":"2022-04-06T16:21:35","modified_gmt":"2022-04-06T07:21:35","slug":"springboot-%eb%b9%84%eb%8f%99%ea%b8%b0-%ec%84%9c%eb%b9%84%ec%8a%a4-%eb%a7%8c%eb%93%a4%ea%b8%b0async","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4160","title":{"rendered":"Spring Boot \ube44\ub3d9\uae30 \uc11c\ube44\uc2a4 \ub9cc\ub4e4\uae30(@Async)"},"content":{"rendered":"<h1>Spring Boot \ube44\ub3d9\uae30 \uc11c\ube44\uc2a4 \ub9cc\ub4e4\uae30(@Async)<\/h1>\n<p><a href=\"https:\/\/www.hanumoka.net\/2020\/07\/02\/springBoot-20200702-sringboot-async-service\/\">\ucc38\uc870<\/a><\/p>\n<p>\ub85c\uadf8 \uc800\uc7a5 \ub85c\uc9c1\uc744 \ube44\ub3d9\uae30\ub85c \ud574 \ub193\uc73c\uba74, \uc11c\ube44\uc2a4 \uc18d\ub3c4 \uc800\ud558\uac00 \uc5c6\uc5b4,<br \/>\n\uc11c\ube44\uc2a4 \ud488\uc9c8 \ud5a5\uc0c1\uc5d0 \ub3c4\uc6c0\uc774 \ub41c\ub2e4.<\/p>\n<h2>@Configuration<\/h2>\n<p>CorePoolSize : \uae30\ubcf8 \uc2e4\ud589 \ub300\uae30\ud558\ub294 Thread\uc758 \uc218<br \/>\nMaxPoolSize : \ub3d9\uc2dc \ub3d9\uc791\ud558\ub294 \ucd5c\ub300 Thread\uc758 \uc218<br \/>\nQueueCapacity : MaxPoolSize \ucd08\uacfc \uc694\uccad\uc2dc \ucd5c\ub300 \uc218\uc6a9 \uac00\ub2a5\ud55c Queue\uc758 \uc218<\/p>\n<pre><code class=\"language-java\">@Configuration\n@EnableAsync\npublic class AsyncConfig extends AsyncConfigurerSupport {\n\n    public Executor getAsyncExecutor() {\n        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();\n        executor.setCorePoolSize(2);\n        executor.setMaxPoolSize(10);\n        executor.setQueueCapacity(500);\n        executor.setThreadNamePrefix(&quot;mg-async-&quot;);\n        executor.initialize();\n\n        return executor;\n    }\n}<\/code><\/pre>\n<h2>@Async<\/h2>\n<p><code>@Async<\/code> \uc560\ub108\ud14c\uc774\uc158 \ucd94\uac00\ub9cc\uc73c\ub85c \ube44\ub3d9\uae30 \uc11c\ube44\uc2a4\uac00 \ub41c\ub2e4.<\/p>\n<pre><code class=\"language-java\">@RequiredArgsConstructor\n@Service\npublic class LoggerService {\n\n    private final LogKakaoRestaurantInfoRepository repository;\n    private final LogKakaoRestaurantInfoMapper mapper = Mappers.getMapper(LogKakaoRestaurantInfoMapper.class);\n\n    @Async\n    @Transactional\n    public void saveKakaoRestaurantInfo(List&lt;Document&gt; documents) {\n        for (Document document : documents) {\n            try {\n                LogKakaoRestaurantInfo info = repository.getById(document.getId());\n                mapper.updateFromDto(document, info);\n            } catch (EntityNotFoundException ex) {\n                repository.save(mapper.toEntity(document));\n            }\n        }\n    }\n}<\/code><\/pre>\n<h2>\uc8fc\uc758\uc0ac\ud56d<\/h2>\n<ol>\n<li>\n<p>private method \uc0ac\uc6a9 \ubd88\uac00<\/p>\n<\/li>\n<li>\n<p>self-invocation(\uc790\uac00 \ud638\ucd9c) \ubd88\uac00<\/p>\n<p>\uac19\uc740 Class \uc5d0 \uc874\uc7ac\ud558\ub294 method \uc5d0 @Async Annotation\uc744 \ubd99\uc5ec\ub3c4 \ub3d9\uae30\ubc29\uc2dd\uc73c\ub85c \uc791\ub3d9\ud55c\ub2e4.<\/p>\n<\/li>\n<li>\n<p>QueueCapacity \ucd08\uacfc \uc694\uccad\uc5d0 \ub300\ud55c \ube44\ub3d9\uae30 method \ud638\ucd9c\uc2dc \ubc29\uc5b4 \ucf54\ub4dc \uc791\uc131 \ud544\uc694<\/p>\n<\/li>\n<\/ol>\n<p><code>QueueCapacity<\/code> \ub97c \ubaa8\ub450 \uc18c\uc9c4\ud55c \uacbd\uc6b0, <code>TaskRejectedException<\/code> \uc774 \ubc1c\uc0dd\ud55c\ub2e4.<\/p>\n<pre><code class=\"language-java\">@AllArgsConstructor\n@Controller\npublic Class TestController {\n\n    private TestService testService;\n\n    @GetMapping(&quot;async&quot;)\n    public String testAsync() {\n        log.info(&quot;TEST ASYNC&quot;);\n        try {\n            for(int i=0; i&lt;50; i++) {\n                testService.asyncMethod(i);\n        } catch (TaskRejectedException e) {\n            \/\/ \ubc29\uc5b4\ucf54\ub4dc\n        }\n        return &quot;&quot;;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \ube44\ub3d9\uae30 \uc11c\ube44\uc2a4 \ub9cc\ub4e4\uae30(@Async) \ucc38\uc870 \ub85c\uadf8 \uc800\uc7a5 \ub85c\uc9c1\uc744 \ube44\ub3d9\uae30\ub85c \ud574 \ub193\uc73c\uba74, \uc11c\ube44\uc2a4 \uc18d\ub3c4 \uc800\ud558\uac00 \uc5c6\uc5b4, \uc11c\ube44\uc2a4 \ud488\uc9c8 \ud5a5\uc0c1\uc5d0 \ub3c4\uc6c0\uc774 \ub41c\ub2e4. @Configuration CorePoolSize : \uae30\ubcf8 \uc2e4\ud589 \ub300\uae30\ud558\ub294 Thread\uc758 \uc218 MaxPoolSize : \ub3d9\uc2dc \ub3d9\uc791\ud558\ub294 \ucd5c\ub300 Thread\uc758 \uc218 QueueCapacity : MaxPoolSize \ucd08\uacfc \uc694\uccad\uc2dc \ucd5c\ub300 \uc218\uc6a9 \uac00\ub2a5\ud55c Queue\uc758 \uc218 @Configuration @EnableAsync public class AsyncConfig extends AsyncConfigurerSupport { public\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=4160\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-4160","post","type-post","status-publish","format-standard","hentry","category-spring-boot-2-5"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4160"}],"version-history":[{"count":4,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4160\/revisions"}],"predecessor-version":[{"id":5080,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4160\/revisions\/5080"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}