{"id":8350,"date":"2022-11-09T10:40:40","date_gmt":"2022-11-09T02:40:40","guid":{"rendered":"http:\/\/123.57.164.21\/?p=8350"},"modified":"2022-11-09T10:50:55","modified_gmt":"2022-11-09T02:50:55","slug":"springbatch%e6%a6%82%e8%bf%b0","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=8350","title":{"rendered":"SpringBatch\u6982\u8ff0"},"content":{"rendered":"\n<p>\u200b Spring Batch\u662f\u4e2a\u8f7b\u91cf\u7ea7\u7684\u3001 \u5b8c\u5584\u7684\u6279\u5904\u7406\u6846\u67b6,\u65e8\u5728\u5e2e\u52a9\u4f01\u4e1a\u5efa\u7acb\u5065\u58ee\u3001\u9ad8\u6548\u7684\u6279\u5904\u7406\u5e94\u7528\u3002Spring Batch\u662fSpring\u7684\u4e00\u4e2a\u5b50\u9879\u76ee,\u4f7f\u7528Java\u8bed\u8a00\u5e76\u57fa\u4e8eSpring\u6846\u67b6\u4e3a\u57fa\u7840\u5f00\u53d1,\u4f7f\u5f97\u5df2\u7ecf\u4f7f\u7528Spring\u6846\u67b6\u7684\u5f00\u53d1\u8005\u6216\u8005\u4f01\u4e1a\u66f4\u5bb9\u6613\u8bbf\u95ee\u548c\u5229\u7528\u4f01\u4e1a\u670d\u52a1\u3002<\/p>\n\n\n\n<p>Spring Batch\u63d0\u4f9b\u4e86\u5927\u91cf\u53ef\u91cd\u7528\u7684\u7ec4\u4ef6,\u5305\u62ec\u4e86\u65e5\u5fd7\u8ffd\u8e2a\u3001\u4e8b\u52a1\u3001\u4efb\u52a1\u4f5c\u4e1a\u7edf\u8ba1\u3001\u4efb\u52a1\u91cd\u542f\u3001\u8df3\u8fc7\u3001\u91cd\u590d\u8d44\u6e90\u7ba1\u7406\u3002\u5bf9\u4e8e\u5927\u6570\u636e\u91cf\u548c\u9ad8\u6027\u80fd\u7684\u6279\u5904\u7406\u4efb \u52a1,Spring Batch\u540c\u6837\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u529f\u80fd\u548c\u7279\u6027\u6765\u652f\u6301\u6bd4\u5982\u5206\u533a\u529f\u80fd\u3001\u8fdc\u7a0b\u529f\u80fd\u3002\u603b\u4e4b,\u901a\u8fc7Spring Batch\u80fd\u591f\u652f\u6301\u7b80\u5355\u7684\u3001\u590d\u6742\u7684\u548c\u5927\u6570\u636e\u91cf\u7684\u6279\u5904\u7406\u4f5c\u4e1a\u3002<\/p>\n\n\n\n<p>Spring Batch\u662f\u4e00\u4e2a\u6279\u5904\u7406\u5e94\u7528\u6846\u67b6,\u4e0d\u662f\u8c03\u5ea6\u6846\u67b6\uff0c\u4f46\u9700\u8981\u548c\u8c03\u5ea6\u6846\u67b6\u5408\u4f5c\u6765\u6784\u5efa\u5b8c\u6210\u7684\u6279\u5904\u7406\u4efb\u52a1\u3002\u5b83\u53ea\u5173\u6ce8\u6279\u5904\u7406\u4efb\u52a1\u76f8\u5173\u7684\u95ee\u9898,\u5982\u4e8b\u52a1\u3001\u5e76\u53d1\u3001\u76d1\u63a7\u3001\u6267\u884c\u7b49,\u5e76\u4e0d\u63d0\u4f9b\u76f8\u5e94\u7684\u8c03\u5ea6\u529f\u80fd\u3002\u5982\u679c\u9700\u8981\u4f7f\u7528\u8c03\u7528\u6846\u67b6\uff0c\u5728\u5546\u4e1a\u8f6f\u4ef6\u548c\u5f00\u6e90\u8f6f\u4ef6\u4e2d\u5df2\u7ecf\u6709\u5f88\u591a\u4f18\u79c0\u7684\u4f01\u4e1a\u7ea7\u8c03\u5ea6\u6846\u67b6(\u5982Quartz. Tivoli\u3001 Control-M\u3001 Cron\u7b49)\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u6846\u67b6\u4e3b\u8981\u6709\u4ee5\u4e0b\u529f\u80fd: Transaction management (\u4e8b\u52a1\u7ba1\u7406) Chunk based processing (\u57fa\u4e8e\u5757\u7684\u5904\u7406) Declarative 1\/0 (\u58f0\u660e\u5f0f\u7684\u8f93\u5165\u8f93\u51fa) Start\/Stop\/Restart (\u542f\u52a8\/\u505c\u6b62\/\u518d\u542f\u52a8) Retry\/Skip (\u91cd\u8bd5\/\u8df3\u8fc7)<\/p>\n\n\n\n<p>Spring Batch\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6279\u91cf\u5904\u7406\u6846\u67b6\uff0cSpring Batch\u63d0\u4f9b\u4e86\u7c7b\u548cAPI\u6765\u8bfb\u5199\u8d44\u6e90\uff0c\u7ba1\u7406\u4e8b\u52a1\uff0c\u4f5c\u4e1a\u5904\u7406\u7edf\u8ba1\u3001\u91cd\u542f\u3001\u4ee5\u53ca\u5206\u533a\u6280\u672f\u5904\u7406\u5927\u91cf\u6570\u636e\u3002\u5728Spring Batch\u4e2d\uff0c\u4e00\u4e2a\u4f5c\u4e1a\u4efb\u52a1\u53ef\u4ee5\u7531\u591a\u4e2astep\u7ec4\u6210\uff0c\u6bcf\u4e2a\u4efb\u52a1\u53c8\u90fd\u53ef\u4ee5\u5206\u4e3aRead-Process-Write\u6216\u8005\u662ftasklet<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-110-1024x391.png\" alt=\"\" class=\"wp-image-8359\" width=\"609\" height=\"232\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-110-1024x391.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-110-300x115.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-110-768x293.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-110-1536x586.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-110-830x317.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-110-230x88.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-110-350x134.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-110-480x183.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-110.png 1954w\" sizes=\"(max-width: 609px) 100vw, 609px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-111-1024x697.png\" alt=\"\" class=\"wp-image-8361\" width=\"609\" height=\"413\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-111-300x204.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-111-1536x1045.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-111-230x156.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-111-350x238.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-111-480x327.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-111.png 1946w\" sizes=\"(max-width: 609px) 100vw, 609px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-112-911x1024.png\" alt=\"\" class=\"wp-image-8363\" width=\"573\" height=\"644\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-112-911x1024.png 911w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-112-267x300.png 267w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-112-768x863.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-112-830x933.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-112-230x258.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-112-350x393.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-112-480x539.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-112.png 1164w\" sizes=\"(max-width: 573px) 100vw, 573px\" \/><\/figure><\/div>\n\n\n\n<p>\u6846\u67b6\u4e00\u5171\u67094\u4e2a\u4e3b\u8981\u89d2\u8272:<\/p>\n\n\n\n<ul><li>JobLauncher\u662f\u4efb\u52a1\u542f\u52a8\u5668\uff0c\u901a\u8fc7\u5b83\u6765\u542f\u52a8\u4efb\u52a1\uff0c\u53ef\u4ee5\u770b\u505a\u662f\u7a0b\u5e8f\u7684\u5165\u53e3\u3002<\/li><li>Job\u4ee3\u8868\u7740\u4e00\u4e2a\u5177\u4f53\u7684\u4efb\u52a1\u3002<\/li><li>Step\u4ee3\u8868\u7740\u4e00\u4e2a\u5177\u4f53\u7684\u6b65\u9aa4\uff0c\u4e00\u4e2aJob\u53ef\u4ee5\u5305\u542b\u591a\u4e2aStep (\u60f3\u8c61\u628a\u5927\u8c61\u653e\u8fdb\u51b0\u7bb1\u8fd9\u4e2a\u4efb\u52a1\u9700\u8981\u591a\u5c11\u4e2a\u6b65\u9aa4\u4f60\u5c31\u660e\u767d\u4e86) .<\/li><li>JobRepository\u662f\u5b58\u50a8\u6570\u636e\u7684\u5730\u65b9\uff0c\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2a\u6570\u636e\u5e93\u7684\u63a5\u53e3\uff0c\u5728\u4efb\u52a1\u6267\u884c\u7684\u65f6\u5019\u9700\u8981\u901a\u8fc7\u5b83\u6765\u8bb0\u5f55\u4efb\u52a1\u72b6\u6001\u7b49\u7b49\u4fe1\u606f\u3002<\/li><\/ul>\n\n\n\n<ul><li>\u5bf9\u4e8e\u201cRead-Process-Write\u201d\u8fc7\u7a0b\uff0c\u5b83\u662f\u6307\u4ece\u8d44\u6e90\uff08csv\u3001xml\u6216\u6570\u636e\u5e93\uff09\u4e2d\u201c\u8bfb\u53d6\u201d\u6570\u636e\uff0c\u201c\u5904\u7406\u201d\u5b83\u5e76\u201c\u5199\u5165\u201d\u5b83\u5230\u5176\u4ed6\u8d44\u6e90\uff08csv\u3001xml\u548c\u6570\u636e\u5e93\uff09\u3002\u4f8b\u5982\uff0c\u6b65\u9aa4\u53ef\u4ee5\u4eceCSV\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u5bf9\u5176\u8fdb\u884c\u5904\u7406\u5e76\u5c06\u5176\u5199\u5165\u6570\u636e\u5e93\u3002Spring Batch\u63d0\u4f9b\u4e86\u8bb8\u591a\u5b9a\u5236\u7c7b\u6765\u8bfb\/\u5199CSV\u3001XML\u548c\u6570\u636e\u5e93\u3002<\/li><li>\u5bf9\u4e8e\u201c\u5355\u4e2a\u201d\u64cd\u4f5c\u4efb\u52a1\uff08tasklet\uff09\uff0c\u5b83\u610f\u5473\u7740\u53ea\u6267\u884c\u5355\u4e2a\u4efb\u52a1\uff0c\u6bd4\u5982\u5728\u6b65\u9aa4\u542f\u52a8\u6216\u5b8c\u6210\u4e4b\u540e\u6216\u4e4b\u524d\u6e05\u7406\u8d44\u6e90\u3002<\/li><li>\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u4ee5\u94fe\u63a5\u5728\u4e00\u8d77\u4f5c\u4e3a\u4f5c\u4e1a\u8fd0\u884c\u3002<\/li><\/ul>\n\n\n\n<p><strong>SpringBatch\u5165\u95e8\u7a0b\u5e8f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1.\u521b\u5efa\u4e00\u4e2aSpringBoot\u9879\u76ee\uff0cSpringBatch\u4f9d\u8d56\uff0c\u5982\u4e0b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n 2 &lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\"\n 3          xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n 4          xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\">\n 5     &lt;modelVersion>4.0.0&lt;\/modelVersion>\n 6 \n 7     &lt;groupId>com.test&lt;\/groupId>\n 8     &lt;artifactId>test-springboot-batch&lt;\/artifactId>\n 9     &lt;version>1.0-SNAPSHOT&lt;\/version>\n10 \n11     &lt;parent>\n12         &lt;groupId>org.springframework.boot&lt;\/groupId>\n13         &lt;artifactId>spring-boot-starter-parent&lt;\/artifactId>\n14         &lt;version>2.2.5.RELEASE&lt;\/version>\n15         &lt;relativePath\/> &lt;!-- lookup parent from repository -->\n16     &lt;\/parent>\n17 \n18     &lt;properties>\n19         &lt;maven.compiler.source>8&lt;\/maven.compiler.source>\n20         &lt;maven.compiler.target>8&lt;\/maven.compiler.target>\n21     &lt;\/properties>\n22 \n23     &lt;dependencies>\n24         &lt;dependency>\n25             &lt;groupId>org.springframework.boot&lt;\/groupId>\n26             &lt;artifactId>spring-boot-starter-web&lt;\/artifactId>\n27         &lt;\/dependency>\n28 \n29         &lt;dependency>\n30             &lt;groupId>org.springframework.boot&lt;\/groupId>\n31             &lt;artifactId>spring-boot-starter-batch&lt;\/artifactId>\n32         &lt;\/dependency>\n33 \n34         &lt;!-- mysql -->\n35         &lt;dependency>\n36             &lt;groupId>mysql&lt;\/groupId>\n37             &lt;artifactId>mysql-connector-java&lt;\/artifactId>\n38             &lt;version>8.0.12&lt;\/version>\n39         &lt;\/dependency>\n40     &lt;\/dependencies>\n41 \n42     &lt;build>\n43         &lt;plugins>\n44             &lt;plugin>\n45                 &lt;groupId>org.springframework.boot&lt;\/groupId>\n46                 &lt;artifactId>spring-boot-maven-plugin&lt;\/artifactId>\n47             &lt;\/plugin>\n48         &lt;\/plugins>\n49     &lt;\/build>\n50 \n51 &lt;\/project><\/pre>\n\n\n\n<p><strong>2.\u7f16\u8f91\u914d\u7f6e\u6587\u4ef6\uff0c\u8fd9\u91cc\u8981\u4f7f\u7528mysql\u6570\u636e\u5e93\uff0c\u4efb\u52a1\u4fe1\u606f\u6301\u4e45\u5316\u5230\u6570\u636e\u5e93\u4e2d<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">spring:\n  datasource:\n    username: root\n    password: 123456\n    url: jdbc:mysql:\/\/127.0.0.1:3306\/test_springbatch?allowPublicKeyRetrieval=true&amp;useSSL=true\n    driver-class-name: com.mysql.cj.jdbc.Driver\n    # \u521d\u59cb\u5316\u6570\u636e\u5e93\uff0c\u6587\u4ef6\u5728\u4f9d\u8d56jar\u5305\u4e2d\n    schema: classpath:org\/springframework\/batch\/core\/schema-mysql.sql\n    initialization-mode: always<\/pre>\n\n\n\n<p><strong>3.\u7f16\u8f91\u4e3b\u542f\u52a8\u7c7b<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootApplication\npublic class Application {\n\n    public static void main(String[] args) {\n\n        SpringApplication.run(Application.class);\n    }\n}<\/pre>\n\n\n\n<p><strong>4.\u914d\u7f6eJob<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package com.test.config;\n\nimport org.springframework.batch.core.Job;\nimport org.springframework.batch.core.Step;\nimport org.springframework.batch.core.StepContribution;\nimport org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;\nimport org.springframework.batch.core.configuration.annotation.JobBuilderFactory;\nimport org.springframework.batch.core.configuration.annotation.StepBuilderFactory;\nimport org.springframework.batch.core.scope.context.ChunkContext;\nimport org.springframework.batch.core.step.tasklet.Tasklet;\nimport org.springframework.batch.repeat.RepeatStatus;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\n\/**\n * @author H__D\n * @description\n * @date 2021\/10\/30\n *\/\n@Configuration\n\/\/ \u542f\u7528\u6279\u5904\u7406\u529f\u80fd\n@EnableBatchProcessing\npublic class JobConfiguration {\n\n    \/\/ \u6ce8\u5165\u521b\u5efa\u4efb\u52a1\u7684\u5bf9\u8c61\n    @Autowired\n    private JobBuilderFactory jobBuilderFactory;\n\n    \/\/ \u6ce8\u5165\u521b\u5efa\u6b65\u9aa4\u7684\u5bf9\u8c61\n    @Autowired\n    private StepBuilderFactory stepBuilderFactory;\n\n    @Bean\n    public Job helloworldJob() {\n        return jobBuilderFactory.get(\"helloworldJob\")\n                .start(step1())\n                .build();\n    }\n\n    @Bean\n    public Step step1() {\n        return stepBuilderFactory.get(\"step1\")\n                .tasklet(new Tasklet() {\n                    @Override\n                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {\n                        System.out.println(Thread.currentThread().getName() + \"------\" + \"hello world\");\n                        \/\/ \u8fd4\u56de\u6267\u884c\u5b8c\u6210\u72b6\u6001\n                        return RepeatStatus.FINISHED;\n                    }\n                }).build();\n    }\n}<\/pre>\n\n\n\n<p><strong>5.\u8fd0\u884c\u542f\u52a8\u7c7b<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-109-1024x332.png\" alt=\"\" class=\"wp-image-8355\" width=\"563\" height=\"182\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-109-1024x332.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-109-300x97.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-109-768x249.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-109-830x269.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-109-230x75.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-109-350x113.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-109-480x156.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-109.png 1394w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><figcaption>\u67e5\u770b\u7ed3\u679c<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-108-1024x391.png\" alt=\"\" class=\"wp-image-8354\" width=\"559\" height=\"212\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-108-1024x391.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-108-300x115.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-108-830x317.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-108-230x88.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-108-350x134.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/11\/\u56fe\u7247-108-480x183.png 480w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><figcaption>mysql\u6570\u636e\u5e93<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>\u53e6\u4e00\u4e2a\u5c0f\u4f8b\u5b50<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>1.\u9879\u76ee\u4f9d\u8d56<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;dependency>\n            &lt;groupId>org.springframework.boot&lt;\/groupId>\n            &lt;artifactId>spring-boot-starter-batch&lt;\/artifactId>\n        &lt;\/dependency>\n        &lt;dependency>\n            &lt;groupId>org.springframework.boot&lt;\/groupId>\n            &lt;artifactId>spring-boot-starter-web&lt;\/artifactId>\n        &lt;\/dependency>\n\n        &lt;dependency>\n            &lt;groupId>mysql&lt;\/groupId>\n            &lt;artifactId>mysql-connector-java&lt;\/artifactId>\n            &lt;scope>runtime&lt;\/scope>\n        &lt;\/dependency>\n<\/pre>\n\n\n\n<p><strong>2.\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684Tasklet<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class MessageTasklet implements Tasklet {\n\n\n    @Override\n    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {\n        String message = (String) chunkContext.getStepContext().getJobParameters().get(\"message\");\n        ExecutionContext jobContext = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();\n        \n        jobContext.put(\"message\", message);\n        \/\/\u6253\u5370\u4f20\u5165\u7684\u53c2\u6570\n        System.out.println(message);\n\n        return RepeatStatus.FINISHED;\n    }\n}<\/pre>\n\n\n\n<p><strong>3.Job\u914d\u7f6e<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Configuration\npublic class TaskletJobConfiguration {\n\n    @Autowired\n    private JobBuilderFactory jobs;\n\n    @Autowired\n    private StepBuilderFactory steps;\n\n    @Bean\n    public Job taskletJob() {\n        return this.jobs.get(\"taskletJob\")\n                .start(step())\n                .build();\n    }\n\n    @Bean\n    protected Step step() {\n        return steps\n                .get(\"step\")\n                .tasklet(messageTasklet())\n                .build();\n    }\n\n    @Bean\n    public MessageTasklet messageTasklet() {\n        MessageTasklet tasklet = new MessageTasklet();\n        return tasklet;\n    }\n\n}<\/pre>\n\n\n\n<p><strong>4.\u53c2\u6570\u914d\u7f6e<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#\u521d\u59cb\u5316Spring Batch \u6570\u636e\u8868\nspring.batch.initialize-schema=always\n\n#\u5de5\u7a0b\u542f\u52a8\u65f6\u4e0d\u6267\u884c\u4efb\u52a1\nspring.batch.job.enabled=false\n\nspring.datasource.url=jdbc:mysql:\/\/127.0.0.1:3306\/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;useSSL=false&amp;zeroDateTimeBehavior=convertToNull\nspring.datasource.username=username\nspring.datasource.password=password\nspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver\nspring.datasource.type=com.zaxxer.hikari.HikariDataSource\nspring.datasource.hikari.pool-name=HikariPool\n#\u6700\u5927\u8fde\u63a5\u6570\uff0c\u5c0f\u4e8e\u7b49\u4e8e0\u4f1a\u88ab\u91cd\u7f6e\u4e3a\u9ed8\u8ba4\u503c10\uff1b\u5927\u4e8e\u96f6\u5c0f\u4e8e1\u4f1a\u88ab\u91cd\u7f6e\u4e3aminimum-idle\u7684\u503c\nspring.datasource.hikari.maximum-pool-size=10\n#\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4:\u6beb\u79d2\uff0c\u5c0f\u4e8e250\u6beb\u79d2\uff0c\u5426\u5219\u88ab\u91cd\u7f6e\u4e3a\u9ed8\u8ba4\u503c30\u79d2\nspring.datasource.hikari.connection-timeout=60000\n#\u6700\u5c0f\u7a7a\u95f2\u8fde\u63a5\uff0c\u9ed8\u8ba4\u503c10\uff0c\u5c0f\u4e8e0\u6216\u5927\u4e8emaximum-pool-size\uff0c\u90fd\u4f1a\u91cd\u7f6e\u4e3amaximum-pool-size\nspring.datasource.hikari.minimum-idle=10\n#\u7a7a\u95f2\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba4\u503c600000\uff0810\u5206\u949f\uff09\uff0c\u5927\u4e8e\u7b49\u4e8emax-lifetime\u4e14max-lifetime>0\uff0c\u4f1a\u88ab\u91cd\u7f6e\u4e3a0\uff1b\u4e0d\u7b49\u4e8e0\u4e14\u5c0f\u4e8e10\u79d2\uff0c\u4f1a\u88ab\u91cd\u7f6e\u4e3a10\u79d2\u3002\n# \u53ea\u6709\u7a7a\u95f2\u8fde\u63a5\u6570\u5927\u4e8e\u6700\u5927\u8fde\u63a5\u6570\u4e14\u7a7a\u95f2\u65f6\u95f4\u8d85\u8fc7\u8be5\u503c\uff0c\u624d\u4f1a\u88ab\u91ca\u653e\nspring.datasource.hikari.idle-timeout=500000\n#\u8fde\u63a5\u6700\u5927\u5b58\u6d3b\u65f6\u95f4.\u4e0d\u7b49\u4e8e0\u4e14\u5c0f\u4e8e30\u79d2\uff0c\u4f1a\u88ab\u91cd\u7f6e\u4e3a\u9ed8\u8ba4\u503c30\u5206\u949f.\u8bbe\u7f6e\u5e94\u8be5\u6bd4mysql\u8bbe\u7f6e\u7684\u8d85\u65f6\u65f6\u95f4\u77ed\nspring.datasource.hikari.max-lifetime=540000\n#\u8fde\u63a5\u6d4b\u8bd5\u67e5\u8be2\nspring.datasource.hikari.connection-test-query=SELECT 1<\/pre>\n\n\n\n<p><strong>5.\u63a5\u53e3\u6d4b\u8bd5<\/strong>\uff0c\u6700\u540e\u4e0d\u8981\u5fd8\u8bb0\u5728\u542f\u52a8\u7c7b\u4e0a\u52a0\u4e0a\u6ce8\u89e3<code>@EnableBatchProcessing<\/code><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RestController\n@Slf4j\npublic class JobLauncherController {\n\n\n    @Autowired\n    JobLauncher jobLauncher;\n\n    @Autowired\n    Job job;\n\n    @RequestMapping(\"\/launchjob\")\n    public String handle() throws Exception {\n        String parameter = UUID.randomUUID().toString();\n        try {\n            \/\/\u63a5\u53e3\u6bcf\u6b21\u90fd\u91cd\u65b0\u751f\u6210\u4e00\u4e2a\uff35\uff35\uff29\uff24\uff0c\u5982\u679c\u53c2\u6570\u5b8c\u5168\u76f8\u540c\uff0c\u65e5\u5fd7\u4f1a\u63d0\u793a\u4efb\u52a1\u5df2\u7ecf\u6267\u884c\u6210\u529f\uff0c\u4e0d\u80fd\u91cd\u590d\u6267\u884c\n            JobParameters jobParameters = new JobParametersBuilder().addString(\"message\", \"Welcome To Spring Batch World!\" + parameter)\n                    .toJobParameters();\n            jobLauncher.run(job, jobParameters);\n        } catch (Exception e) {\n            log.error(\"\", e);\n        }\n\n        return parameter;\n    }\n}<\/pre>\n\n\n\n<p><strong>6.Job\u62e6\u622a\u5668<\/strong>\uff0c\u5b9e\u73b0<code>JobExecutionListener<\/code>\u63a5\u53e3\u540e\u5728Job\u914d\u7f6e\u7684\u5730\u65b9\u589e\u52a0\u4e00\u4e0blistener\u5373\u53ef\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Component\npublic class InterceptingJobExecution implements JobExecutionListener {\n    @Override\n    public void beforeJob(JobExecution jobExecution) {\n        System.out.println(\"Intercepting Job Execution - Before Job!\");\n    }\n\n    @Override\n    public void afterJob(JobExecution jobExecution) {\n        System.out.println(\"Intercepting Job Execution - after Job!\");\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u200b Spring Batch\u662f\u4e2a\u8f7b\u91cf\u7ea7\u7684\u3001 \u5b8c\u5584\u7684\u6279\u5904\u7406\u6846\u67b6,\u65e8\u5728\u5e2e\u52a9\u4f01\u4e1a\u5efa\u7acb\u5065\u58ee\u3001\u9ad8\u6548\u7684\u6279\u5904\u7406\u5e94\u7528\u3002Spri [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8350"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8350"}],"version-history":[{"count":7,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8350\/revisions"}],"predecessor-version":[{"id":8364,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/8350\/revisions\/8364"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}