1. Multi Job

 - 한개의 Application 에서 여러개의 Job을 만들기


2. Job을 2개 만든다.

 1) 이전 글에서 만든 Job

/**
* Basic Configuration
*
* @author akageun
*/
@Configuration
public class BasicConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(BasicConfiguration.class);

private static final String BASIC_JOB_NM = "BASIC_JOB";
private static final String BASIC_STEP_NM = "BASIC_TASKLET_STEP";

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

/**
* Basic Job Configuration
*
* @return
*/
@Bean(name = BASIC_JOB_NM)
public Job basicJob() {
//@formatter:off
return jobBuilderFactory
.get(BASIC_JOB_NM)
.incrementer(new RunIdIncrementer())
.start(basicTaskletStep())
.build();
//@formatter:on
}

/**
* Basic Step Configuration
*
* @return
*/
@Bean(name = BASIC_STEP_NM)
public Step basicTaskletStep() {
//@formatter:off
return stepBuilderFactory
.get(BASIC_STEP_NM)
.tasklet((stepContribution, chunkContext) -> {
LOG.info("Tasklet Run!!");

return RepeatStatus.FINISHED;
})
.build();
//@formatter:on
}
}


 2) 새로 만든 Multi Job


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
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.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* MULTI BASIC Configuration
*
* @author akageun
*/
@Configuration
public class MultiBasicTestConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(MultiBasicTestConfiguration.class);

private static final String MULTI_BASIC_JOB_NM = "MULTI_BASIC_JOB";
private static final String MULTI_BASIC_STEP_NM = "MULTI_BASIC_TASKLET_STEP";

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

/**
* MULTI_BASIC Job Configuration
*
* @return
*/
@Bean(name = MULTI_BASIC_JOB_NM)
public Job multiBasicJob() {
//@formatter:off
return jobBuilderFactory
.get(MULTI_BASIC_JOB_NM)
.incrementer(new RunIdIncrementer())
.start(multiBasicTaskletStep())
.build();
//@formatter:on
}

/**
* MULTI_BASIC Step Configuration
*
* @return
*/
@Bean(name = MULTI_BASIC_STEP_NM)
public Step multiBasicTaskletStep() {
//@formatter:off
return stepBuilderFactory
.get(MULTI_BASIC_STEP_NM)
.tasklet((stepContribution, chunkContext) -> {
LOG.info("Tasklet Multi Job Run!!");

return RepeatStatus.FINISHED;
})
.build();
//@formatter:on
}
}


3. 실행

 1) application.yml 내 옵션 설정

spring:
batch:
job:
enabled: true

 - 위와 같이 설정할 경우 해당 App내에 모든 Job이 실행된다.


 2) application 내 옵션

spring:
batch:
job:
enabled: false
names: BASIC_JOB,MULTI_BASIC_JOB

- 위와같이 설정할 경우 names에 들어있는 Job들만 실행된다. (Job들은 콤마(,)로 구분된다)


 3) jar파일 실행

java -jar ./BATCH_STUDY.jar --spring.batch.job.names=BASIC_JOB,MULTI_BASIC_JOB


 4) intellij 실행


4. 결과


5. 전체소스보기 : 바로가기

+ Recent posts