spring batch la gi

Bài viết lách này bản thân tiếp tục trình làng cho tới người xem một định nghĩa này là Spring Batch. Vậy Spring Batch là gì ? Cùng thám thính hiểu ở nội dung bài viết này nhé

1. Một số định nghĩa cơ bản

a. Batch

  • Batch là gì ? Batch hoặc còn được gọi là Batch Processing, là công tác triển khai hàng loạt công việc nhưng mà ko Chịu đựng sự can thiệp kể từ người tiêu dùng.
  • Batch hoàn toàn có thể được dùng để làm lập lịch biểu và cung ứng những khoáng sản được chấp nhận.

b. Spring Batch

Spring Batch là một trong Framework xử lý batch (một chuỗi những công việc). một việc làm bao hàm nhiều steps và từng step bao hàm một trọng trách là READ-PROCESS-WRITE.

Bạn đang xem: spring batch la gi

c. Mybatis

MyBatis là một trong class persistence framework được đưa đến nhằm tương hỗ mang đến việc dùng SQL, dùng để làm tàng trữ và nâng lên tài năng liên kết

2. Cấu trúc của Spring Batch

Cấu trúc của một Spring Batch bao hàm 4 bộ phận như sau:

  • JobLaucher là một trong Interface cho 1 thực thi đua Job triển khai truyền thông số nguồn vào .
  • Job đang được khái niệm phía trên tiếp tục nhận thông số truyền nhập kể từ JobLaucher và thực thi đua từng bước đang được khái niệm nhập Job.
  • Step được khái niệm nhằm điều khiển và tinh chỉnh những phương pháp sinh hoạt của batch processing. Các bộ phận Step nhập step gồm những: Item Reader, Item Writer, Item Processor.
  • JobRepository cung ứng CRUD mang đến JobLaucher, Job, Step nhập quy trình thực thi đua.

3. Ví dụ về Spring Batch với JobRepository

a. Cài bịa đặt thư viện

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-batch'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.batch:spring-batch-test'
}Code language: JavaScript (javascript)

Cấu hình 

Cài bịa đặt tệp tin application.properties:

spring.batch.initialize-schema=always

Tạo một class DataSourceConfiguration nhằm thông số kỹ thuật liên kết cho tới hạ tầng tài liệu MySQL

Xem thêm: mắt biếc nguyễn nhật ánh

DataSourceConfiguration.java

package com.example.demo.configure;

import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfiguration {
    @Bean
    public DataSource dataSource(){
        return DataSourceBuilder
                .create()
                .username("root")
                .password("123456")
                .url("jdbc:mysql://localhost:3306/demo-spring-batch")
                .build();
    }
}
Code language: JavaScript (javascript)

Cấu hình Batch vày class BatchConfiguration.java như sau

BatchConfiguration.java

Xem thêm: một đồng tiền xu

package com.example.demo.configure;

import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
@EnableBatchProcessing
@Import({DataSourceConfiguration.class})
public class BatchConfiguration implements BatchConfigurer {
    @Autowired
    private DataSource dataSource;

    @Override
    public JobRepository getJobRepository() throws Exception {
        return createNewJobRepository();
    }

    @Override
    public PlatformTransactionManager getTransactionManager() throws Exception {
        return new DataSourceTransactionManager(dataSource);
    }

    @Override
    public JobLauncher getJobLauncher() throws Exception {
        return null;
    }

    @Override
    public JobExplorer getJobExplorer() throws Exception {
        return null;
    }

    public JobRepository createNewJobRepository() throws Exception {
        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTransactionManager(getTransactionManager());
        factory.afterPropertiesSet();
        return factory.getObject();
    }

    @Bean
    public PlatformTransactionManager platformTransactionManager() throws Exception {
        return getTransactionManager();
    }
    @Bean
    public JobRepository jobRepository() throws Exception {
        return getJobRepository();
    }
}
Code language: PHP (php)

Controller

Chúng tớ tạo ra một controller mang tên là DemoController nhằm đánh giá demo như sau

package com.example.demo.controller;

import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {
    @Autowired
    private JobRepository jobRepository;

    @Autowired
    private DefaultBatchConfigurer defaultBatchConfigurer;

    @GetMapping("/jobRepository")
    @ResponseBody
    public String execute() throws Exception {
        JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
        jobParametersBuilder.addString("firstParameter", "1");
        defaultBatchConfigurer.getJobRepository().createJobExecution("Quân", jobParametersBuilder.toJobParameters());
        return "<h1>Batch Success</h1>";
    }
}
Code language: JavaScript (javascript)

Vậy là xong xuôi tất cả chúng ta hãy nằm trong chạy demo và cảm biến nhé ^^

>> Xem ngay lập tức Tài liệu Java bộ vi xử lý Core khiến cho bạn “Nâng Cấp” tài năng lập trình