-
Notifications
You must be signed in to change notification settings - Fork 0
Spring Boot Batch
stella edited this page Jun 19, 2014
·
6 revisions
- Batch 수행에 대해 Boot에서 간단하게 처리해 줌. (@EnableBatchProcessing (from Spring Batch))
- 모든 Job을 실행시키는게 디폴트이지만 특정 JOB을 지정할 수도 있음. (프로퍼티 파일을 통해 지정)
Spring Batch auto configuration is enabled by adding @EnableBatchProcessing (from Spring Batch) somewhere in your context. By default it executes all Jobs in the application context on startup (see JobLauncherCommandLineRunner for details). You can narrow down to a specific job or jobs by specifying spring.batch.job.names (comma separated job name patterns). If the application context includes a JobRegistry then the jobs in spring.batch.job.names are looked up in the registry instead of being autowired from the context. This is a common pattern with more complex systems where multiple jobs are defined in child contexts and registered centrally. See BatchAutoConfiguration and @EnableBatchProcessing for more details.
- Boot 에서 내부적으로 JobLauncherCommandLineRunner를 생성, 지정한 JOB들을 실행시킨다.
- Job 구동 환경에 대해 간단하게 설정 가능
@Configuration
@ComponentScan
@EnableBatchProcessing
@EnableAutoConfiguration
public class MelonApplication {
public static void main( String [] args ) {
SpringApplication app = new SpringApplication(MelonApplication . class);
app .run(args );
}
}- Boot는 context를 로드할때 job repository에 등록도니 모든 job을 실행시키는게 default.
- 실행시킬 job들을 따로 리스트업도 가능하지만 properties 파일에 명시해야 함.
- 이를 우회하는 방법을 선택 --> 이게 맞는지 의문...
- spring.batch.job.enabled=false --> context로드 시 배치 실행을 막음
- 아래와 같은 코드로 JOB 실행이 가능하긴 함.
ConfigurableApplicationContext ctx = SpringApplication.run(MelonApplication.class, args);
JobLauncher launcher = ctx.getBean(JobLauncher.class);
try {
launcher.run(ctx.getBean(jobName, Job.class), new JobParameters());
} catch (BeansException
| JobExecutionAlreadyRunningException
| JobRestartException
| JobInstanceAlreadyCompleteException
| JobParametersInvalidException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}- Job Parameter 전달 관련
- 위 코드에서 파라메터를 주려면 "JobParameterBuilder"를 통해 파라메터 생성 후 Job에 전달
JobParametersBuilder paramBuilder = new JobParametersBuilder();
paramBuilder.addString("date", date);
launcher.run(ctx.getBean(jobName, Job.class), paramBuilder.toJobParameters());- 이때 Reader/Processor/Writer에서 이 파라메터를 전달받으려면 StepScope로 명시해줘야 함.
@BatchReader
@StepScope
@Slf4j
public class SampleItemReader implements ItemReader<Person> {
...
@Value("#{jobParameters['date']}")
public void setDate(final String date) {
log.warn("param date : {}", date);
}