List of usage examples for org.springframework.batch.core Job getName
String getName();
From source file:com.iisigroup.cap.batch.handler.BatchHandler.java
private void jobRegistryLoad(BatchJob job) { org.springframework.core.io.Resource resource = batchSrv.getJobResource(job); ApplicationContext context = createApplicationContextFactory(CapAppContext.getApplicationContext(), resource);/*ww w.j a v a2s . c o m*/ Job j = context.getBean(job.getJobId(), Job.class); jobRegistry.unregister(j.getName()); try { jobRegistry.register(new ReferenceJobFactory(j)); } catch (DuplicateJobException e) { e.getMessage(); } }
From source file:de.codecentric.batch.web.JobOperationsController.java
private JobParameters createJobParametersWithIncrementerIfAvailable(String parameters, Job job) throws JobParametersNotFoundException { JobParameters jobParameters = jobParametersConverter .getJobParameters(PropertiesConverter.stringToProperties(parameters)); // use JobParametersIncrementer to create JobParameters if incrementer is set and only if the job is no restart if (job.getJobParametersIncrementer() != null) { JobExecution lastJobExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters); boolean restart = false; // check if job failed before if (lastJobExecution != null) { BatchStatus status = lastJobExecution.getStatus(); if (status.isUnsuccessful() && status != BatchStatus.ABANDONED) { restart = true;/* w w w .j av a2 s . c o m*/ } } // if it's not a restart, create new JobParameters with the incrementer if (!restart) { JobParameters nextParameters = getNextJobParameters(job); Map<String, JobParameter> map = new HashMap<String, JobParameter>(nextParameters.getParameters()); map.putAll(jobParameters.getParameters()); jobParameters = new JobParameters(map); } } return jobParameters; }
From source file:org.springframework.batch.core.configuration.support.DefaultJobLoader.java
@SuppressWarnings("resource") private Collection<Job> doLoad(ApplicationContextFactory factory, boolean unregister) throws DuplicateJobException { Collection<String> jobNamesBefore = jobRegistry.getJobNames(); ConfigurableApplicationContext context = factory.createApplicationContext(); Collection<String> jobNamesAfter = jobRegistry.getJobNames(); // Try to detect auto-registration (e.g. through a bean post processor) boolean autoRegistrationDetected = jobNamesAfter.size() > jobNamesBefore.size(); Collection<String> jobsRegistered = new HashSet<String>(); if (autoRegistrationDetected) { for (String name : jobNamesAfter) { if (!jobNamesBefore.contains(name)) { jobsRegistered.add(name); }// w w w .j a va 2 s . c o m } } contexts.put(factory, context); String[] names = context.getBeanNamesForType(Job.class); for (String name : names) { if (!autoRegistrationDetected) { Job job = (Job) context.getBean(name); String jobName = job.getName(); // On reload try to unregister first if (unregister) { if (logger.isDebugEnabled()) { logger.debug( "Unregistering job: " + jobName + " from context: " + context.getDisplayName()); } doUnregister(jobName); } if (logger.isDebugEnabled()) { logger.debug("Registering job: " + jobName + " from context: " + context.getDisplayName()); } doRegister(context, job); jobsRegistered.add(jobName); } } Collection<Job> result = new ArrayList<Job>(); for (String name : jobsRegistered) { try { result.add(jobRegistry.getJob(name)); } catch (NoSuchJobException e) { // should not happen; throw new IllegalStateException("Could not retrieve job that was should have been registered", e); } } contextToJobNames.put(context, jobsRegistered); return result; }
From source file:org.springframework.batch.core.configuration.support.DefaultJobLoader.java
/** * Registers the specified {@link Job} defined in the specified {@link ConfigurableApplicationContext}. * <br>// w w w. j a va2s.c o m * Makes sure to update the {@link StepRegistry} if it is available. * * @param context the context in which the job is defined * @param job the job to register * @throws DuplicateJobException if that job is already registered */ private void doRegister(ConfigurableApplicationContext context, Job job) throws DuplicateJobException { final JobFactory jobFactory = new ReferenceJobFactory(job); jobRegistry.register(jobFactory); if (stepRegistry != null) { if (!(job instanceof StepLocator)) { throw new UnsupportedOperationException( "Cannot locate steps from a Job that is not a StepLocator: job=" + job.getName() + " does not implement StepLocator"); } stepRegistry.register(job.getName(), getSteps((StepLocator) job, context)); } }
From source file:org.springframework.batch.core.launch.support.CommandLineJobRunner.java
/** * @param job the job that we need to find the next parameters for * @return the next job parameters if they can be located * @throws JobParametersNotFoundException if there is a problem *//* ww w. j av a2s .c o m*/ private JobParameters getNextJobParameters(Job job) throws JobParametersNotFoundException { String jobIdentifier = job.getName(); JobParameters jobParameters; List<JobInstance> lastInstances = jobExplorer.getJobInstances(jobIdentifier, 0, 1); JobParametersIncrementer incrementer = job.getJobParametersIncrementer(); if (incrementer == null) { throw new JobParametersNotFoundException( "No job parameters incrementer found for job=" + jobIdentifier); } if (lastInstances.isEmpty()) { jobParameters = incrementer.getNext(new JobParameters()); if (jobParameters == null) { throw new JobParametersNotFoundException( "No bootstrap parameters found from incrementer for job=" + jobIdentifier); } } else { List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0)); jobParameters = incrementer.getNext(lastExecutions.get(0).getJobParameters()); } return jobParameters; }
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./*from w w w . j a v a2s. 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; }
From source file:org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.java
private JobParameters getNextJobParameters(Job job, JobParameters additionalParameters) { String name = job.getName(); JobParameters parameters = new JobParameters(); List<JobInstance> lastInstances = this.jobExplorer.getJobInstances(name, 0, 1); JobParametersIncrementer incrementer = job.getJobParametersIncrementer(); Map<String, JobParameter> additionals = additionalParameters.getParameters(); if (lastInstances.isEmpty()) { // Start from a completely clean sheet if (incrementer != null) { parameters = incrementer.getNext(new JobParameters()); }//from ww w . j av a 2 s .co m } else { List<JobExecution> previousExecutions = this.jobExplorer.getJobExecutions(lastInstances.get(0)); JobExecution previousExecution = previousExecutions.get(0); if (previousExecution == null) { // Normally this will not happen - an instance exists with no executions if (incrementer != null) { parameters = incrementer.getNext(new JobParameters()); } } else if (isStoppedOrFailed(previousExecution) && job.isRestartable()) { // Retry a failed or stopped execution parameters = previousExecution.getJobParameters(); // Non-identifying additional parameters can be removed to a retry removeNonIdentifying(additionals); } else if (incrementer != null) { // New instance so increment the parameters if we can parameters = incrementer.getNext(previousExecution.getJobParameters()); } } return merge(parameters, additionals); }
From source file:org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.java
private void executeLocalJobs(JobParameters jobParameters) throws JobExecutionException { for (Job job : this.jobs) { if (StringUtils.hasText(this.jobNames)) { String[] jobsToRun = this.jobNames.split(","); if (!PatternMatchUtils.simpleMatch(jobsToRun, job.getName())) { logger.debug("Skipped job: " + job.getName()); continue; }/*from w ww . j a va 2s.co m*/ } execute(job, jobParameters); } }
From source file:org.springframework.yarn.batch.support.YarnJobLauncher.java
private void executeLocalJobs(JobParameters jobParameters) throws JobExecutionException { if (jobs.size() == 0) { log.info("No local jobs defined"); }// w ww . j av a2 s . co m for (Job job : jobs) { if (StringUtils.hasText(this.jobName) && jobMatches(this.jobName, job.getName())) { log.debug("Executing local job: " + job.getName()); executeJob(job, jobParameters); } else { log.debug("Skipped local job: " + job.getName()); } } }
From source file:org.springframework.yarn.batch.support.YarnJobLauncher.java
protected void executeJob(Job job, JobParameters jobParameters) throws JobExecutionException { String jobIdentifier = job.getName(); JobProperties jobProperties = yarnBatchProperties != null ? yarnBatchProperties.getJobProperties(jobIdentifier) : null;//from w w w . ja va 2s . c om boolean restart = false; // re-create by adding props from a boot JobProperties if (jobProperties != null && jobProperties.getParameters() != null) { log.info("Job parameters from boot properties, parameters" + jobProperties.getParameters()); Properties tmpProperties = new Properties(); Map<String, Object> tmpParameters = jobProperties.getParameters(); tmpProperties.putAll(tmpParameters); JobParameters tmpJobParameters = this.converter.getJobParameters(tmpProperties); Map<String, JobParameter> map1 = new HashMap<String, JobParameter>(tmpJobParameters.getParameters()); map1.putAll(jobParameters.getParameters()); jobParameters = new JobParameters(map1); log.info("Modified jobParameters=" + jobParameters); } if (jobProperties != null && jobProperties.isRestart()) { if (jobExplorer == null) { throw new JobExecutionException( "A JobExplorer must be provided for a restart or start next operation."); } JobExecution jobExecution = getLastFailedJobExecution(jobIdentifier); if (log.isDebugEnabled()) { log.info("Last failed JobExecution: " + jobExecution); } if (jobExecution == null && jobProperties.isFailRestart()) { throw new JobExecutionNotFailedException( "No failed or stopped execution found for job=" + jobIdentifier); } else { log.info("No failed or stopped execution found for job=" + jobIdentifier + ", batch properties flag for failRestart=" + jobProperties.isFailRestart() + " so we don't fail restart."); } if (jobExecution != null) { restart = true; jobParameters = jobExecution.getJobParameters(); } } if (jobProperties != null && jobProperties.isNext() && !restart) { if (jobExplorer == null) { throw new JobExecutionException( "A JobExplorer must be provided for a restart or start next operation."); } JobParameters nextParameters = getNextJobParameters(job, false); Map<String, JobParameter> map = new HashMap<String, JobParameter>(nextParameters.getParameters()); map.putAll(jobParameters.getParameters()); jobParameters = new JobParameters(map); if (log.isDebugEnabled()) { log.info("JobParameter for job=[" + job + "] next=" + nextParameters + " used=" + jobParameters); } } JobExecution execution = this.jobLauncher.run(job, jobParameters); if (this.publisher != null) { this.publisher.publishEvent(new JobExecutionEvent(this, execution)); } }