본문 바로가기
개발/스프링

[SPRING BOOT] SPRING BATCH + SCHEDULER

by 로그인시러 2016. 8. 3.

scheduler를 통해서 batch 가 돌고, 

rest 기능을 수행하는 서버를 

spring boot 를 통해서 만들어야 할 일이 생겼다.

 

일단, maven !

 

  		 			org.springframework.boot 			spring-boot-starter-batch 		 		 			org.springframework.boot 			spring-boot-starter-web 		  		 			org.springframework.boot 			spring-boot-starter-test 			test 		 		 		 		 			org.codehaus.janino 			janino 		 		 		 		 			org.mybatis.spring.boot 			mybatis-spring-boot-starter 			1.0.0 		 		 		 			org.projectlombok 			lombok 			1.16.4 			provided 		 		 		 			com.oracle 			ojdbc6 			11.1.0.7.0 		 

 

 

보다시피, 기본 spring boot 설정에

spring-boot-starter-batch, spring-boot-starter-web 설정만 추가하였다.

헐 ~ 편하다. 개꿀이네ㅋㅋ

 

일단, batch를 schedule 하기 위해서는

JobLauncher 가 필요하다.

 

다음은 JobLaucher 를 위한 각종 bean에 대한 java config 이다.

 

@Configuration @EnableScheduling public class BatchScheduler { 	@Bean     public ResourcelessTransactionManager transactionManager() {         return new ResourcelessTransactionManager();     }      @Bean     public MapJobRepositoryFactoryBean mapJobRepositoryFactory(             ResourcelessTransactionManager txManager) throws Exception {                  MapJobRepositoryFactoryBean factory = new                  MapJobRepositoryFactoryBean(txManager);                  factory.afterPropertiesSet();                  return factory;     }      @Bean     public JobRepository jobRepository(             MapJobRepositoryFactoryBean factory) throws Exception {         return factory.getObject();     }      @Bean     public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {         SimpleJobLauncher launcher = new SimpleJobLauncher();         launcher.setJobRepository(jobRepository);         return launcher;     } } 

 

 

설정한 JobLaucher 는 Batch Configuration 클래스에서 아래와 같이 사용한다.

 

 

  @Configuration @EnableBatchProcessing @EnableScheduling @Slf4j //@Import({BatchScheduler.class}) public class ETLBatchConf {  	@Autowired 	public JobBuilderFactory jobBuilderFactory;  	@Autowired 	public StepBuilderFactory stepBuilderFactory; 	     @Autowired     private SimpleJobLauncher jobLauncher;  	@Bean 	public ETLReader etlReader() { 		return new ETLReader(); 	}  	@Bean 	public ItemWriter etlWriter() { 		// TODO Auto-generated method stub 		return new ETLWriter(); 	}  	@Bean 	public ETLProcessor etlProcessor() { 		// TODO Auto-generated method stub 		return new ETLProcessor(); 	}  	@Bean 	public JobExecutionListener etlListener() { 		return new ETLJobCompletionListener(); 	}  	// Configure job step 	@Bean //	public Job etlJob(@Qualifier("etlStep") Step step) { 	public Job etlJob() { 		return jobBuilderFactory.get("[ETL JOB]") 				.incrementer(new RunIdIncrementer()) 				.listener(etlListener()) 				.start(etlStep()) 				.build(); 		 		//start().next().next()  	}  	@Bean 	public Step etlStep() { 		return stepBuilderFactory.get("[ETL]") 				.<inputtype, outputtype=""> chunk(2) 				.reader(etlReader()) 				.processor(etlProcessor()) 				.writer(etlWriter()) 				.build(); 	}  	 //	@Scheduled(cron = "1 53/3 17 * * ?") 	@Scheduled(cron = "0 51 14 * * ?")     public void perform() throws Exception {         log.info("Job Started at :" + new Date());          JobParameters param = new JobParametersBuilder().addString("JobID",                 String.valueOf(System.currentTimeMillis())).toJobParameters();          JobExecution execution = jobLauncher.run(etlJob(), param);          log.info("Job finished with status :" + execution.getStatus());     } 	 } </inputtype,>

 

 

이외에 batch job 을 추가로 설정하고 싶을 수 있다.

아래는 tasklet 을 구현한 클래스로 설정하는 과정이다.

 

@Configuration @EnableBatchProcessing @EnableScheduling @Slf4j public class AnalysisBatchConf { 	@Autowired 	public JobBuilderFactory jobBuilderFactory;  	@Autowired 	public StepBuilderFactory stepBuilderFactory; 	     @Autowired     private SimpleJobLauncher jobLauncher;      @Scheduled(cron = "0 10 18 * * ?")     public void perform() throws Exception {         log.info("Job Started at :" + new Date());          JobParameters param = new JobParametersBuilder().addString("JobID",                 String.valueOf(System.currentTimeMillis())).toJobParameters();          JobExecution execution = jobLauncher.run(analysisJob(), param);          log.info("Job finished with status :" + execution.getStatus());     }  	private Job analysisJob() { 		// TODO Auto-generated method stub 		return jobBuilderFactory.get("[ANALYSIS JOB]") 				.incrementer(new RunIdIncrementer()) 				.start(analysisStep()) 				.build(); 	}  	private Step analysisStep() { 		// TODO Auto-generated method stub 		return stepBuilderFactory.get("[ANALYSIS]") 				.tasklet(new AnalysisTask()) 				.build(); 	}  } 

 

댓글