본문 바로가기

Web/spring

[Spring Batch] Configuring and Running a Job - (4) Configuring a JobLauncher

728x90

https://docs.spring.io/spring-batch/docs/current/reference/html/job.html#configuringJobLauncher

 

Configuring and Running a Job

If a group of Jobs share similar but not identical configurations, it may help to define a “parent” Job from which the concrete Job instances can inherit properties. Similar to class inheritance in Java, a “child” Job combines its elements and attr

docs.spring.io

 

틀린 해석이 있다면 알려주세용 🌯

 


Configuring a JobLauncher

JobRepository와 마찬가지로 @EnableBatchProcessing 애노테이션을 붙이면 자동으로 JobRegistry를 제공해준다

 

The most basic implementation of the JobLauncher interface is the TaskExecutorJobLauncher.

Its only required dependency is a JobRepository (needed to obtain an execution).

JobLauncher에서 가장 기본적인 구현은 TaskExecutorJobLauncheer 이다

JobRepository 에 대한 종속성이 필요하다

 

xml예시

<bean id="jobLauncher"
      class="org.springframework.batch.core.launch.support.TaskExecutorJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
</bean>

 

java예시

...
@Bean
public JobLauncher jobLauncher() throws Exception {
	TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
	jobLauncher.setJobRepository(jobRepository);
	jobLauncher.afterPropertiesSet();
	return jobLauncher;
}
...

 

Once a JobExecution is obtained, it is passed to the execute method of Job, ultimately returning the JobExecution to the caller

JobExecution을 얻으면 실행 메소드로 전달된 뒤 궁극적으로 caller에게 반환 시켜준다

 

Job Launcher Sequence

sequence 는 scheduler에서 시작할때 간단하게 잘 작동한다

 

 

 

그러나 아래처럼 HTTP 요청에서 시작하려고 할 때는 문제가 발생한다.

이 scenario에서 TaskExecutorJobLauncher가 호출자에게 즉시 반환되도록 실행을 비동기적으로 수행해야한다.

 장기 실행 프로세스(batch job)에 필요한 시간 동안 HTTP 요청이 open 되어 있는 상태로 유지되는 것은 좋지않다.

 

Asynchronous Job Launcher Sequence

이럴 때 TaskExecutor에 TaskExecutorJobLauncher을 구성해줄 수 있다

You can configure the TaskExecutorJobLauncher to allow for this scenario by configuring a TaskExecutor

 

 

example configures a TaskExecutorJobLauncher to return immediately

xml예시

<bean id="jobLauncher"
      class="org.springframework.batch.core.launch.support.TaskExecutorJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    <property name="taskExecutor">
        <bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
    </property>
</bean>

java예시

@Bean
public JobLauncher jobLauncher() {
	TaskExecutorJobLauncher jobLauncher = new TaskExecutorJobLauncher();
	jobLauncher.setJobRepository(jobRepository());
	jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
	jobLauncher.afterPropertiesSet();
	return jobLauncher;
}

You can use any implementation of the spring TaskExecutor interface to control how jobs are asynchronously executed.

 

 

728x90