Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.chrisbeech.batch;
- import java.util.Date;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.batch.core.Job;
- import org.springframework.batch.core.JobExecution;
- import org.springframework.batch.core.JobParametersBuilder;
- import org.springframework.batch.core.JobParametersInvalidException;
- import org.springframework.batch.core.launch.JobLauncher;
- import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
- import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
- import org.springframework.batch.core.repository.JobRestartException;
- import org.springframework.beans.BeansException;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.context.ConfigurableApplicationContext;
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.PropertySource;
- @ComponentScan
- @EnableAutoConfiguration
- @PropertySource("classpath:batch.properties")
- public class Application {
- private static final Logger log = LoggerFactory.getLogger(Application.class);
- public static void main(String[] args)
- throws BeansException, JobExecutionAlreadyRunningException, JobRestartException,
- JobInstanceAlreadyCompleteException, JobParametersInvalidException, InterruptedException {
- // get reference to job launcher
- SpringApplication springApp = new SpringApplication(Application.class);
- ConfigurableApplicationContext context = springApp.run(args);
- JobLauncher jobLauncher = context.getBean(JobLauncher.class);
- // obtain reference to job
- Job personLoadJob = context.getBean("springBatchJob", Job.class);
- // add current date/time to ensure we can repeatedly run this batch without changing params
- JobParametersBuilder jParamsBuilder = new JobParametersBuilder();
- jParamsBuilder.addDate("now", new Date());
- // launch job
- JobExecution jobExecution = jobLauncher.run(personLoadJob, jParamsBuilder.toJobParameters());
- // log exit code
- log.info("Batch exited with exit code : " + jobExecution.getExitStatus().getExitCode());
- // quit
- System.exit(0);
- }
- }
Add Comment
Please, Sign In to add comment