List of usage examples for org.springframework.batch.core BatchStatus isRunning
public boolean isRunning()
From source file:com.apress.prospringintegration.springbatch.integration.Main.java
public static void main(String[] args) throws Throwable { ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext( "integration.xml"); classPathXmlApplicationContext.start(); JobLauncher jobLauncher = (JobLauncher) classPathXmlApplicationContext.getBean("jobLauncher"); Job job = (Job) classPathXmlApplicationContext.getBean("importData"); JobParametersBuilder jobParametersBuilder = new JobParametersBuilder(); jobParametersBuilder.addDate("date", new Date()); jobParametersBuilder.addString("input.file", "registrations"); JobParameters jobParameters = jobParametersBuilder.toJobParameters(); JobExecution jobExecution = jobLauncher.run(job, jobParameters); BatchStatus batchStatus = jobExecution.getStatus(); while (batchStatus.isRunning()) { System.out.println("Still running..."); Thread.sleep(1000);// w w w . j a v a 2 s .co m } System.out.println("Exit status: " + jobExecution.getExitStatus().getExitCode()); JobInstance jobInstance = jobExecution.getJobInstance(); System.out.println("job instance Id: " + jobInstance.getId()); }
From source file:org.cloudfoundry.workers.stocks.batch.NightlyStockSymbolRecorder.java
@Scheduled(fixedRate = 10 * 1000) public void runNightlyStockPriceRecorder() throws Throwable { JobParameters params = new JobParametersBuilder().addDate("date", new Date()).toJobParameters(); JobExecution jobExecution = jobLauncher.run(job, params); BatchStatus batchStatus = jobExecution.getStatus(); while (batchStatus.isRunning()) { logger.info("Still running..."); Thread.sleep(1000);/*from w w w.j a va2 s. c om*/ } logger.info(String.format("Exit status: %s", jobExecution.getExitStatus().getExitCode())); JobInstance jobInstance = jobExecution.getJobInstance(); logger.info(String.format("job instance Id: %d", jobInstance.getId())); }
From source file:org.cloudfoundry.workers.twitter.batch.NightlyTweetRecorder.java
@Scheduled(fixedRate = 10 * 1000) public void runNightlyTweetRecorder() throws Throwable { logger.info("Running nightly tweet recorder"); JobParameters params = new JobParametersBuilder().addDate("date", new Date()).toJobParameters(); JobExecution jobExecution = jobLauncher.run(job, params); BatchStatus batchStatus = jobExecution.getStatus(); while (batchStatus.isRunning()) { logger.info("Still running..."); Thread.sleep(1000);/*from ww w . j a v a 2 s. c om*/ } logger.info(String.format("Exit status: %s", jobExecution.getExitStatus().getExitCode())); JobInstance jobInstance = jobExecution.getJobInstance(); logger.info(String.format("job instance Id: %d", jobInstance.getId())); }
From source file:org.springframework.batch.core.launch.support.SimpleJobLauncher.java
/** * Run the provided job with the given {@link JobParameters}. The * {@link JobParameters} will be used to determine if this is an execution * of an existing job instance, or if a new one should be created. * * @param job the job to be run.//w w w .j a va 2 s . c o m * @param jobParameters the {@link JobParameters} for this particular * execution. * @return JobExecutionAlreadyRunningException if the JobInstance already * exists and has an execution already running. * @throws JobRestartException if the execution would be a re-start, but a * re-start is either not allowed or not needed. * @throws JobInstanceAlreadyCompleteException if this instance has already * completed successfully * @throws JobParametersInvalidException */ @Override public JobExecution run(final Job job, final JobParameters jobParameters) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException { Assert.notNull(job, "The Job must not be null."); Assert.notNull(jobParameters, "The JobParameters must not be null."); final JobExecution jobExecution; JobExecution lastExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters); if (lastExecution != null) { if (!job.isRestartable()) { throw new JobRestartException("JobInstance already exists and is not restartable"); } /* * validate here if it has stepExecutions that are UNKNOWN, STARTING, STARTED and STOPPING * retrieve the previous execution and check */ for (StepExecution execution : lastExecution.getStepExecutions()) { BatchStatus status = execution.getStatus(); if (status.isRunning() || status == BatchStatus.STOPPING) { throw new JobExecutionAlreadyRunningException( "A job execution for this job is already running: " + lastExecution); } else if (status == BatchStatus.UNKNOWN) { throw new JobRestartException("Cannot restart step [" + execution.getStepName() + "] from UNKNOWN status. " + "The last execution ended with a failure that could not be rolled back, " + "so it may be dangerous to proceed. Manual intervention is probably necessary."); } } } // Check the validity of the parameters before doing creating anything // in the repository... job.getJobParametersValidator().validate(jobParameters); /* * There is a very small probability that a non-restartable job can be * restarted, but only if another process or thread manages to launch * <i>and</i> fail a job execution for this instance between the last * assertion and the next method returning successfully. */ jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters); try { taskExecutor.execute(new Runnable() { @Override public void run() { try { logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters + "]"); job.execute(jobExecution); logger.info("Job: [" + job + "] completed with the following parameters: [" + jobParameters + "] and the following status: [" + jobExecution.getStatus() + "]"); } catch (Throwable t) { logger.info("Job: [" + job + "] failed unexpectedly and fatally with the following parameters: [" + jobParameters + "]", t); rethrow(t); } } private void rethrow(Throwable t) { if (t instanceof RuntimeException) { throw (RuntimeException) t; } else if (t instanceof Error) { throw (Error) t; } throw new IllegalStateException(t); } }); } catch (TaskRejectedException e) { jobExecution.upgradeStatus(BatchStatus.FAILED); if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) { jobExecution.setExitStatus(ExitStatus.FAILED.addExitDescription(e)); } jobRepository.update(jobExecution); } return jobExecution; }