Spring Batch

꽃봉우리·2024년 8월 6일

Spring Batch

스프링 배치(Spring Batch)는 대용량 데이터 처리 작업을 쉽게 관리하고 실행할 수 있게 해주는 스프링 프레임워크의 모듈입니다. 주로 대량의 데이터 처리, 배치 작업 및 일괄 작업을 자동화하고 효율적으로 처리하는 데 사용됩니다.

구성 요소

  • Job: 배치 작업의 최상위 컨테이너. 하나 이상의 Step으로 구성됩니다.

  • Step: 독립적으로 실행 가능한 배치 작업의 단위. 통상적으로 ItemReader, ItemProcessor, - ItemWriter로 구성됩니다.

  • ItemReader: 데이터를 읽어오는 역할을 합니다.

  • ItemProcessor: 읽어온 데이터를 가공하는 역할을 합니다.

  • ItemWriter: 가공된 데이터를 저장하는 역할을 합니다.

예제

  • 간단한 콘솔 출력 배치 작업
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
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.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.listener.JobExecutionListenerSupport;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Bean
    public Job job(JobBuilderFactory jobBuilderFactory, Step step) {
        return jobBuilderFactory.get("job")
                .incrementer(new RunIdIncrementer())
                .listener(listener())
                .flow(step)
                .end()
                .build();
    }

    @Bean
    public Step step(StepBuilderFactory stepBuilderFactory) {
        return stepBuilderFactory.get("step")
                .tasklet(tasklet())
                .build();
    }

    @Bean
    public Tasklet tasklet() {
        return (contribution, chunkContext) -> {
            System.out.println("Hello, Spring Batch!");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public JobExecutionListener listener() {
        return new JobExecutionListenerSupport() {};
    }
}
  • 데이터베이스 읽고 처리 후 다른 데이터베이스에 쓰기
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.ItemProcessor;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@EnableBatchProcessing
public class DatabaseBatchConfiguration {

    @Bean
    public JdbcCursorItemReader<Person> reader(DataSource dataSource) {
        return new JdbcCursorItemReaderBuilder<Person>()
                .dataSource(dataSource)
                .name("personItemReader")
                .sql("SELECT first_name, last_name FROM people")
                .rowMapper((rs, rowNum) -> new Person(rs.getString("first_name"), rs.getString("last_name")))
                .build();
    }

    @Bean
    public ItemProcessor<Person, Person> processor() {
        return person -> {
            person.setFirstName(person.getFirstName().toUpperCase());
            person.setLastName(person.getLastName().toUpperCase());
            return person;
        };
    }

    @Bean
    public JdbcBatchItemWriter<Person> writer(DataSource dataSource) {
        return new

0개의 댓글