/** * 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; }
/** * 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; }
@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); }
@Override public long restart(final long executionId, final Properties restartParameters) throws JobExecutionAlreadyCompleteException, NoSuchJobExecutionException, JobExecutionNotMostRecentException, JobRestartException, JobSecurityException { return getDelegate().restart(executionId, restartParameters); }
@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)); }
@Override public long restart(final long executionId, final String restartParameters) { return operator.restart(executionId, toProperties(restartParameters)); }
@Override public long restart(long executionId, Properties restartParameters) throws JobExecutionAlreadyCompleteException, NoSuchJobExecutionException, JobExecutionNotMostRecentException, JobRestartException, JobSecurityException { Transaction tran = suspendTran(); try { return jobOperator.restart(executionId, restartParameters); } finally { resumeTran(tran); } }
public TCKJobExecutionWrapper restartJobWithoutWaitingForResult(long oldExecutionId, Properties jobParameters) throws NoSuchJobExecutionException, NoSuchJobException, JobRestartException, JobExecutionAlreadyCompleteException, JobExecutionNotMostRecentException, JobSecurityException, JobExecutionTimeoutException { Long execID = (Long)jobOp.restart(oldExecutionId, jobParameters); JobExecution jobExecution = jobOp.getJobExecution(execID); return new TCKJobExecutionWrapper(jobExecution, jobOp); }
public TCKJobExecutionWrapper restartJobAndWaitForResult(long oldExecutionId, Properties restartJobParameters) throws NoSuchJobExecutionException, NoSuchJobException, JobRestartException, JobExecutionAlreadyCompleteException, JobExecutionNotMostRecentException, JobSecurityException, JobExecutionTimeoutException { JobExecution terminatedJobExecution = null; long newExecutionId = jobOp.restart(oldExecutionId, restartJobParameters); JobExecutionWaiter waiter = waiterFactory.createWaiter(newExecutionId, jobOp, sleepTime); try { terminatedJobExecution = waiter.awaitTermination(); } catch (JobExecutionTimeoutException e) { logger.severe(TIMEOUT_MSG); Reporter.log(TIMEOUT_MSG); throw e; } return new TCKJobExecutionWrapper(terminatedJobExecution, jobOp); }
@Override public void execute() throws MojoExecutionException, MojoFailureException { final long restartId = getOrCreateOperator().restart(executionId, toProperties(jobParameters)); getLog().info("Restarted #" + executionId + ", new id is #" + restartId); if (wait) { waitEnd(getOrCreateOperator(), restartId); } } }
private void batchRestart(String batchId, HttpServletRequest req, HttpServletResponse resp) { Long executionId = extractExecutionId(batchId, resp); if (executionId == null) { return; } Properties jobProperties = extractJobProperties(req, resp); try { jobOperator.restart(executionId, jobProperties); } catch (NoSuchJobExecutionException noSuchJob) { reportFailure(executionId, resp, "NoSuchJob"); } catch (JobExecutionAlreadyCompleteException alreadyCompleted) { reportFailure(executionId, resp, "NoSuchJob"); } catch (Exception generalException) { StringBuilder msg = new StringBuilder("Failure in BatchExecution"); appendExceptionMsg(msg, generalException); reportFailure(executionId, resp, msg.toString()); } }
@Override public JobExecution restart(Long jobExecutionId, JobParameters params) throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, NoSuchJobException, JobParametersInvalidException { JobExecution jobExecution = null; JobExecution target = getJobExecution(jobExecutionId); JobInstance lastInstance = target.getJobInstance(); if(jobLocator.getJobNames().contains(lastInstance.getJobName())) { Job job = jobLocator.getJob(lastInstance.getJobName()); jobExecution = jobLauncher.run(job, target.getJobParameters()); if (jobExecution.isRunning()) { activeExecutions.add(jobExecution); } } else { if(jsrJobOperator != null) { if(params != null) { jobExecution = new JobExecution(jsrJobOperator.restart(jobExecutionId, params.toProperties())); } else { jobExecution = new JobExecution(jsrJobOperator.restart(jobExecutionId, new Properties())); } } else { throw new NoSuchJobException(String.format("Can't find job associated with job execution id %s to restart", String.valueOf(jobExecutionId))); } } return jobExecution; }
@Override public JobExecution restart(Long jobExecutionId, JobParameters params) throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, NoSuchJobException, JobParametersInvalidException { JobExecution jobExecution = null; JobExecution target = getJobExecution(jobExecutionId); JobInstance lastInstance = target.getJobInstance(); if(jobLocator.getJobNames().contains(lastInstance.getJobName())) { Job job = jobLocator.getJob(lastInstance.getJobName()); jobExecution = jobLauncher.run(job, target.getJobParameters()); if (jobExecution.isRunning()) { activeExecutions.add(jobExecution); } } else { if(jsrJobOperator != null) { if(params != null) { jobExecution = new JobExecution(jsrJobOperator.restart(jobExecutionId, params.toProperties())); } else { jobExecution = new JobExecution(jsrJobOperator.restart(jobExecutionId, new Properties())); } } else { throw new NoSuchJobException(String.format("Can't find job associated with job execution id %s to restart", String.valueOf(jobExecutionId))); } } return jobExecution; }
@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); } }
@Test public void testJob() throws InterruptedException, IOException, ParseException { assertEquals( 0, messageManager.findMessagesFor( SDF.parse( "31/08/2016" ) ).size() ); assertEquals( 0, messageManager.findMessagesFor( SDF.parse( "01/09/2016" ) ).size() ); JobOperator jobOperator = BatchRuntime.getJobOperator(); // The 1st execution. Keep it alive and wait Byteman to stop it JobInterruptorUtil.enable(); long execId1 = jobOperator.start( MassIndexingJob.NAME, MassIndexingJob.parameters() .forEntity( Message.class ) .build() ); JobExecution jobExec1 = jobOperator.getJobExecution( execId1 ); jobExec1 = JobTestUtil.waitForTermination( jobOperator, jobExec1, JOB_TIMEOUT_MS ); assertEquals( BatchStatus.FAILED, jobExec1.getBatchStatus() ); JobInterruptorUtil.disable(); // Restart the job. This is the 2nd execution. long execId2 = jobOperator.restart( execId1, null ); JobExecution jobExec2 = jobOperator.getJobExecution( execId2 ); jobExec2 = JobTestUtil.waitForTermination( jobOperator, jobExec2, JOB_TIMEOUT_MS ); assertEquals( BatchStatus.COMPLETED, jobExec2.getBatchStatus() ); assertEquals( DB_DAY1_ROWS, messageManager.findMessagesFor( SDF.parse( "31/08/2016" ) ).size() ); assertEquals( DB_DAY2_ROWS, messageManager.findMessagesFor( SDF.parse( "01/09/2016" ) ).size() ); }
@Test public void testJob_usingCriteria() throws InterruptedException, IOException, ParseException { assertEquals( 0, messageManager.findMessagesFor( SDF.parse( "31/08/2016" ) ).size() ); assertEquals( 0, messageManager.findMessagesFor( SDF.parse( "01/09/2016" ) ).size() ); JobOperator jobOperator = BatchRuntime.getJobOperator(); // The 1st execution. Keep it alive and wait Byteman to stop it JobInterruptorUtil.enable(); long execId1 = jobOperator.start( MassIndexingJob.NAME, MassIndexingJob.parameters() .forEntity( Message.class ) .restrictedBy( Restrictions.ge( "date", SDF.parse( "01/09/2016" ) ) ) .build() ); JobExecution jobExec1 = jobOperator.getJobExecution( execId1 ); jobExec1 = JobTestUtil.waitForTermination( jobOperator, jobExec1, JOB_TIMEOUT_MS ); assertEquals( BatchStatus.FAILED, jobExec1.getBatchStatus() ); JobInterruptorUtil.disable(); // Restart the job. This is the 2nd execution. long execId2 = jobOperator.restart( execId1, null ); JobExecution jobExec2 = jobOperator.getJobExecution( execId2 ); jobExec2 = JobTestUtil.waitForTermination( jobOperator, jobExec2, JOB_TIMEOUT_MS ); assertEquals( BatchStatus.COMPLETED, jobExec2.getBatchStatus() ); assertEquals( 0, messageManager.findMessagesFor( SDF.parse( "31/08/2016" ) ).size() ); assertEquals( DB_DAY2_ROWS, messageManager.findMessagesFor( SDF.parse( "01/09/2016" ) ).size() ); }
@Test public void testJob_usingHQL() throws Exception { assertEquals( 0, messageManager.findMessagesFor( SDF.parse( "31/08/2016" ) ).size() ); assertEquals( 0, messageManager.findMessagesFor( SDF.parse( "01/09/2016" ) ).size() ); JobOperator jobOperator = BatchRuntime.getJobOperator(); JobInterruptorUtil.enable(); long execId1 = jobOperator.start( MassIndexingJob.NAME, MassIndexingJob.parameters() .forEntity( Message.class ) .restrictedBy( "select m from Message m where day( m.date ) = 1" ) .build() ); JobExecution jobExec1 = BatchRuntime.getJobOperator().getJobExecution( execId1 ); jobExec1 = JobTestUtil.waitForTermination( jobOperator, jobExec1, JOB_TIMEOUT_MS ); assertEquals( BatchStatus.FAILED, jobExec1.getBatchStatus() ); JobInterruptorUtil.disable(); // Restart the job. long execId2 = jobOperator.restart( execId1, null ); JobExecution jobExec2 = jobOperator.getJobExecution( execId2 ); JobTestUtil.waitForTermination( jobOperator, jobExec2, JOB_TIMEOUT_MS ); assertEquals( BatchStatus.COMPLETED, jobExec2.getBatchStatus() ); assertEquals( 0, messageManager.findMessagesFor( SDF.parse( "31/08/2016" ) ).size() ); assertEquals( DB_DAY2_ROWS, messageManager.findMessagesFor( SDF.parse( "01/09/2016" ) ).size() ); }
/** * */ @Test(expected = IllegalArgumentException.class) public void testRestartFail() throws Exception { final JobOperator mockJobOperator = mockery.mock(JobOperator.class); final TransactionManager mockTranMgr = mockery.mock(TransactionManager.class); final Transaction mockTran = mockery.mock(Transaction.class); mockery.checking( new Expectations() { { oneOf(mockTranMgr).suspend(); will(returnValue(mockTran)); oneOf(mockJobOperator).restart(0, null); will(throwException(new IllegalArgumentException())); oneOf(mockTranMgr).resume(mockTran); } }); buildJobOperator(mockJobOperator, mockTranMgr).restart(0, null); }
/** * */ @Test public void testRestartNoTran() throws Exception { final JobOperator mockJobOperator = mockery.mock(JobOperator.class); final TransactionManager mockTranMgr = mockery.mock(TransactionManager.class); final long retMe = 1; mockery.checking( new Expectations() { { oneOf(mockTranMgr).suspend(); will(returnValue(null)); oneOf(mockJobOperator).restart(0, null); will(returnValue(retMe)); } }); assertEquals(retMe, buildJobOperator(mockJobOperator, mockTranMgr).restart(0, null) ); }
/** * */ @Test public void testRestart() throws Exception { final JobOperator mockJobOperator = mockery.mock(JobOperator.class); final TransactionManager mockTranMgr = mockery.mock(TransactionManager.class); final Transaction mockTran = mockery.mock(Transaction.class); final long retMe = 1; mockery.checking( new Expectations() { { oneOf(mockTranMgr).suspend(); will(returnValue(mockTran)); oneOf(mockJobOperator).restart(0, null); will(returnValue(retMe)); oneOf(mockTranMgr).resume(mockTran); } }); assertEquals(retMe, buildJobOperator(mockJobOperator, mockTranMgr).restart(0, null) ); }