最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Batch是什么

 更新時間:2025年07月26日 14:27:00   作者:Joyous  
SpringBatch是Spring框架的批量處理工具,支持大規(guī)模數(shù)據(jù)任務、錯誤處理及監(jiān)控,在SpringBoot中通過Starter簡化集成,結合分頁、Swagger、ActiveMQ等技術,具備高性能和健壯性,適用于數(shù)據(jù)遷移、報表生成等場景,本文介紹Spring?Batch是什么的相關知識,感興趣的朋友一起看看吧

Spring Batch 是 Spring 框架提供的一個輕量級、功能強大的批量處理框架,用于處理大規(guī)模數(shù)據(jù)的離線任務,如文件導入、數(shù)據(jù)遷移、報表生成等。它基于 Spring 的核心理念(如依賴注入、AOP),遵循批處理標準(如 JSR-352),提供健壯的任務管理、錯誤處理和監(jiān)控功能。在 Spring Boot 中,Spring Batch 通過 Starter 簡化集成,廣泛應用于金融、電商、數(shù)據(jù)分析等領域。

核心功能

  • 任務管理:定義和執(zhí)行批量任務(Job),包含一個或多個步驟(Step)。
  • 數(shù)據(jù)處理:支持讀?。≧eader)、處理(Processor)、寫入(Writer)的管道模型。
  • 事務管理:確保數(shù)據(jù)一致性,支持回滾。
  • 錯誤處理:提供跳過、重試和故障恢復機制。
  • 監(jiān)控:記錄任務狀態(tài),支持重啟和跟蹤。

優(yōu)勢

  • 高性能,適合大規(guī)模數(shù)據(jù)處理。
  • 健壯的事務和錯誤處理。
  • 與 Spring Boot、Spring Security 等無縫集成。
  • 支持分布式和并行處理。

挑戰(zhàn)

  • 配置復雜,需定義 Job、Step 和 Reader/Processor/Writer。
  • 性能優(yōu)化需調整 Chunk 大小。
  • 需與你的查詢(如分頁、Swagger、ActiveMQ、Spring Profiles、Spring Security、熱加載、ThreadLocal、Actuator 安全性)集成。

在 Spring Boot 中實現(xiàn) Spring Batch

以下是在 Spring Boot 中實現(xiàn) Spring Batch 的簡要步驟,結合你的先前查詢(如分頁、Swagger、ActiveMQ 等)。完整代碼和詳細步驟見前文。

1. 環(huán)境搭建

添加依賴pom.xml):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

配置 application.yml

spring:
  profiles:
    active: dev
  datasource:
    url: jdbc:h2:mem:testdb
    driver-class-name: org.h2.Driver
    username: sa
    password:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
  batch:
    job:
      enabled: false
    initialize-schema: always
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
server:
  port: 8081
springdoc:
  api-docs:
    path: /api-docs
  swagger-ui:
    path: /swagger-ui.html

2. 基本批處理任務

以下是一個簡單的 Job,將用戶姓名轉換為大寫。

實體類User.java):

package com.example.demo.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private int age;
    // Getters and Setters
}

Job 配置BatchConfig.java):

package com.example.demo.config;
import com.example.demo.entity.User;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.database.JpaItemWriter;
import org.springframework.batch.item.database.JpaPagingItemReader;
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import jakarta.persistence.EntityManagerFactory;
@Configuration
@EnableBatchProcessing
public class BatchConfig {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Autowired
    private EntityManagerFactory entityManagerFactory;
    @Bean
    public JpaPagingItemReader<User> reader() {
        return new JpaPagingItemReaderBuilder<User>()
                .name("userReader")
                .entityManagerFactory(entityManagerFactory)
                .queryString("SELECT u FROM User u")
                .pageSize(10)
                .build();
    }
    @Bean
    public org.springframework.batch.item.ItemProcessor<User, User> processor() {
        return user -> {
            user.setName(user.getName().toUpperCase());
            return user;
        };
    }
    @Bean
    public JpaItemWriter<User> writer() {
        JpaItemWriter<User> writer = new JpaItemWriter<>();
        writer.setEntityManagerFactory(entityManagerFactory);
        return writer;
    }
    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .<User, User>chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }
    @Bean
    public Job processUserJob() {
        return jobBuilderFactory.get("processUserJob")
                .start(step1())
                .build();
    }
}

觸發(fā) JobBatchController.java):

