{"id":233351,"date":"2022-01-09T10:09:52","date_gmt":"2022-01-09T02:09:52","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=233351"},"modified":"2021-12-30T13:10:34","modified_gmt":"2021-12-30T05:10:34","slug":"springboot-scheduled-public","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/springboot-scheduled-public.html","title":{"rendered":"springBoot @Scheduled\u591a\u4efb\u52a1\u540c\u65f6\u5f00\u59cb\u6267\u884c"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86springBoot @Scheduled\u5b9e\u73b0\u591a\u4e2a\u4efb\u52a1\u540c\u65f6\u5f00\u59cb\u6267\u884c\uff0c\u5177\u6709\u5f88\u597d\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002\u5982\u6709\u9519\u8bef\u6216\u672a\u8003\u8651\u5b8c\u5168\u7684\u5730\u65b9\uff0c\u671b\u4e0d\u541d\u8d50\u6559<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
@Scheduled\u591a\u4e2a\u4efb\u52a1\u540c\u65f6\u5f00\u59cb\u6267\u884c<\/strong><\/div>\n

\u53ea\u9700\u5728springBoot\u542f\u52a8\u7c7b\u4e0a\u6dfb\u52a0<\/p>\n

\u5982\u4e0b\u4ee3\u7801\u5373\u53ef\uff1a<\/p>\n

\r\n@Bean\r\npublic TaskScheduler taskScheduler() {\r\n    ThreadPoolTaskScheduler taskExecutor = new ThreadPoolTaskScheduler();\r\n    taskExecutor.setPoolSize(50);\r\n    return taskExecutor;\r\n}\r\n<\/pre>\n

\"\"<\/p>\n

@Scheduled\u591a\u5b9a\u65f6\u4efb\u52a1\uff0c\u91cd\u53e0\u6267\u884c<\/strong><\/div>\n

@Scheduled\u5982\u679c\u6709\u4e24\u4e2a\u5b9a\u65f6\u4efb\u52a1<\/p>\n

\u5b9a\u65f6\u4efb\u52a1\u91cd\u590d\u65f6\uff0c\u53ea\u6709\u4e00\u4e2a\u53ef\u4ee5\u6267\u884c\u3002<\/p>\n

\u5982\u4e0b<\/p>\n

\r\nimport org.springframework.scheduling.annotation.Scheduled;\r\nimport org.springframework.stereotype.Component; \r\nimport java.time.LocalDateTime;\r\n  \r\n@Component\r\npublic class MyScheduled { \r\n    @Scheduled(cron = \"0\/5 * * * * ?\")\r\n    public void execute1(){\r\n        String curName = Thread.currentThread().getName() ;\r\n        System.out.println(\"\u5f53\u524d\u65f6\u95f4:\"+LocalDateTime.now()+\"  \u4efb\u52a1execute1\u5bf9\u5e94\u7684\u7ebf\u7a0b\u540d: \"+curName);\r\n        try {\r\n            Thread.sleep(1000);\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        } \r\n    }\r\n  \r\n    @Scheduled(cron = \"0\/5 * * * * ?\")\r\n    public void execute2(){ \r\n        String curName = Thread.currentThread().getName() ;\r\n        System.out.println(\"\u5f53\u524d\u65f6\u95f4:\"+LocalDateTime.now()+\"  \u4efb\u52a1execute2\u5bf9\u5e94\u7684\u7ebf\u7a0b\u540d: \"+curName);\r\n        try {\r\n            Thread.sleep(1000);\r\n        } catch (InterruptedException e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n

\u901a\u8fc7\u6267\u884c\u53ef\u4ee5\u770b\u5230\uff0c\u6253\u5370\u7ebf\u7a0b\u540d\u79f0\u4e3a\u540c\u4e00\u4e2a\u3002\u5373\u5982\u679c\u4e0d\u624b\u52a8\u6307\u5b9a\u7ebf\u7a0b\u6c60\uff0c\u5219\u9ed8\u8ba4\u542f\u52a8\u5355\u7ebf\u7a0b\uff0c\u8fdb\u884c\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n

\u5982\u679c\u60f3\u8981\u591a\u4e2a\u5b9a\u65f6\u4efb\u52a1\u91cd\u53e0\u6267\u884c<\/strong><\/div>\n

\u9700\u8981\u624b\u52a8\u6307\u5b9a\u7ebf\u7a0b\u6c60\uff0c\u5982\u4e0b<\/p>\n

\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.scheduling.TaskScheduler;\r\nimport org.springframework.scheduling.annotation.EnableScheduling;\r\nimport org.springframework.scheduling.annotation.Scheduled;\r\nimport org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;\r\nimport org.springframework.stereotype.Component; \r\nimport java.time.LocalDateTime;\r\n  \r\n@Component\r\n@EnableScheduling\r\npublic class MyScheduled {\r\n  \r\n    @Bean\r\n    public TaskScheduler taskScheduler() {\r\n        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();\r\n        taskScheduler.setPoolSize(50);\r\n        return taskScheduler;\r\n    }\r\n  \r\n    @Scheduled(cron = \"0\/5 * * * * ?\")\r\n    public void execute1(){\r\n        String curName = Thread.currentThread().getName() ;\r\n        System.out.println(\"\u5f53\u524d\u65f6\u95f4:\"+LocalDateTime.now()+\"  \u4efb\u52a1execute1\u5bf9\u5e94\u7684\u7ebf\u7a0b\u540d: \"+curName);\r\n        try {\r\n            Thread.sleep(1000);\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        } \r\n    }\r\n  \r\n    @Scheduled(cron = \"0\/5 * * * * ?\")\r\n    public void execute2(){ \r\n        String curName = Thread.currentThread().getName() ;\r\n        System.out.println(\"\u5f53\u524d\u65f6\u95f4:\"+LocalDateTime.now()+\"  \u4efb\u52a1execute2\u5bf9\u5e94\u7684\u7ebf\u7a0b\u540d: \"+curName);\r\n        try {\r\n            Thread.sleep(1000);\r\n        } catch (InterruptedException e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n

\u6b64\u65f6\uff0c\u591a\u4e2a\u5b9a\u65f6\u4efb\u52a1\uff0c\u662f\u4e0d\u901a\u7684\u7ebf\u7a0b\u6267\u884c\uff0c\u540c\u65f6\uff0c\u5b9a\u65f6\u4efb\u52a1\u53ef\u4ee5\u91cd\u53e0\u6267\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u53ea\u9700\u5728springBoot\u542f\u52a8\u7c7b\u4e0a\u6dfb\u52a0 \u5982\u4e0b\u4ee3\u7801\u5373\u53ef\uff1a @Bean public TaskScheduler […]<\/p>\n","protected":false},"author":362,"featured_media":233353,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-233351","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=233351"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233351\/revisions"}],"predecessor-version":[{"id":233355,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/233351\/revisions\/233355"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/233353"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=233351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=233351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=233351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}