@Test public void testJobStopRoseyScenario() throws Exception { jsrJobOperator = BatchRuntime.getJobOperator(); long executionId = jsrJobOperator.start("longRunningJob", null); // Give the job a chance to get started Thread.sleep(1000L); jsrJobOperator.stop(executionId); // Give the job the chance to finish stopping Thread.sleep(1000L); assertEquals(BatchStatus.STOPPED, jsrJobOperator.getJobExecution(executionId).getBatchStatus()); }
/** * Restarts a job and waits for it's status to be any of {@link BatchStatus#STOPPED}, * {@link BatchStatus#COMPLETED}, or {@link BatchStatus#FAILED}. If the job does not * reach one of those statuses within the given timeout, a {@link java.util.concurrent.TimeoutException} is * thrown. * * @param executionId the id of the job execution to restart. * @param properties job parameters to be associated with the job. * @param timeout maximum amount of time to wait in milliseconds. * @return the {@link JobExecution} for the final state of the job * @throws java.util.concurrent.TimeoutException if the timeout occurs */ public static JobExecution restartJob(long executionId, Properties properties, long timeout) throws TimeoutException { long restartId = operator.restart(executionId, properties); JobExecution execution = operator.getJobExecution(restartId); Date curDate = new Date(); BatchStatus curBatchStatus = execution.getBatchStatus(); while(true) { if(curBatchStatus == BatchStatus.STOPPED || curBatchStatus == BatchStatus.COMPLETED || curBatchStatus == BatchStatus.FAILED) { break; } if(new Date().getTime() - curDate.getTime() > timeout) { throw new TimeoutException("Job processing did not complete in time"); } execution = operator.getJobExecution(restartId); curBatchStatus = execution.getBatchStatus(); } return execution; }
@Test(expected=NoSuchJobInstanceException.class) public void testGetJobExecutionsNullJobInstance() { jsrJobOperator.getJobExecutions(null); }
/** * Executes a job and waits for it's status to be any of {@link BatchStatus#STOPPED}, * {@link BatchStatus#COMPLETED}, or {@link BatchStatus#FAILED}. If the job does not * reach one of those statuses within the given timeout, a {@link java.util.concurrent.TimeoutException} is * thrown. * * @param jobName the name of the job. * @param properties job parameters to be associated with the job. * @param timeout maximum amount of time to wait in milliseconds. * @return the {@link JobExecution} for the final state of the job * @throws java.util.concurrent.TimeoutException if the timeout occurs */ public static JobExecution runJob(String jobName, Properties properties, long timeout) throws TimeoutException{ long executionId = operator.start(jobName, properties); JobExecution execution = operator.getJobExecution(executionId); Date curDate = new Date(); BatchStatus curBatchStatus = execution.getBatchStatus(); while(true) { if(curBatchStatus == BatchStatus.STOPPED || curBatchStatus == BatchStatus.COMPLETED || curBatchStatus == BatchStatus.FAILED) { break; } if(new Date().getTime() - curDate.getTime() > timeout) { throw new TimeoutException("Job processing did not complete in time"); } execution = operator.getJobExecution(executionId); curBatchStatus = execution.getBatchStatus(); } return execution; }
public void run() { executedBatchs.add(BatchRuntime.getJobOperator().start("myJob", new Properties())); afterRun(); }
@Override public boolean skipRun(LastExecution lastExecutionInfo, Date scheduledRunTime) { List<Long> executedBatchs = MyJob.executedBatchs; for (Long executedBatch : executedBatchs) { if (!BatchRuntime.getJobOperator().getJobExecution(executedBatch).getBatchStatus().equals( COMPLETED)) { return true; } } return false; }
@POST @Path("execution/restart/{id}") @Produces(MediaType.TEXT_PLAIN) public long restart(final @PathParam("id") long executionId, final RestProperties restartParameters) { return operator.restart(executionId, RestProperties.unwrap(restartParameters)); }
@Test(expected=NoSuchJobException.class) public void testGetJobInstanceCountZeroInstancesReturned() throws Exception { when(jobExplorer.getJobInstanceCount("myJob")).thenReturn(0); jsrJobOperator.getJobInstanceCount("myJob"); }
@Test public void testGetStepExecutionsNoStepExecutions() { JobExecution jobExecution = new JobExecution(5L); when(jobExplorer.getJobExecution(5L)).thenReturn(jobExecution); List<javax.batch.runtime.StepExecution> results = jsrJobOperator.getStepExecutions(5L); assertEquals(0, results.size()); }
@Test(expected=NoSuchJobException.class) public void testGetJobInstancesNullInstancesReturned() { jsrJobOperator.getJobInstances("myJob", 0, 3); }
@Test(expected = JobRestartException.class) public void testRestartAbandoned() throws Exception { jsrJobOperator = BatchRuntime.getJobOperator(); javax.batch.runtime.JobExecution execution = runJob("jsrJobOperatorTestRestartAbandonJob", null, TIMEOUT); assertEquals(BatchStatus.FAILED, execution.getBatchStatus()); jsrJobOperator.abandon(execution.getExecutionId()); jsrJobOperator.restart(execution.getExecutionId(), null); }
@Test(expected=NoSuchJobExecutionException.class) public void testAbandonNoSuchJob() throws Exception { jsrJobOperator.abandon(5L); }
@Override public void stop(final long executionId) { operator.stop(executionId); }
@Override public JobExecution getJobExecution(final long executionId) throws NoSuchJobExecutionException, JobSecurityException { checkState(); final ClassLoader current = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged(); try { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader); final JobInstance instance = delegate.getJobInstance(executionId); validateJob(instance.getJobName()); return delegate.getJobExecution(executionId); } finally { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(current); } }
@Test(expected=NoSuchJobExecutionException.class) public void testGetJobInstanceNoExecution() { JobInstance instance = new JobInstance(1L, "my job"); JobExecution execution = new JobExecution(5L); execution.setJobInstance(instance); jsrJobOperator.getJobInstance(5L); }
@Override public long restart(final long executionId, final Properties restartParameters) throws JobExecutionAlreadyCompleteException, NoSuchJobExecutionException, JobExecutionNotMostRecentException, JobRestartException, JobSecurityException { checkState(); final ClassLoader current = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged(); try { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader); final JobInstance instance = delegate.getJobInstance(executionId); validateJob(instance.getJobName()); return delegate.restart(executionId, restartParameters); } finally { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(current); } }
// // Assumes JobContext injected into 'jobCtx' field // private boolean isRestart() { JobOperator jo = BatchRuntime.getJobOperator(); JobInstance jobInstance = jo.getJobInstance(jobCtx.getExecutionId()); int numExecutions = jo.getJobExecutions(jobInstance).size(); return numExecutions > 1; }
@Override public void stop(final long executionId) throws NoSuchJobExecutionException, JobExecutionNotRunningException, JobSecurityException { checkState(); final ClassLoader current = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged(); try { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader); final JobInstance instance = delegate.getJobInstance(executionId); validateJob(instance.getJobName()); delegate.stop(executionId); } finally { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(current); } }
@Override public void abandon(final long executionId) throws NoSuchJobExecutionException, JobExecutionIsRunningException, JobSecurityException { checkState(); final ClassLoader current = WildFlySecurityManager.getCurrentContextClassLoaderPrivileged(); try { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(classLoader); final JobInstance instance = delegate.getJobInstance(executionId); validateJob(instance.getJobName()); delegate.abandon(executionId); } finally { WildFlySecurityManager.setCurrentContextClassLoaderPrivileged(current); } }
@Test public void testRunningJobWithManuallyWiredJsrJobOperator() throws Exception { Date startTime = new Date(); long jobExecutionId = jobOperator.start("jsrJobOperatorTestJob", new Properties()); JobExecution jobExecution = jobOperator.getJobExecution(jobExecutionId); long timeout = startTime.getTime() + 10000; while(!jobExecution.getBatchStatus().equals(BatchStatus.COMPLETED)) { Thread.sleep(500); jobExecution = jobOperator.getJobExecution(jobExecutionId); if(new Date().getTime() > timeout) { throw new TimeoutException("Job didn't finish within 10 seconds"); } } } }