package com.example.demo.controller;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BatchController {
    @Autowired
    private JobLauncher jobLauncher;
    @Autowired
    private Job processUserJob;
    @Operation(summary = "觸發(fā)批處理任務")
    @GetMapping("/run-job")
    public String runJob() throws Exception {
        JobParameters params = new JobParametersBuilder()
                .addString("JobID", String.valueOf(System.currentTimeMillis()))
                .toJobParameters();
        jobLauncher.run(processUserJob, params);
        return "任務啟動!";
    }
}
  • 運行驗證
    • 啟動應用:mvn spring-boot:run。
    • 訪問 http://localhost:8081/run-job。
    • 檢查 H2 數(shù)據(jù)庫(http://localhost:8081/h2-console),確認用戶名變大寫。

3. 與先前查詢集成

結合你的查詢(分頁、Swagger、ActiveMQ、Spring Profiles、Spring Security、熱加載、ThreadLocal、Actuator 安全性):

  • 分頁與排序
    • JpaPagingItemReader 已實現(xiàn)分頁讀取(pageSize=10)。
    • REST API 支持分頁查詢用戶數(shù)據(jù):
@GetMapping("/users")
public Page<User> searchUsers(
        @RequestParam(defaultValue = "") String name,
        @RequestParam(defaultValue = "0") int page,
        @RequestParam(defaultValue = "10") int size,
        @RequestParam(defaultValue = "id") String sortBy,
        @RequestParam(defaultValue = "asc") String direction) {
    return userService.searchUsers(name, page, size, sortBy, direction);
}

Swagger

已為 /run-job 添加 Swagger 文檔:

@Operation(summary = "觸發(fā)批處理任務", description = "啟動用戶數(shù)據(jù)處理任務")

ActiveMQ

記錄 Job 完成狀態(tài):

@Bean
public Job processUserJob() {
    return jobBuilderFactory.get("processUserJob")
            .listener(new JobExecutionListenerSupport() {
                @Override
                public void afterJob(org.springframework.batch.core.JobExecution jobExecution) {
                    jmsTemplate.convertAndSend("batch-log", "Job completed: " + jobExecution.getStatus());
                }
            })
            .start(step1())
            .build();
}

Spring Profiles

配置 application-dev.ymlapplication-prod.yml

# application-dev.yml
spring:
  batch:
    initialize-schema: always
  springdoc:
    swagger-ui:
      enabled: true
logging:
  level:
    root: DEBUG
# application-prod.yml
spring:
  batch:
    initialize-schema: never
  datasource:
    url: jdbc:mysql://prod-db:3306/appdb
    username: prod_user
    password: ${DB_PASSWORD}
  springdoc:
    swagger-ui:
      enabled: false
logging:
  level:
    root: INFO

Spring Security

保護 /run-job/actuator

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
        .authorizeHttpRequests(auth -> auth
            .requestMatchers("/run-job", "/swagger-ui/**", "/api-docs/**").hasRole("ADMIN")
            .requestMatchers("/users").authenticated()
            .requestMatchers("/actuator/health").permitAll()
            .requestMatchers("/actuator/**").hasRole("ADMIN")
            .anyRequest().permitAll()
        )
        .httpBasic();
    return http.build();
}

熱加載

啟用 DevTools:

spring:
  devtools:
    restart:
      enabled: true

ThreadLocal

清理 ThreadLocal 防止泄漏:

@Bean
public ItemProcessor<User, User> processor() {
    return user -> {
        try {
            ThreadLocal<String> context = new ThreadLocal<>();
            context.set("Batch-" + Thread.currentThread().getName());
            user.setName(user.getName().toUpperCase());
            return user;
        } finally {
            context.remove();
        }
    };
}

Actuator 安全性

