/** * 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; }
/** * 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; }
@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; }
private BatchStatus waitForJobComplete(JobOperator jobOperator, long executionId) { JobExecution execution = jobOperator.getJobExecution(executionId); BatchStatus curBatchStatus = execution.getBatchStatus(); while(true) { if(curBatchStatus == BatchStatus.STOPPED || curBatchStatus == BatchStatus.COMPLETED || curBatchStatus == BatchStatus.FAILED) { break; } execution = jobOperator.getJobExecution(executionId); curBatchStatus = execution.getBatchStatus(); } return curBatchStatus; } }
@Test(expected=NoSuchJobExecutionException.class) public void testGetJobExecutionNoExecutionFound() { jsrJobOperator.getJobExecution(5L); }
/** * We need to keep the test running because JobOperator runs the batch job in an asynchronous way. * Returns when either the job execution completes or we have polled the maximum number of tries. * * @param jobExecution * the JobExecution of the job that is being runned on JobOperator. * @return the most recent JobExecution obtained for this execution * @throws InterruptedException thrown by Thread.sleep. */ public static JobExecution keepTestAlive(JobExecution jobExecution) throws InterruptedException { System.out.println(" * Entering keepTestAlive, completed is: " + jobExecution.getBatchStatus().equals(COMPLETED)); int maxTries = 0; while (!jobExecution.getBatchStatus().equals(COMPLETED)) { if (maxTries < MAX_TRIES) { maxTries++; Thread.sleep(THREAD_SLEEP); jobExecution = BatchRuntime.getJobOperator().getJobExecution(jobExecution.getExecutionId()); } else { break; } } Thread.sleep(THREAD_SLEEP); System.out.println(" * Exiting keepTestAlive, completed is: " + jobExecution.getBatchStatus().equals(COMPLETED)); return jobExecution; }
@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"); } } } }
/** * 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 execution id to restart * @param properties The Properties to pass to the new run * @param timeout The length of time to wait for the job to run * @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; }
/** * Executes a job and waits for it's status to be any of {@link javax.batch.runtime.BatchStatus#STOPPED}, * {@link javax.batch.runtime.BatchStatus#COMPLETED}, or {@link javax.batch.runtime.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 Name of the job to run * @param properties Properties to pass the job * @param timeout length of time to wait for a job to finish * @return the {@link javax.batch.runtime.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 { System.out.println("Operator = " + operator); 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; }
@Test public void testGetJobExecutionRoseyScenario() { when(jobExplorer.getJobExecution(5L)).thenReturn(new JobExecution(5L)); assertEquals(5L, jsrJobOperator.getJobExecution(5L).getExecutionId()); }
@Test public void testCustomBaseContextCustomWired() throws Exception { GenericApplicationContext context = new AnnotationConfigApplicationContext(BatchConfgiuration.class); JobOperator jobOperator = (JobOperator) context.getBean("jobOperator"); assertEquals(context, ReflectionTestUtils.getField(jobOperator, "baseContext")); long executionId = jobOperator.start("longRunningJob", null); // Give the job a chance to get started Thread.sleep(1000L); jobOperator.stop(executionId); // Give the job the chance to finish stopping Thread.sleep(1000L); assertEquals(BatchStatus.STOPPED, jobOperator.getJobExecution(executionId).getBatchStatus()); System.getProperties().remove("JSR-352-BASE-CONTEXT"); }
@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()); }
@Test public void testCustomBaseContextJsrCompliant() throws Exception { System.setProperty("JSR-352-BASE-CONTEXT", "META-INF/alternativeJsrBaseContext.xml"); ReflectionTestUtils.setField(JsrJobOperator.BaseContextHolder.class, "instance", null); JobOperator jobOperator = BatchRuntime.getJobOperator(); Object transactionManager = ReflectionTestUtils.getField(jobOperator, "transactionManager"); assertTrue(transactionManager instanceof ResourcelessTransactionManager); long executionId = jobOperator.start("longRunningJob", null); // Give the job a chance to get started Thread.sleep(1000L); jobOperator.stop(executionId); // Give the job the chance to finish stopping Thread.sleep(1000L); assertEquals(BatchStatus.STOPPED, jobOperator.getJobExecution(executionId).getBatchStatus()); System.getProperties().remove("JSR-352-BASE-CONTEXT"); }
protected void waitEnd(final JobOperator jobOperator, final long id) { do { try { Thread.sleep(100); } catch (final InterruptedException e) { return; } } while (!BATCH_END_STATUSES.contains(jobOperator.getJobExecution(id).getBatchStatus())); } }
@GET @Path("/jobs/{jobId}") @Produces(MediaType.APPLICATION_JSON) public Response listBatchJobById(@PathParam("jobId") String jobId) { try { JobExecution execution = BatchRuntime.getJobOperator().getJobExecution(Long.valueOf(jobId)); return Response.ok(mapper.writeValueAsString(JobExecutionEntity.create(execution))).build(); } catch (Exception e) { return Response.ok(EMPTY_JSON_OBJ).build(); } }
@Override public JobExecution getJobExecution(long executionId) throws NoSuchJobExecutionException, JobSecurityException { Transaction tran = suspendTran(); try { return jobOperator.getJobExecution(executionId); } finally { resumeTran(tran); } }
/** * @return true if the given jobexecution ran (or is running) on this server. */ @Override public boolean isLocalJobExecution(long executionId) { JobExecution jobExecution = BatchRuntime.getJobOperator().getJobExecution(executionId); return (jobExecution instanceof WSJobExecution) ? isLocalJobExecution( (WSJobExecution) jobExecution ) : true; }
@Override public void execute() throws MojoExecutionException, MojoFailureException { final JobExecution execution = getOrCreateOperator().getJobExecution(executionId); getLog().info("Job execution for execution #" + executionId + ":"); getLog().info(" - " + toStr(execution)); } }
private List<StepExecution> findStepExecutions() throws JobSecurityException, NoSuchJobExecutionException { JobOperator jobOperator = AbstractListCommand.getJobOperatorFromBatchRuntime(); JobExecution je = jobOperator.getJobExecution(Long.parseLong(executionId)); if (!glassFishBatchSecurityHelper.isVisibleToThisInstance(((TaggedJobExecution) je).getTagName())) throw new NoSuchJobExecutionException("No job execution exists for job execution id: " + executionId); List<StepExecution> stepExecutions = jobOperator.getStepExecutions(Long.parseLong(executionId)); if (stepExecutions == null || stepExecutions.size() == 0) throw new NoSuchJobExecutionException("No job execution exists for job execution id: " + executionId); return stepExecutions; }
public static void startJobAndWait(String jobName, Properties jobParams, int timeoutInMs) throws InterruptedException { JobOperator jobOperator = BatchRuntime.getJobOperator(); long execId = jobOperator.start( jobName, jobParams ); JobExecution jobExec = jobOperator.getJobExecution( execId ); jobExec = JobTestUtil.waitForTermination( jobOperator, jobExec, timeoutInMs ); assertThat( jobExec.getBatchStatus() ).isEqualTo( BatchStatus.COMPLETED ); }