@Override public void run() { JsrJobContextFactoryBean factoryBean = null; try { factoryBean = (JsrJobContextFactoryBean) batchContext.getBean("&" + JSR_JOB_CONTEXT_BEAN_NAME); factoryBean.setJobExecution(jobExecution); final Job job = batchContext.getBean(Job.class); if(!job.isRestartable()) { throw new JobRestartException("Job " + jobName + " is not restartable"); } semaphore.release(); // Initialization of the JobExecution for job level dependencies jobRegistry.register(job, jobExecution); job.execute(jobExecution); jobRegistry.remove(jobExecution); } catch (Exception e) { exceptionHolder.add(e); } finally { if(factoryBean != null) { factoryBean.close(); } batchContext.close(); if(semaphore.availablePermits() == 0) { semaphore.release(); } } } });
@Override public final JobRestartException restartRunningExecution(final long executionId, final String jobName, final BatchStatus previousStatus, final String restartMode) { final JobRestartException result = new JobRestartException(String.format(getLoggingLocale(), restartRunningExecution$str(), executionId, jobName, previousStatus, restartMode)); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String invalidRestartMode = "JBERET000648: Restarting job execution %s, job name %s, batch status %s, but restart mode %s is invalid. Valid values are %s";
@Override public final JobRestartException jobRestartException(final long jobExecutionId, final BatchStatus previousStatus) { final JobRestartException result = new JobRestartException(String.format(getLoggingLocale(), jobRestartException$str(), jobExecutionId, previousStatus)); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String jobExecutionNotMostRecentException = "JBERET000611: Job execution %s is not the most recent execution of job instance %s.";
throw new JobRestartException("JobExecution ID: " + jobExecution.getId() + " is abandoned and attempted to be restarted."); batchContext.refresh(); } catch (BeanCreationException e) { throw new JobRestartException(e); jobExecution = jobRepository.createJobExecution(previousJobExecution.getJobInstance(), jobParameters, previousJobExecution.getJobConfigurationName()); } catch (Exception e) { throw new JobRestartException(e); if(exceptionHolder.size() > 0) { semaphore.release(); throw new JobRestartException(exceptionHolder.get(0)); throw new JobRestartException(e);
@Override public final JobRestartException invalidRestartMode(final long executionId, final String jobName, final BatchStatus previousStatus, final String restartMode, final java.util.List<String> validRestartMode) { final JobRestartException result = new JobRestartException(String.format(getLoggingLocale(), invalidRestartMode$str(), executionId, jobName, previousStatus, restartMode, validRestartMode)); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String nullVar = "JBERET000649: %s cannot be null";
@Override public long restart(long oldExecutionId, Properties restartParameters) throws JobExecutionAlreadyCompleteException, NoSuchJobExecutionException, JobExecutionNotMostRecentException, JobRestartException, JobSecurityException { /* * The whole point of this method is to have JobRestartException serve as a blanket exception for anything other * than the rest of the more specific exceptions declared on the throws clause. So we won't log but just rethrow. */ try { return restartInternal(oldExecutionId, restartParameters); } catch (JobExecutionAlreadyCompleteException e) { throw e; } catch (NoSuchJobExecutionException e) { throw e; } catch (JobExecutionNotMostRecentException e) { throw e; } catch (JobSecurityException e) { throw e; } catch (Exception e) { throw new JobRestartException(e); } }
@Override public final JobRestartException unrestartableJob(final String jobName, final long jobExecutionId) { final JobRestartException result = new JobRestartException(String.format(getLoggingLocale(), unrestartableJob$str(), jobName, jobExecutionId)); final StackTraceElement[] st = result.getStackTrace(); result.setStackTrace(Arrays.copyOfRange(st, 1, st.length)); return result; } private static final String restartRunningExecution = "JBERET000647: Restarting job execution %s, job name %s, batch status %s, restart mode %s, but it seems the original execution is still alive.";
@Override public long restart(long oldExecutionId, Properties restartParameters) throws JobExecutionAlreadyCompleteException, NoSuchJobExecutionException, JobExecutionNotMostRecentException, JobRestartException, JobSecurityException { /* * The whole point of this method is to have JobRestartException serve as a blanket exception for anything other * than the rest of the more specific exceptions declared on the throws clause. So we won't log but just rethrow. */ long retVal = 0L; try { retVal = restartInternal(oldExecutionId, restartParameters); } catch (JobExecutionAlreadyCompleteException e) { throw e; } catch (NoSuchJobExecutionException e) { throw e; } catch (JobExecutionNotMostRecentException e) { throw e; } catch (JobSecurityException e) { throw e; } catch (Exception e) { throw new JobRestartException(e); } return retVal; }
private static void validateRestartableFalseJobsDoNotRestart(JSLJob jobModel) throws JobRestartException { if (jobModel.getRestartable() != null && jobModel.getRestartable().equalsIgnoreCase("false")) { throw new JobRestartException("Job Restartable attribute is false, Job cannot be restarted."); } }
private static void validateRestartableFalseJobsDoNotRestart(final JSLJob jobModel) throws JobRestartException { if (jobModel.getRestartable() != null && jobModel.getRestartable().equalsIgnoreCase("false")) { throw new JobRestartException("Job Restartable attribute is false, Job cannot be restarted."); } }
@Override public long restart(long oldExecutionId, Properties restartParameters) throws JobExecutionAlreadyCompleteException, NoSuchJobExecutionException, JobExecutionNotMostRecentException, JobRestartException, JobSecurityException { /* * The whole point of this method is to have JobRestartException serve as a blanket exception for anything other * than the rest of the more specific exceptions declared on the throws clause. So we won't log but just rethrow. */ long retVal = 0L; try { retVal = restartInternal(oldExecutionId, restartParameters); } catch (JobExecutionAlreadyCompleteException e) { throw e; } catch (NoSuchJobExecutionException e) { throw e; } catch (JobExecutionNotMostRecentException e) { throw e; } catch (JobSecurityException e) { throw e; } catch (Exception e) { throw new JobRestartException(e); } return retVal; }
private static void validateRestartableFalseJobsDoNotRestart(JSLJob jobModel) throws JobRestartException { if (jobModel.getRestartable() != null && jobModel.getRestartable().equalsIgnoreCase("false")) { throw new JobRestartException("Job Restartable attribute is false, Job cannot be restarted."); } }
private void validateJobInstanceFailedOrStopped() throws JobRestartException { InstanceState instanceState = jobInstance.getInstanceState(); if (!checkIfStateMatchesFromList(instanceState, RESTARTABLE_INSTANCE_STATES)) { String msg = "The job instance " + jobInstance.getInstanceId() + " cannot be restarted because it is still in a non-final state."; logger.fine(msg); throw new JobRestartException(msg); } }
@Override public JobInstance call() { JobInstanceEntity instance = entityMgr.find(JobInstanceEntity.class, jobInstanceId); if (instance == null) { throw new NoSuchJobInstanceException("No job instance found for id = " + jobInstanceId); } try { verifyStateTransitionIsValid(instance, InstanceState.SUBMITTED); verifyStatusTransitionIsValid(instance, BatchStatus.STARTING); } catch (BatchIllegalJobStatusTransitionException e) { throw new PersistenceException(e); } Query jpaQuery = entityMgr.createQuery(FINAL_UPDATE); jpaQuery.setParameter("instanceState", InstanceState.SUBMITTED); jpaQuery.setParameter("instanceId", jobInstanceId); if (instanceVersion >= 2) jpaQuery.setParameter("lastUpdatedTime", lastUpdated); jpaQuery.setParameter("batchStatus", BatchStatus.STARTING); int count = jpaQuery.executeUpdate(); if (count > 0) { // Need to refresh to pick up changes made to the database entityMgr.refresh(instance); } else { String msg = "The job instance " + jobInstanceId + " cannot be restarted because it is still in a non-final state."; throw new JobRestartException(msg); } return instance; } }.runInNewOrExistingGlobalTran();
private void validateRestartableFalseJobsDoNotRestart() throws JobRestartException { if (!StringUtils.isEmpty(jobInstance.getJobXml())) { JSLJob jobModel = ModelResolverFactory.createJobResolver().resolveModel(new StreamSource(new StringReader(jobInstance.getJobXml()))); PropertyResolver<JSLJob> propResolver = PropertyResolverFactory.createJSLJobPropertyResolver(false); propResolver.substituteProperties(jobModel, restartJobParameters); if (jobModel.getRestartable() != null && jobModel.getRestartable().equalsIgnoreCase("false")) { throw new JobRestartException("Job Restartable attribute is false, Job cannot be restarted."); } } }
private void validateJobExecutionFailedOrStoppedOrStarting() throws JobRestartException { BatchStatus executionStatus = getPersistenceManagerService().getJobExecution(previousExecutionId).getBatchStatus(); if (!checkIfStatusMatchesFromList(executionStatus, RESTARTABLE_EXECUTION_STATES)) { // String msg = getFormattedMessage("job.restart.denied", new Object[]{jobInstance.getInstanceId()}, "The job instance " + jobInstance.getInstanceId() + " cannot be restarted because the most recent execution is still in a non-final state."); String msg = "The job instance " + jobInstance.getInstanceId() + " cannot be restarted because the most recent execution is still in a non-final state."; logger.fine(msg); throw new JobRestartException(msg); } }
private static void validateJobInstanceNotCompleteOrAbandonded(JobStatus jobStatus) throws JobRestartException, JobExecutionAlreadyCompleteException { if (jobStatus.getBatchStatus() == null) { String msg = "On restart, we didn't find an earlier batch status."; logger.warning(msg); throw new IllegalStateException(msg); } if (jobStatus.getBatchStatus().equals(BatchStatus.COMPLETED)) { String msg = "Already completed job instance = " + jobStatus.getJobInstanceId(); logger.fine(msg); throw new JobExecutionAlreadyCompleteException(msg); } else if (jobStatus.getBatchStatus().equals(BatchStatus.ABANDONED)) { String msg = "Abandoned job instance = " + jobStatus.getJobInstanceId(); logger.warning(msg); throw new JobRestartException(msg); } }
private static void validateJobInstanceNotCompleteOrAbandonded(JobStatus jobStatus) throws JobRestartException, JobExecutionAlreadyCompleteException { if (jobStatus.getBatchStatus() == null) { String msg = "On restart, we didn't find an earlier batch status."; logger.warning(msg); throw new IllegalStateException(msg); } if (jobStatus.getBatchStatus().equals(BatchStatus.COMPLETED)) { String msg = "Already completed job instance = " + jobStatus.getJobInstanceId(); logger.fine(msg); throw new JobExecutionAlreadyCompleteException(msg); } else if (jobStatus.getBatchStatus().equals(BatchStatus.ABANDONED)) { String msg = "Abandoned job instance = " + jobStatus.getJobInstanceId(); logger.warning(msg); throw new JobRestartException(msg); } }
private static void validateJobInstanceNotCompleteOrAbandonded(final JobStatus jobStatus) throws JobRestartException, JobExecutionAlreadyCompleteException { if (jobStatus.getBatchStatus() == null) { throw new IllegalStateException("On restart, we didn't find an earlier batch status."); } if (jobStatus.getBatchStatus().equals(BatchStatus.COMPLETED)) { throw new JobExecutionAlreadyCompleteException("Already completed job instance = " + jobStatus.getJobInstanceId()); } else if (jobStatus.getBatchStatus().equals(BatchStatus.ABANDONED)) { throw new JobRestartException("Abandoned job instance = " + jobStatus.getJobInstanceId()); } }
private void validateJobNotCompleteOrAbandonded() throws JobRestartException, JobExecutionAlreadyCompleteException { BatchStatus instanceBatchStatus = jobInstance.getBatchStatus(); BatchStatus executionStatus = getPersistenceManagerService().getJobExecution(previousExecutionId).getBatchStatus(); if (instanceBatchStatus == null) { String msg = "On restart, we didn't find an earlier instance batch status."; logger.fine(msg); throw new IllegalStateException(msg); } if (instanceBatchStatus.equals(BatchStatus.COMPLETED) || executionStatus.equals(BatchStatus.COMPLETED)) { String msg = "Job already completed. (Instance, most recent execution) = (" + jobInstance.getInstanceId() + "," + previousExecutionId + "), instanceStatus = " + instanceBatchStatus + ", executionStatus = " + executionStatus; logger.fine(msg); throw new JobExecutionAlreadyCompleteException(msg); } else if (instanceBatchStatus.equals(BatchStatus.ABANDONED) || executionStatus.equals(BatchStatus.ABANDONED)) { String msg = "Job previously abandoned. (Instance, most recent execution) = (" + jobInstance.getInstanceId() + "," + previousExecutionId + "), instanceStatus = " + instanceBatchStatus + ", executionStatus = " + executionStatus; logger.fine(msg); throw new JobRestartException(msg); } }