{"id":8026,"date":"2023-11-28T20:10:36","date_gmt":"2023-11-28T12:10:36","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8026"},"modified":"2023-12-05T16:05:10","modified_gmt":"2023-12-05T08:05:10","slug":"springboot-zhong-shen-me-shi-spring-batch","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhong-shen-me-shi-spring-batch\/","title":{"rendered":"SpringBoot \u4e2d\u4ec0\u4e48\u662f Spring Batch?"},"content":{"rendered":"<p>Spring Batch \u662f\u4e00\u4e2a\u7528\u4e8e\u6279\u5904\u7406\u5904\u7406\u7684\u8f7b\u91cf\u7ea7\u6846\u67b6\uff0c\u57fa\u4e8e Spring \u6846\u67b6\u3002\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u7684\u6279\u91cf\u4f5c\u4e1a\u3002Spring Batch \u4e3b\u8981\u7528\u4e8e\u6267\u884c\u5b9a\u671f\u3001\u5927\u89c4\u6a21\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\uff0c\u4f8b\u5982\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e\u3001\u8fdb\u884c\u8f6c\u6362\u3001\u5904\u7406\u3001\u6700\u540e\u5199\u56de\u6570\u636e\u5e93\u3002<\/p>\n<p>Spring Batch \u7684\u6838\u5fc3\u6982\u5ff5\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\n<strong>Job\uff08\u4f5c\u4e1a\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>Job \u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u6279\u5904\u7406\u4efb\u52a1\uff0c\u7531\u4e00\u7cfb\u5217\u6b65\u9aa4\uff08Step\uff09\u7ec4\u6210\u3002\u6bcf\u4e2a Job \u6709\u4e00\u4e2a\u552f\u4e00\u7684\u6807\u8bc6\u7b26\uff0c\u5b83\u5b9a\u4e49\u4e86\u6574\u4e2a\u6279\u5904\u7406\u7684\u6267\u884c\u6d41\u7a0b\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>Step\uff08\u6b65\u9aa4\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>Step \u662f Job \u4e2d\u7684\u4e00\u4e2a\u72ec\u7acb\u5904\u7406\u6b65\u9aa4\uff0c\u4ee3\u8868\u4e86\u6279\u5904\u7406\u7684\u4e00\u4e2a\u9636\u6bb5\u3002\u4e00\u4e2a Job \u901a\u5e38\u7531\u4e00\u4e2a\u6216\u591a\u4e2a Step \u7ec4\u6210\uff0c\u6bcf\u4e2a Step \u5305\u542b\u4e86\u4e00\u4e2a ItemReader\uff08\u8bfb\u53d6\u6570\u636e\uff09\u3001\u4e00\u4e2a ItemProcessor\uff08\u5904\u7406\u6570\u636e\uff09\u3001\u548c\u4e00\u4e2a ItemWriter\uff08\u5199\u5165\u6570\u636e\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>ItemReader\u3001ItemProcessor \u548c ItemWriter\uff1a<\/strong><\/p>\n<ul>\n<li>\u8fd9\u4e09\u4e2a\u63a5\u53e3\u662f Spring Batch \u4e2d\u5904\u7406\u6570\u636e\u7684\u6838\u5fc3\u7ec4\u4ef6\u3002\n<ul>\n<li><strong>ItemReader\uff1a<\/strong> \u7528\u4e8e\u8bfb\u53d6\u6570\u636e\uff0c\u4f8b\u5982\u4ece\u6570\u636e\u5e93\u3001\u6587\u4ef6\u3001\u6d88\u606f\u961f\u5217\u7b49\u3002<\/li>\n<li><strong>ItemProcessor\uff1a<\/strong> \u5bf9\u8bfb\u53d6\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u8f6c\u6362\u3002<\/li>\n<li><strong>ItemWriter\uff1a<\/strong> \u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u5199\u56de\u5230\u76ee\u6807\uff0c\u4f8b\u5982\u5199\u5165\u6570\u636e\u5e93\u3001\u6587\u4ef6\u3001\u53d1\u9001\u5230\u6d88\u606f\u961f\u5217\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>JobRepository\uff1a<\/strong><\/p>\n<ul>\n<li>\u7528\u4e8e\u5b58\u50a8\u4f5c\u4e1a\u7684\u5143\u6570\u636e\uff0c\u4f8b\u5982\u4f5c\u4e1a\u7684\u72b6\u6001\u3001\u53c2\u6570\u7b49\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSpring Batch \u4f7f\u7528\u5173\u7cfb\u6570\u636e\u5e93\u6765\u5b58\u50a8\u8fd9\u4e9b\u5143\u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>JobLauncher\uff1a<\/strong><\/p>\n<ul>\n<li>\u7528\u4e8e\u542f\u52a8\u548c\u6267\u884c Job\u3002JobLauncher \u662f\u4e00\u4e2a\u63a5\u53e3\uff0cSpring Batch \u63d0\u4f9b\u4e86\u4e0d\u540c\u7684\u5b9e\u73b0\uff0c\u4f8b\u5982 SimpleJobLauncher \u548c AsyncJobLauncher\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>Spring Batch \u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u914d\u7f6e\u9009\u9879\u548c\u53ef\u6269\u5c55\u6027\uff0c\u53ef\u4ee5\u9002\u5e94\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002\u4f7f\u7528 Spring Batch \u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u3001\u8c03\u5ea6\u548c\u7ba1\u7406\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u4efb\u52a1\uff0c\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u548c\u5904\u7406\u7684\u53ef\u9760\u6027\u3002<\/p>\n<p>\u793a\u4f8b\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-java\">@Configuration\n@EnableBatchProcessing\npublic class BatchConfiguration {\n\n    @Autowired\n    private JobBuilderFactory jobBuilderFactory;\n\n    @Autowired\n    private StepBuilderFactory stepBuilderFactory;\n\n    @Bean\n    public ItemReader&lt;MyEntity&gt; itemReader() {\n        \/\/ Configure and return an ItemReader implementation\n    }\n\n    @Bean\n    public ItemProcessor&lt;MyEntity, MyEntity&gt; itemProcessor() {\n        \/\/ Configure and return an ItemProcessor implementation\n    }\n\n    @Bean\n    public ItemWriter&lt;MyEntity&gt; itemWriter() {\n        \/\/ Configure and return an ItemWriter implementation\n    }\n\n    @Bean\n    public Step myStep() {\n        return stepBuilderFactory.get(&quot;myStep&quot;)\n                .&lt;MyEntity, MyEntity&gt;chunk(10)\n                .reader(itemReader())\n                .processor(itemProcessor())\n                .writer(itemWriter())\n                .build();\n    }\n\n    @Bean\n    public Job myJob() {\n        return jobBuilderFactory.get(&quot;myJob&quot;)\n                .start(myStep())\n                .build();\n    }\n}\n<\/code><\/pre>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Spring Batch \u914d\u7f6e\u793a\u4f8b\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u8bfb\u53d6\u3001\u5904\u7406\u548c\u5199\u5165\u6b65\u9aa4\u7684\u4f5c\u4e1a\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u4e1a\u52a1\u903b\u8f91\u548c\u9700\u6c42\u914d\u7f6e ItemReader\u3001ItemProcessor\u3001\u548c ItemWriter \u7684\u5b9e\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Batch \u662f\u4e00\u4e2a\u7528\u4e8e\u6279\u5904\u7406\u5904\u7406\u7684\u8f7b\u91cf\u7ea7\u6846\u67b6\uff0c\u57fa\u4e8e Spring \u6846\u67b6\u3002\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5de5\u5177 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-8026","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=8026"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8026\/revisions"}],"predecessor-version":[{"id":16562,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8026\/revisions\/16562"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8026"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}