updateStatus(execution, BatchStatus.STARTED); doExecute(execution); if (logger.isDebugEnabled()) { logger.debug("Job execution complete: " + execution); logger.debug("Full exception", e); execution.setExitStatus(getDefaultExitStatusForFailure(e, execution)); execution.setStatus(BatchStatus.max(BatchStatus.STOPPED, e.getStatus())); execution.addFailureException(e); } catch (Throwable t) { logger.error("Encountered fatal error executing job", t); execution.setExitStatus(getDefaultExitStatusForFailure(t, execution)); execution.setStatus(BatchStatus.FAILED); execution.addFailureException(t);
protected void enhance(Job target) { if (target instanceof AbstractJob) { AbstractJob job = (AbstractJob) target; job.setJobRepository(properties.getJobRepository()); JobParametersIncrementer jobParametersIncrementer = properties.getJobParametersIncrementer(); if (jobParametersIncrementer != null) { job.setJobParametersIncrementer(jobParametersIncrementer); } JobParametersValidator jobParametersValidator = properties.getJobParametersValidator(); if (jobParametersValidator != null) { job.setJobParametersValidator(jobParametersValidator); } Boolean restartable = properties.getRestartable(); if (restartable != null) { job.setRestartable(restartable); } List<JobExecutionListener> listeners = properties.getJobExecutionListeners(); if (!listeners.isEmpty()) { job.setJobExecutionListeners(listeners.toArray(new JobExecutionListener[0])); } } }
/** * Launch just the specified step in the job. An IllegalStateException is * thrown if there is no Step with the given name. * * @param stepName The name of the step to launch * @param jobParameters The JobParameters to use during the launch * @param jobExecutionContext An ExecutionContext whose values will be * loaded into the Job ExecutionContext prior to launching the step. * @return JobExecution */ public JobExecution launchStep(String stepName, JobParameters jobParameters, ExecutionContext jobExecutionContext) { Step step = this.job.getStep(stepName); if (step == null) { step = this.job.getStep(this.job.getName() + "." + stepName); } if (step == null) { throw new IllegalStateException("No Step found with name: [" + stepName + "]"); } return getStepRunner().launchStep(step, jobParameters, jobExecutionContext); } }
/** * Test method for * {@link org.springframework.batch.core.job.AbstractJob#setRestartable(boolean)} * . */ @Test public void testSetRestartable() { assertTrue(job.isRestartable()); job.setRestartable(false); assertFalse(job.isRestartable()); }
public void addListenerToJob(AbstractJob job) { if (addProtocolListener) { job.registerJobExecutionListener(protocolListener); } job.registerJobExecutionListener(runningExecutionTrackerListener); if (addLoggingListener) { job.registerJobExecutionListener(loggingListener); job.registerJobExecutionListener(loggingAfterJobListener); for (String stepName : job.getStepNames()) { AbstractStep step = (AbstractStep) job.getStep(stepName); step.registerStepExecutionListener(loggingListener); } } if (listenerProviders != null) { for (ListenerProvider listenerProvider : listenerProviders) { for (JobExecutionListener jobExecutionListener : listenerProvider.jobExecutionListeners()) { job.registerJobExecutionListener(jobExecutionListener); } for (StepExecutionListener stepExecutionListener : listenerProvider.stepExecutionListeners()) { for (String stepName : job.getStepNames()) { AbstractStep step = (AbstractStep) job.getStep(stepName); step.registerStepExecutionListener(stepExecutionListener); } } } } }
factory.afterPropertiesSet(); JobRepository repository = factory.getObject(); job.setJobRepository(repository); job.setRestartable(true); job.handleStep(new StubStep(), execution);
@Component public class MailListenerBeanPostProcessor implements BeanPostProcessor { @Autowired private MailListener listener; @Override public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { return bean; } @Override public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { if (bean instanceof AbstractJob) { final AbstractJob job = (AbstractJob) bean; job.registerJobExecutionListener(listener); } return bean; } }
@Test public void testSetValidator() throws Exception { job.setJobParametersValidator(new DefaultJobParametersValidator() { @Override public void validate(JobParameters parameters) throws JobParametersInvalidException { throw new JobParametersInvalidException("FOO"); } }); JobExecution execution = jobRepository.createJobExecution("job", new JobParameters()); job.execute(execution); assertEquals(BatchStatus.FAILED, execution.getStatus()); assertEquals("FOO", execution.getFailureExceptions().get(0).getMessage()); String description = execution.getExitStatus().getExitDescription(); assertTrue("Wrong description: "+description, description.contains("FOO")); }
@Test public void testAfterPropertiesSet() throws Exception { job.setJobRepository(null); try { job.afterPropertiesSet(); fail(); } catch (IllegalArgumentException e) { assertTrue(e.getMessage().contains("JobRepository")); } }
/** * Launch just the specified step in the job. An IllegalStateException is * thrown if there is no Step with the given name. * * @param stepName * @return JobExecution */ public JobExecution launchStep(String stepName) { Step step = this.job.getStep(stepName); if (step == null) { throw new IllegalStateException("No Step found with name: [" + stepName + "]"); } return getStepRunner().launchStep(step); }
public void addListenerToJob(AbstractJob job) { if (addProtocolListener) { job.registerJobExecutionListener(protocolListener); } job.registerJobExecutionListener(runningExecutionTrackerListener); if (addLoggingListener) { job.registerJobExecutionListener(loggingListener); job.registerJobExecutionListener(loggingAfterJobListener); for (String stepName : job.getStepNames()) { AbstractStep step = (AbstractStep) job.getStep(stepName); step.registerStepExecutionListener(loggingListener); } } if (listenerProviders != null) { for (ListenerProvider listenerProvider : listenerProviders) { for (JobExecutionListener jobExecutionListener : listenerProvider.jobExecutionListeners()) { job.registerJobExecutionListener(jobExecutionListener); } for (StepExecutionListener stepExecutionListener : listenerProvider.stepExecutionListeners()) { for (String stepName : job.getStepNames()) { AbstractStep step = (AbstractStep) job.getStep(stepName); step.registerStepExecutionListener(stepExecutionListener); } } } } }
private void registerJobExecutionEventListener(Object bean) { if (bean instanceof AbstractJob && this.applicationContext.containsBean(BatchEventAutoConfiguration.JOB_EXECUTION_EVENTS_LISTENER)) { JobExecutionListener jobExecutionEventsListener = (JobExecutionListener) this.applicationContext.getBean( BatchEventAutoConfiguration.JOB_EXECUTION_EVENTS_LISTENER); AbstractJob job = (AbstractJob) bean; job.registerJobExecutionListener( jobExecutionEventsListener); } }
/** * Launch just the specified step in the job. * * @param stepName * @param jobParameters */ public JobExecution launchStep(String stepName, JobParameters jobParameters) { return getStepRunner().launchStep(this.job.getStep(stepName), jobParameters); } }
protected void enhance(Job target) { if (target instanceof AbstractJob) { AbstractJob job = (AbstractJob) target; job.setJobRepository(properties.getJobRepository()); JobParametersIncrementer jobParametersIncrementer = properties.getJobParametersIncrementer(); if (jobParametersIncrementer != null) { job.setJobParametersIncrementer(jobParametersIncrementer); } JobParametersValidator jobParametersValidator = properties.getJobParametersValidator(); if (jobParametersValidator != null) { job.setJobParametersValidator(jobParametersValidator); } Boolean restartable = properties.getRestartable(); if (restartable != null) { job.setRestartable(restartable); } List<JobExecutionListener> listeners = properties.getJobExecutionListeners(); if (!listeners.isEmpty()) { job.setJobExecutionListeners(listeners.toArray(new JobExecutionListener[0])); } } }
updateStatus(execution, BatchStatus.STARTED); doExecute(execution); if (logger.isDebugEnabled()) { logger.debug("Job execution complete: " + execution); logger.debug("Full exception", e); execution.setExitStatus(getDefaultExitStatusForFailure(e, execution)); execution.setStatus(BatchStatus.max(BatchStatus.STOPPED, e.getStatus())); execution.addFailureException(e); } catch (Throwable t) { logger.error("Encountered fatal error executing job", t); execution.setExitStatus(getDefaultExitStatusForFailure(t, execution)); execution.setStatus(BatchStatus.FAILED); execution.addFailureException(t);
protected void injectStatusListener(final Job job, final ApplicationContext applicationContext) { if (job instanceof AbstractJob) { AbstractJob abstractJob = (AbstractJob) job; JobExecutionListener listener = new ProcessJobExecutionListener(this, applicationContext); abstractJob.registerJobExecutionListener(listener); abstractJob.registerJobExecutionListener(new TriggerStateHolderAwareJobExecutionListener(this.stateHolder, applicationContext)); abstractJob.registerJobExecutionListener(new ProblemHandlerAwareJobExecutionListener(applicationContext)); } }
protected void enhance(Job target) { if (target instanceof AbstractJob) { AbstractJob job = (AbstractJob) target; job.setJobRepository(properties.getJobRepository()); JobParametersIncrementer jobParametersIncrementer = properties.getJobParametersIncrementer(); if (jobParametersIncrementer != null) { job.setJobParametersIncrementer(jobParametersIncrementer); } JobParametersValidator jobParametersValidator = properties.getJobParametersValidator(); if (jobParametersValidator != null) { job.setJobParametersValidator(jobParametersValidator); } Boolean restartable = properties.getRestartable(); if (restartable != null) { job.setRestartable(restartable); } List<JobExecutionListener> listeners = properties.getJobExecutionListeners(); if (!listeners.isEmpty()) { job.setJobExecutionListeners(listeners.toArray(new JobExecutionListener[0])); } } }
updateStatus(execution, BatchStatus.STARTED); doExecute(execution); logger.debug("Job execution complete: " + execution); execution.setExitStatus(getDefaultExitStatusForFailure(e)); execution.setStatus(BatchStatus.STOPPED); execution.addFailureException(e); execution.setExitStatus(getDefaultExitStatusForFailure(t)); execution.setStatus(BatchStatus.FAILED); execution.addFailureException(t);
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if(jobNames.size() > 0 && !jobNames.contains(beanName)) { return bean; } int length = this.applicationContext .getBeanNamesForType(TaskBatchExecutionListener.class).length; if(bean instanceof AbstractJob) { if(length != 1) { throw new IllegalStateException("The application context is required to " + "have exactly 1 instance of the TaskBatchExecutionListener but has " + length); } ((AbstractJob) bean).registerJobExecutionListener( this.applicationContext.getBean(TaskBatchExecutionListener.class)); } return bean; }
protected void enhance(Job target) { if (target instanceof AbstractJob) { AbstractJob job = (AbstractJob) target; job.setJobRepository(properties.getJobRepository()); JobParametersIncrementer jobParametersIncrementer = properties.getJobParametersIncrementer(); if (jobParametersIncrementer != null) { job.setJobParametersIncrementer(jobParametersIncrementer); } JobParametersValidator jobParametersValidator = properties.getJobParametersValidator(); if (jobParametersValidator != null) { job.setJobParametersValidator(jobParametersValidator); } Boolean restartable = properties.getRestartable(); if (restartable != null) { job.setRestartable(restartable); } List<JobExecutionListener> listeners = properties.getJobExecutionListeners(); if (!listeners.isEmpty()) { job.setJobExecutionListeners(listeners.toArray(new JobExecutionListener[0])); } } }