ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring Batch Job 알아보기
    Language/Spring 2023. 11. 24. 23:51
    728x90
    반응형

     

    - 목차

     

    JobLauncher.

    JobLauncher 는 스프링 배치를 구성하는 대표적인 컴포넌트입니다.

    스프링으로 웹서버를 구현할 때에 Controller, Service 등이 필요하듯, 스프링 배치를 구동하기 위해서 JobLauncher 가 필요합니다.

    이름에서 알 수 있듯이 JobLauncher 는 Job 를 실행하도록 돕는 컴포넌트입니다.

    JobLauncher 는 run 이라는 메소드를 가지구요.

    run 메소드를 통해서 특정 Job 을 실행할 수 있습니다.

     

    아래 코드 예시는 스프링 배치에서 Job 을 실행하는 코드입니다.

     

    < Spring Batch code Example >

    import org.springframework.batch.core.Job;
    import org.springframework.batch.core.JobExecution;
    import org.springframework.batch.core.JobParameters;
    import org.springframework.batch.core.launch.JobLauncher;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class MyJobService {
    
        @Autowired
        private JobLauncher jobLauncher;
    
        @Autowired
        private Job myJob; // Inject the job you want to launch
    
        public void launchMyJob() {
            try {
                JobParameters jobParameters = new JobParametersBuilder()
                    .addString("param1", "value1")
                    .toJobParameters();
    
                JobExecution jobExecution = jobLauncher.run(myJob, jobParameters);
    
                System.out.println("Job Status: " + jobExecution.getStatus());
                System.out.println("Exit Status: " + jobExecution.getExitStatus());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

     

     

    Job.

    Job 은 실행해야하는 배치 단위입니다.

    Job 은 스프링에서 Bean 으로 관리됩니다.

    즉, IoC Container 에서 관리되는 자바 오브젝트입니다.

    Job 은 ETL 프로세스를 처리할 수 있는 인터페이스를 가지며,

    각 ETL 단계를 Step 이라는 또 다른 Bean 으로 구성됩니다.

     

    아래 예시는 Job 과 관련된 코드 예시입니다.

     

    Job 이라는 Bean 을 생성하기 위해서

    Step 이라는 Bean 이 필요합니다.

    Step 은 실질적인 실행 로직을 가지는 실행단위입니다.

    Job 은 Step 들을 관리하는 상위 요소이죠.

    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.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableBatchProcessing
    public class MyBatchConfiguration {
    
        private final JobBuilderFactory jobBuilderFactory;
        private final StepBuilderFactory stepBuilderFactory;
    
        public MyBatchConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
            this.jobBuilderFactory = jobBuilderFactory;
            this.stepBuilderFactory = stepBuilderFactory;
        }
    
        @Bean
        public Step myStep() {
            // Define and configure the Step
            return stepBuilderFactory.get("myStep")
                .<String, String>chunk(10)
                .reader(myItemReader())
                .processor(myItemProcessor())
                .writer(myItemWriter())
                .build();
        }
    
        @Bean
        public Job myJob() {
            // Define and configure the Job with one or more Steps
            return jobBuilderFactory.get("myJob")
                .start(myStep())
                .build();
        }
    
        // Other bean definitions for ItemReader, ItemProcessor, ItemWriter, etc.
    }

     

     

     

    Job - start, next.

    Job 은 Step 을 등록하기 위해서 start 와 next 라는 인터페이스를 가집니다.

    start 는 첫번째 Step 을 등록하기 위한 인터페이스이구요.

    next 는 두번째 그리고 추가적인 Step 을 등록하기 위해서 사용됩니다.

    하나의 Step 만을 필요로하는 Job 이라면 start 하나만 사용하면 됩니다.

     

    예를 들어보겠습니다.

    아래 예시처럼 2개의 Step 을 가지는 Job 은 아래와 같이 표현됩니다.

     

    
    @Bean
    public Job myJob() {
        return jobBuilderFactory.get("myJob")
            .start(myFirstStep())
            .next(mySecondStep())
            .next(myThirdStep())
            .build();
    }

     

    Step.

    Step 은 Job 의 실질적인 실행 내용를 구현한 단위입니다.

    Step 또한 Bean 으로 관리됩니다.

     

    Step 은 내부적으로 Builder 디자인 패턴을 사용하며,

    - reader

    - processor

    - writer

    를 추가하여 사용합니다.

    전형적인 ETL 프로세스의 인터페이스를 가집니다.

     

     

    Step - Reader.

    Reader 는 배치 프로세스의 시작점에서 데이터를 읽어들이는 과정입니다.

    어떠한 처리를 하기 위해서 input 데이터가 필요할 수 있는데,

    Reader 가 해당 input 데이터를 조회하는 단계입니다.

    ETL 에서 Extract 단계라고 생각하시면 좋습니다.

     

    장기간 접속하지 않은 사용자를 휴면처리하는 배치를 예를 들어보겠습니다.

    이때, 장기간 접속하지 않은 사용자들을 조회하는 단계가 Extract 에 해당하는 Reader 입니다.

     

     

     

     

     

     

     

     

     

     

     

    Step - chunk.

    chunk 는 Step 에서 설정할 수 있는 데이터 처리양입니다.

    Reader -> Processor -> Writer 로 이어지는 ETL 프로세스에서 한번의 단계에서 처리하는 데이터의 양이죠.

    예를 들어, Reader 가 100개의 데이터를 읽어들인다고 하겠습니다.

    그리고 chunk 가 10 이라면, 총 10번의 Read -> Process -> Write 프로세스가 실행됩니다.

     

    1. Read and Process Chunk 1 (items 1-10):
    Read: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Process and Write: [Processed1, Processed2, ..., Processed10]
    
    2. Read and Process Chunk 2 (items 11-20):
    Read: [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
    Process and Write: [Processed11, Processed12, ..., Processed20]
    
    ...
    
    10. Read and Process Chunk 10 (items 99-100):
    Read: [91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
    Process and Write: [Processed91, Processed92, ..., Processed100]

     

     

     

    Listener.

    Listener 는 Job 또는 Step 의 라이프사이클과 관련된 설정을 등록하는 역할을 합니다.

    여러 프레임워크에서 특정 컴포넌트의 생성과 소멸 그리고 그 사이의 시점에 callback 을 등록할 수 있습니다.

    예를 들어 Android, IOS, Angular 등 onStart, onFinish 와 같은 라이프사이클이 존재하죠.

    JUnit 에서도 BeforeEach, BeforeAll, AfterAll 과 같은 테스트 단위의 라이프사이클이 존재합니다.

     

    스프링 배치에서도 Job 과 Step 의 라이프사이클에 접근할 수 있는 방식이 제공됩니다.

     

    - JobListener

    - StepListener

    - ChunkListener

     

    관련된 예시입니다.

     

    < JobListener >

    import org.springframework.batch.core.JobExecution;
    import org.springframework.batch.core.JobExecutionListener;
    
    public class MyJobExecutionListener implements JobExecutionListener {
    
        @Override
        public void beforeJob(JobExecution jobExecution) {
            // Executed before the job starts
            System.out.println("Job started!");
        }
    
        @Override
        public void afterJob(JobExecution jobExecution) {
            // Executed after the job completes
            System.out.println("Job completed!");
        }
    }

     

     

    < StepListener >

    import org.springframework.batch.core.StepExecution;
    import org.springframework.batch.core.listener.StepExecutionListenerSupport;
    
    public class MyStepExecutionListener extends StepExecutionListenerSupport {
    
        @Override
        public void beforeStep(StepExecution stepExecution) {
            // Executed before the step starts
            System.out.println("Step started!");
        }
    
        @Override
        public ExitStatus afterStep(StepExecution stepExecution) {
            // Executed after the step completes
            System.out.println("Step completed!");
            return null; // Return ExitStatus
        }
    }

     

     

    < ChunkListener >

    import org.springframework.batch.core.ChunkListener;
    import org.springframework.batch.core.scope.context.ChunkContext;
    
    public class MyChunkListener implements ChunkListener {
    
        @Override
        public void beforeChunk(ChunkContext context) {
            // Executed before the chunk starts
            System.out.println("Chunk started!");
        }
    
        @Override
        public void afterChunk(ChunkContext context) {
            // Executed after the chunk completes
            System.out.println("Chunk completed!");
        }
    
        @Override
        public void afterChunkError(ChunkContext context) {
            // Executed if an error occurs during chunk processing
            System.out.println("Chunk error!");
        }
    }
    반응형

    'Language > Spring' 카테고리의 다른 글

    Spring Bean 알아보기  (2) 2023.10.30
    Spring IoC Container 알아보기  (0) 2023.10.30
Designed by Tistory.