@Test public void testPauseJob() throws Exception { final int SLEEP_INTERVAL = 600; assertTrue(isConnected()); assertTrue(launcher.getJobNames().contains("loopJob")); long executionId = launcher.start("loopJob", ""); // sleep long enough to avoid race conditions (serializable tx isolation // doesn't work with HSQL) Thread.sleep(SLEEP_INTERVAL); launcher.stop(executionId); Thread.sleep(SLEEP_INTERVAL); logger.debug(launcher.getSummary(executionId)); long resumedId = launcher.restart(executionId); assertNotSame("Picked up the same execution after pause and resume", executionId, resumedId); Thread.sleep(SLEEP_INTERVAL); launcher.stop(resumedId); Thread.sleep(SLEEP_INTERVAL); logger.debug(launcher.getSummary(resumedId)); long resumeId2 = launcher.restart(resumedId); assertNotSame("Picked up the same execution after pause and resume", executionId, resumeId2); Thread.sleep(SLEEP_INTERVAL); launcher.stop(resumeId2); }
@Test public void testStartStopResumeJob() throws Exception { String params = new JobParametersBuilder().addLong("jobOperatorTestParam", 7L).toJobParameters().toString(); long executionId = operator.start(job.getName(), params); assertEquals(params, operator.getParameters(executionId)); stopAndCheckStatus(executionId); long resumedExecutionId = operator.restart(executionId); assertEquals(params, operator.getParameters(resumedExecutionId)); stopAndCheckStatus(resumedExecutionId); List<Long> instances = operator.getJobInstances(job.getName(), 0, 1); assertEquals(1, instances.size()); long instanceId = instances.get(0); List<Long> executions = operator.getExecutions(instanceId); assertEquals(2, executions.size()); // latest execution is the first in the returned list assertEquals(resumedExecutionId, executions.get(0).longValue()); assertEquals(executionId, executions.get(1).longValue()); }
String jobName = job.getName(); Set<String> names = operator.getJobNames(); assertEquals(1, names.size()); assertTrue(names.contains(jobName)); long exec1 = operator.startNextInstance(jobName); long exec2 = operator.startNextInstance(jobName); assertTrue(!operator.getParameters(exec1).equals(operator.getParameters(exec2))); Set<Long> executions = operator.getRunningExecutions(jobName); assertTrue(executions.contains(exec1)); assertTrue(executions.contains(exec2)); boolean running = operator.getSummary(exec1).contains("STARTED") && operator.getSummary(exec2).contains("STARTED"); running = operator.getSummary(exec1).contains("STARTED") && operator.getSummary(exec2).contains("STARTED"); assertTrue(String.format("Jobs not started: [%s] and [%s]", operator.getSummary(exec1), operator .getSummary(exec1)), running); operator.stop(exec1); operator.stop(exec2);
/** * @param executionId id of running job execution */ private void stopAndCheckStatus(long executionId) throws Exception { // wait to the job to get up and running Thread.sleep(1000); Set<Long> runningExecutions = operator.getRunningExecutions(job.getName()); assertTrue("Wrong executions: " + runningExecutions + " expected: " + executionId, runningExecutions .contains(executionId)); assertTrue("Wrong summary: " + operator.getSummary(executionId), operator.getSummary(executionId).contains( BatchStatus.STARTED.toString())); operator.stop(executionId); int count = 0; while (operator.getRunningExecutions(job.getName()).contains(executionId) && count <= 10) { LOG.info("Checking for running JobExecution: count=" + count); Thread.sleep(100); count++; } runningExecutions = operator.getRunningExecutions(job.getName()); assertFalse("Wrong executions: " + runningExecutions + " expected: " + executionId, runningExecutions .contains(executionId)); assertTrue("Wrong summary: " + operator.getSummary(executionId), operator.getSummary(executionId).contains( BatchStatus.STOPPED.toString())); // there is just a single step in the test job Map<Long, String> summaries = operator.getStepExecutionSummaries(executionId); LOG.info(summaries); assertTrue(summaries.values().toString().contains(BatchStatus.STOPPED.toString())); }
@Test public void testLaunchJob() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(); Thread.sleep(1000); assertEquals(BatchStatus.STARTED, jobExecution.getStatus()); assertTrue(jobExecution.isRunning()); assertNotNull(jobExecution.getVersion()); jobOperator.stop(jobExecution.getId()); int count = 0; while(jobExecution.isRunning() && count <= 10){ logger.info("Checking for end time in JobExecution: count="+count); Thread.sleep(100); count++; } assertFalse("Timed out waiting for job to end.", jobExecution.isRunning()); assertEquals(BatchStatus.STOPPED, jobExecution.getStatus()); }
Set<Long> runningJobExecutionIDs = this.operator.getRunningExecutions(jobName); if (runningJobExecutionIDs.isEmpty()) { this.operator.stop(id); stoppedCount++; } catch (JobExecutionNotRunningException e) {
@Test public void testLaunchBadJob() throws Exception { assertEquals(0, errors.size()); assertTrue(isConnected()); try { launcher.start("foo", "time=" + (new Date().getTime())); fail("Expected RuntimeException"); } catch (RuntimeException e) { String message = e.getMessage(); assertTrue("Wrong message: " + message, message.contains("NoSuchJobException")); } }
@Test public void testAvailableJobs() throws Exception { assertEquals(0, errors.size()); assertTrue(isConnected()); assertTrue(launcher.getJobNames().contains("loopJob")); }
/** * Restart a job with a given execution id * @param executionId * @return the new job execution id * @throws JobInstanceAlreadyCompleteException * @throws JobParametersInvalidException * @throws NoSuchJobExecutionException * @throws JobRestartException * @throws NoSuchJobException * @throws NoSuchJobInstanceException */ public Long restartJob(long executionId) throws JobInstanceAlreadyCompleteException, JobParametersInvalidException, NoSuchJobExecutionException, JobRestartException, NoSuchJobException, NoSuchJobInstanceException { return jobOperator.restart(executionId); }
SkipCheckingListener.resetProcessSkips(); try { return this.jobOperator.startNextInstance("skipJob");
/** * Find all job ids having a given name. Start will allow to search jobs from a certain starting index and numberJobs indicates the number of job ids to return * @param jobName * @param start * @param numberJobs * @return * @throws NoSuchJobException * @throws NoSuchJobExecutionException * @throws NoSuchJobInstanceException */ public List<Long> findJobIdsByName(String jobName, int start, int numberJobs) throws NoSuchJobException, NoSuchJobExecutionException, NoSuchJobInstanceException { return jobOperator.getJobInstances(jobName, start, numberJobs); }
jobOperator.stop(jobExecution.getId());
Set<Long> runningJobExecutionIDs = this.operator.getRunningExecutions(jobName); if (runningJobExecutionIDs.isEmpty()) { this.operator.stop(id); stoppedCount++; } catch (JobExecutionNotRunningException e) {
/** * * @param jobName * @param parameters the parameters of type key-value separated by comma * @return * @throws JobExecutionAlreadyRunningException * @throws JobRestartException * @throws JobInstanceAlreadyCompleteException */ public String startJobWithParameters(String jobName, String parameters) throws JobParametersInvalidException, JobInstanceAlreadyExistsException, NoSuchJobException { String jobId = jobName+"_"+System.currentTimeMillis(); if (log.isInfoEnabled()) log.info("Starting job: "+jobName+", job id: "+jobId); jobOperator.start(jobName, "MIJobId=" + jobId+(parameters != null ? ","+parameters:"")); return jobId; }
private static boolean isConnected() throws Exception { boolean connected = false; if (!JobRegistryBackgroundJobRunner.getErrors().isEmpty()) { throw JobRegistryBackgroundJobRunner.getErrors().get(0); } if (launcher == null) { MBeanServerConnectionFactoryBean connectionFactory = new MBeanServerConnectionFactoryBean(); try { launcher = (JobOperator) getMBean(connectionFactory, "spring:service=batch,bean=jobOperator", JobOperator.class); loader = (JobLoader) getMBean(connectionFactory, "spring:service=batch,bean=jobLoader", JobLoader.class); } catch (MBeanServerNotFoundException e) { return false; } } try { launcher.getJobNames(); connected = loader.getConfigurations().size() > 0; logger.info("Configurations loaded: " + loader.getConfigurations()); } catch (InvalidInvocationException e) { // ignore } return connected; }
@Override public void restartJobExecution(final Long jobExecutionId) { try { this.jobOperator.restart(jobExecutionId); } catch (final Exception e) { throw new SpringBatchLightminApplicationException(e, e.getMessage()); } }
@Scheduled(fixedDelay = 5000l) public void runJob() throws Exception { this.jobOperator.startNextInstance("job"); } }
/** * get latest execution for a job with the given name. * * @param jobName name of the job * @return latest execution */ protected JobExecution getLatestJobExecution(String jobName) { try { List<Long> jobInstances = jobOperator.getJobInstances(jobName, 0, 1); //todo expensive query! if (isEmpty(jobInstances)) { return null; } Long jobInstanceId = jobInstances.get(0); JobInstance jobInstance = jobExplorer.getJobInstance(jobInstanceId); if (jobInstance == null) { return null; } List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance); if (isEmpty(jobExecutions)) { return null; } return jobExecutions.get(0); } catch (NoSuchJobException e) { return null; } }
@RequestMapping(value = "/jobs/executions/{executionId}", method = RequestMethod.DELETE) public String stop(@PathVariable long executionId) throws NoSuchJobExecutionException, JobExecutionNotRunningException { if (LOG.isDebugEnabled()) { LOG.debug("Stop JobExecution with id: {}", executionId); } Boolean successful = jobOperator.stop(executionId); return successful.toString(); }
@RequestMapping(value = "/", method = RequestMethod.POST) @ResponseStatus(HttpStatus.ACCEPTED) public long launch(@RequestParam("name") String name) throws Exception { return this.jobOperator.start("job", String.format("name=%s", name)); }