Guest User

Spring Batch Test Application

a guest
Nov 8th, 2016
162
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.chrisbeech.batch;
  2.  
  3.  
  4.  
  5. import java.util.Date;
  6.  
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.batch.core.Job;
  10. import org.springframework.batch.core.JobExecution;
  11. import org.springframework.batch.core.JobParametersBuilder;
  12. import org.springframework.batch.core.JobParametersInvalidException;
  13. import org.springframework.batch.core.launch.JobLauncher;
  14. import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
  15. import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
  16. import org.springframework.batch.core.repository.JobRestartException;
  17. import org.springframework.beans.BeansException;
  18. import org.springframework.boot.SpringApplication;
  19. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  20. import org.springframework.context.ConfigurableApplicationContext;
  21. import org.springframework.context.annotation.ComponentScan;
  22. import org.springframework.context.annotation.PropertySource;
  23.  
  24. @ComponentScan
  25. @EnableAutoConfiguration
  26. @PropertySource("classpath:batch.properties")
  27. public class Application {
  28.    
  29.     private static final Logger log = LoggerFactory.getLogger(Application.class);
  30.    
  31.     public static void main(String[] args)
  32.             throws BeansException, JobExecutionAlreadyRunningException, JobRestartException,
  33.             JobInstanceAlreadyCompleteException, JobParametersInvalidException, InterruptedException {
  34.        
  35.         // get reference to job launcher
  36.         SpringApplication springApp = new SpringApplication(Application.class);
  37.         ConfigurableApplicationContext context = springApp.run(args);
  38.         JobLauncher jobLauncher = context.getBean(JobLauncher.class);
  39.        
  40.         // obtain reference to job
  41.         Job personLoadJob = context.getBean("springBatchJob", Job.class);
  42.            
  43.         // add current date/time to ensure we can repeatedly run this batch without changing params
  44.         JobParametersBuilder jParamsBuilder = new JobParametersBuilder();
  45.         jParamsBuilder.addDate("now", new Date());
  46.        
  47.         // launch job
  48.         JobExecution jobExecution = jobLauncher.run(personLoadJob, jParamsBuilder.toJobParameters());
  49.        
  50.         // log exit code
  51.         log.info("Batch exited with exit code : " + jobExecution.getExitStatus().getExitCode());
  52.        
  53.         // quit
  54.         System.exit(0);
  55.     }
  56. }
RAW Paste Data Copied