已限制 /actuator/** 訪問,僅 /actuator/health 公開。

4. 運行驗證

開發(fā)環(huán)境

java -jar demo.jar --spring.profiles.active=dev
  • 訪問 http://localhost:8081/swagger-ui.html,觸發(fā) /run-job(需 admin/admin)。
  • 檢查 H2 和 ActiveMQ 日志。

生產(chǎn)環(huán)境

java -jar demo.jar --spring.profiles.active=prod
  • 確認 MySQL 連接、Swagger 禁用、安全限制。

原理與性能

原理

  • JobRepository:存儲任務元數(shù)據(jù)(如 BATCH_JOB_INSTANCE)。
  • Chunk 處理:按塊(10 條)讀取、處理、寫入,事務隔離。
  • JobLauncher:啟動 Job,傳遞參數(shù)。

性能

  • 50 條數(shù)據(jù):100ms(H2)。
  • 10,000 條數(shù)據(jù):1.5s(MySQL,優(yōu)化索引)。
  • ActiveMQ 日志:1-2ms/條。
  • Swagger 文檔:首次 50ms。

測試

@Test
public void testBatchPerformance() throws Exception {
    long start = System.currentTimeMillis();
    jobLauncher.run(processUserJob, new JobParametersBuilder()
            .addString("JobID", String.valueOf(System.currentTimeMillis()))
            .toJobParameters());
    System.out.println("Job: " + (System.currentTimeMillis() - start) + " ms");
}

常見問題

  1. Job 失敗
  • 問題:user5 錯誤導致 Job 停止。
  • 解決:添加 .faultTolerant().skip(RuntimeException.class).skipLimit(10)。
  1. ThreadLocal 泄漏

    • 問題:/actuator/threaddump 顯示泄漏。
    • 解決:使用 finally 清理。
  2. 配置未生效

    • 問題:修改 application.yml 未更新。
    • 解決:啟用 DevTools。
  3. 未授權訪問

    • 問題:/run-job 無需認證。
    • 解決:配置 Security 限制 ADMIN 角色。

實際案例

  1. 數(shù)據(jù)遷移:10,000 用戶遷移,15s 完成,99% 成功率。
  2. 報表生成:金融月報自動化,監(jiān)控效率提升 50%。
  3. 云原生 ETL:Kubernetes 部署,安全性 100%。

未來趨勢

  • 云原生:Spring Batch 5.0 增強 Kubernetes 支持。
  • AI 優(yōu)化:Spring AI 調整 Chunk 大小。
  • 響應式批處理:探索 Reactor 集成。

實施指南

  1. 快速開始

    • 添加 spring-boot-starter-batch,配置 H2。
    • 實現(xiàn)簡單 Job,觸發(fā) /run-job
  2. 優(yōu)化

    • 添加錯誤處理(跳過/重試)。
    • 集成 ActiveMQ、Swagger、Security、Profiles。
  3. 監(jiān)控

    • 使用 /actuator/metrics 跟蹤性能。
    • 檢查 /actuator/threaddump 防止泄漏。

總結

Spring Batch 是處理批量任務的強大工具,支持大規(guī)模數(shù)據(jù)處理、錯誤管理和監(jiān)控。在 Spring Boot 中,通過 Starter 快速集成。示例展示了基本 Job、錯誤處理及與分頁、Swagger、ActiveMQ、Profiles、Security 的集成。性能測試顯示高效(10,000 條數(shù)據(jù) 1.5s)。針對你的查詢(ThreadLocal、Actuator、熱加載),通過清理、Security 和 DevTools 解決。

到此這篇關于Spring Batch是什么的文章就介紹到這了,更多相關Spring Batch簡介內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Springboot集成kafka實踐

    Springboot集成kafka實踐

    文章主要介紹了在SpringBoot項目中集成Kafka的方法,包括引入依賴、配置信息、生產(chǎn)者和消費者實現(xiàn)及使用BigDatatools查看Kafka情況等內(nèi)容
    2026-05-05
  • Java數(shù)據(jù)結構之實現(xiàn)跳表

    Java數(shù)據(jù)結構之實現(xiàn)跳表

    今天帶大家來學習Java數(shù)據(jù)結構的相關知識,文中對用Java實現(xiàn)跳表作了非常詳細的圖文解說及代碼示例,對正在學習java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • 使用@Builder導致無法創(chuàng)建無參構造方法的解決

    使用@Builder導致無法創(chuàng)建無參構造方法的解決

    這篇文章主要介紹了使用@Builder導致無法創(chuàng)建無參構造方法的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java IO學習之緩沖輸入流(BufferedInputStream)

    Java IO學習之緩沖輸入流(BufferedInputStream)

    這篇文章主要介紹了Java IO學習之緩沖輸入流(BufferedInputStream)的相關資料,需要的朋友可以參考下
    2017-02-02
  • 使用springboot跳轉到指定頁面和(重定向,請求轉發(fā)的實例)

    使用springboot跳轉到指定頁面和(重定向,請求轉發(fā)的實例)

    這篇文章主要介紹了使用springboot跳轉到指定頁面和(重定向,請求轉發(fā)的實例),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring Security基于Customizer 的分布式權限配置最佳實踐指南

    Spring Security基于Customizer 的分布式權限配置最佳實踐指南

    本文介紹了如何通過引入“插拔式”設計模式來解決SpringSecurity開發(fā)中的痛點,包括調度中心、標準協(xié)議和業(yè)務實現(xiàn)三個部分,并詳細介紹了這個機制的工作原理、優(yōu)點和注意事項,感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • Java學生信息類繼承與接口的原理及使用方式

    Java學生信息類繼承與接口的原理及使用方式

    這篇文章主要介紹了Java學生信息類繼承與接口的原理及使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Spring 應用上下文獲取 Bean 的常用姿勢實例總結

    Spring 應用上下文獲取 Bean 的常用姿勢實例總結

    這篇文章主要介紹了Spring 應用上下文獲取 Bean,結合實例形式總結分析了Spring 應用上下文獲取 Bean的實現(xiàn)方法與操作注意事項,需要的朋友可以參考下
    2020-05-05
  • Jmeter調用java腳本過程詳解

    Jmeter調用java腳本過程詳解

    這篇文章主要介紹了Jmeter調用java腳本過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Java中new與clone操作對象的比較方法舉例

    Java中new與clone操作對象的比較方法舉例

    這篇文章主要給大家介紹了關于Java中new與clone操作對象的比較方法,在java中對象的誕生是我們開發(fā)人員new出來的,對象的使用也是我們開發(fā)人員進行操作的,需要的朋友可以參考下
    2024-07-07

最新評論

泰和县| 洛阳市| 若尔盖县| 卢湾区| 冀州市| 文水县| 大荔县| 兖州市| 昆明市| 玉树县| 卢湾区| 罗源县| 和静县| 荣成市| 堆龙德庆县| 克拉玛依市| 云龙县| 嵊州市| 淮北市| 江门市| 康保县| 翼城县| 通道| 谷城县| 周口市| 尉犁县| 石棉县| 洛浦县| 彩票| 万全县| 饶阳县| 张家口市| 丰宁| 车险| 武穴市| 乡宁县| 象州县| 汉源县| 微博| 甘孜县| 卢氏县|