본문 바로가기

spring

[Spring Batch] 잡 관리 컴포넌트 - 1. JobExplorer

JobExplorer 

- org.springframework.batch.core.explore.JobExplorer

 

 

https://docs.spring.io/spring-batch/docs/current/api/org/springframework/batch/core/explore/JobExplorer.html

 

JobExplorer (Spring Batch 4.3.3 API)

All Known Implementing Classes: SimpleJobExplorer public interface JobExplorer Entry point for browsing executions of running or historical jobs and steps. Since the data may be re-hydrated from persistent storage, it may not contain volatile fields that w

docs.spring.io

 

 

 스프링 배치는 내부적으로 여러 DAO를 사용해 JobRepository 테이블에 접근 하긴 하지만, 프레임워크와 개발자가 사용할 수 있도록 훨씬 실용적인 API 를 제공하기도 한다. 그중 JobExplorer 컴포넌트를 사용하여 데이터 저장소(메타데이터)에 접근 가능하다.  

 

 

 

잡 관리 컴포넌트 간의 관계

 

 

  그림에서 대부분의 배치 프레임워크 컴포넌트가 JobRepository를 사용해 잡 실행에 관련해 저장된 정보에 접근 하지만 JobExplorer는 데이터 베이스에 직접 접근한다. 또한 JobExplorer 는 읽기 전용으로 사용된다. 

 

 사용하려는 데이터에 안전한 방법으로 접근하고 싶다면 JobExplorer 와 같은 API 를 사용하기 바란다. 

 

 

-  jobExplorer 는  JobInstance및 JobExcution과 관련된 정보를 얻을 수 있는 7가지 메서드를 제공한다. 

JobExplorer (Spring Batch 4.3.3 API) - https://docs.spring.io/spring-batch/docs/current/api/org/springframework/batch/core/explore/JobExplorer.html

 

- 예제 

package demo;

import java.util.List;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
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.explore.JobExplorer;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;


@EnableBatchProcessing
@SpringBootApplication
public class DemoApplication {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private JobExplorer jobExplorer;

    @Bean
    public Tasklet explorerTasklet() {
        return new ExploringTasklet(this.jobExplorer);
    }

    @Bean
    public Step explorerStep() {
        return this.stepBuilderFactory.get("explorerStep")
                .tasklet(explorerTasklet())
                .build();
    }

    @Bean
    public Job explorerJob() {
        return this.jobBuilderFactory.get("explorerJob")
                .start(explorerStep())
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    public class ExploringTasklet implements  Tasklet {
        private JobExplorer explorer;

        public ExploringTasklet(JobExplorer explorer) {
            this.explorer = explorer;
        }

        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
            String jobName = chunkContext.getStepContext().getJobName();

            List<JobInstance> instances = explorer.getJobInstances(jobName, 0, Integer.MAX_VALUE);
            System.out.println(
                    String.format("There are %d job instances for the job %s",
                            instances.size(),
                            jobName));
            System.out.println("they have had the following result");

            for (JobInstance instance : instances) {
                List<JobExecution> jobExecutions = this.explorer.getJobExecutions(instance);

                System.out.println(String.format("Instance %d had %d executions",
                        instance.getInstanceId(),
                        jobExecutions.size()));
                for (JobExecution jobExecution : jobExecutions) {
                    System.out.println(String.format("\tExecution %d resulted in Exit Status %s", jobExecution.getId(), jobExecution.getExitStatus()));
                }
            }
            return null;
        }
    }

}

 

 

반응형