/** * Convenience getter for for the id of the enclosing job. Useful for DAO * implementations. * * @return the id of the enclosing job */ public Long getJobId() { if (jobInstance != null) { return jobInstance.getId(); } return null; }
@Override @Nullable public JobInstance getJobInstance(@Nullable Long instanceId) { for (Map.Entry<String, JobInstance> instanceEntry : jobInstances.entrySet()) { JobInstance instance = instanceEntry.getValue(); if (instance.getId().equals(instanceId)) { return instance; } } return null; }
@Override public long getInstanceId() { return jobExecution.getJobInstance().getId(); }
@Override public List<Long> getJobInstances(String jobName, int start, int count) throws NoSuchJobException { List<Long> list = new ArrayList<>(); List<JobInstance> jobInstances = jobExplorer.getJobInstances(jobName, start, count); for (JobInstance jobInstance : jobInstances) { list.add(jobInstance.getId()); } if (list.isEmpty() && !jobRegistry.getJobNames().contains(jobName)) { throw new NoSuchJobException("No such job (either in registry or in historical data): " + jobName); } return list; }
@Override public JobExecution getLastJobExecution(JobInstance jobInstance) { Long id = jobInstance.getId(); List<JobExecution> executions = getJdbcTemplate().query(getQuery(GET_LAST_EXECUTION), new JobExecutionRowMapper(jobInstance), id, id); Assert.state(executions.size() <= 1, "There must be at most one latest job execution"); if (executions.isEmpty()) { return null; } else { return executions.get(0); } }
/** * Convenient accessor for current job identifier. * * @return the job identifier of the enclosing {@link JobInstance} * associated with the current {@link StepExecution} */ public Long getJobId() { Assert.state(stepExecution.getJobExecution() != null, "StepExecution does not have a JobExecution"); Assert.state(stepExecution.getJobExecution().getJobInstance() != null, "StepExecution does not have a JobInstance"); return stepExecution.getJobExecution().getJobInstance().getId(); }
@Override public JobInstance getJobInstance(long executionId) throws NoSuchJobExecutionException, JobSecurityException { org.springframework.batch.core.JobExecution execution = jobExplorer.getJobExecution(executionId); if(execution == null) { throw new NoSuchJobExecutionException("The JobExecution was not found"); } return jobExplorer.getJobInstance(execution.getJobInstance().getId()); }
@Transactional @Test public void testVersionIsNotNullForJob() throws Exception { int version = jdbcTemplate.queryForObject("select version from BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=" + jobInstance.getId(), Integer.class); assertEquals(0, version); }
@Test public void testGetInstanceId() { when(instance.getId()).thenReturn(5L); assertEquals(5L, context.getInstanceId()); }
@Transactional @Test public void testJobWithSimpleJobIdentifier() throws Exception { String testJob = "test"; // Create job. jobInstance = jobInstanceDao.createJobInstance(testJob, jobParameters); List<Map<String, Object>> jobs = jdbcTemplate.queryForList( "SELECT * FROM BATCH_JOB_INSTANCE where JOB_INSTANCE_ID=?", jobInstance.getId()); assertEquals(1, jobs.size()); assertEquals("test", jobs.get(0).get("JOB_NAME")); }
@Transactional @Test public void testUpdateJobExecutionWithLongExitCode() { assertTrue(LONG_STRING.length() > 250); ((JdbcJobExecutionDao) jobExecutionDao).setExitMessageLength(250); jobExecution.setExitStatus(ExitStatus.COMPLETED .addExitDescription(LONG_STRING)); jobExecutionDao.updateJobExecution(jobExecution); List<Map<String, Object>> executions = jdbcTemplate.queryForList( "SELECT * FROM BATCH_JOB_EXECUTION where JOB_INSTANCE_ID=?", jobInstance.getId()); assertEquals(1, executions.size()); assertEquals(LONG_STRING.substring(0, 250), executions.get(0) .get("EXIT_MESSAGE")); }
@Transactional @Test public void testCreateAndGetById() throws Exception { JobInstance fooInstance = dao.createJobInstance(fooJob, fooParams); assertNotNull(fooInstance.getId()); assertEquals(fooJob, fooInstance.getJobName()); JobInstance retrievedInstance = dao.getJobInstance(fooInstance.getId()); assertEquals(fooInstance, retrievedInstance); assertEquals(fooJob, retrievedInstance.getJobName()); }
/** * Create and retrieve a job instance. */ @Transactional @Test public void testGetLastInstances() throws Exception { testCreateAndRetrieve(); // unrelated job instance that should be ignored by the query dao.createJobInstance("anotherJob", new JobParameters()); // we need two instances of the same job to check ordering dao.createJobInstance(fooJob, new JobParameters()); List<JobInstance> jobInstances = dao.getJobInstances(fooJob, 0, 2); assertEquals(2, jobInstances.size()); assertEquals(fooJob, jobInstances.get(0).getJobName()); assertEquals(fooJob, jobInstances.get(1).getJobName()); assertEquals(Integer.valueOf(0), jobInstances.get(0).getVersion()); assertEquals(Integer.valueOf(0), jobInstances.get(1).getVersion()); assertTrue("Last instance should be first on the list", jobInstances.get(0).getId() > jobInstances.get(1) .getId()); }
@Transactional @Test public void testCreateAndRetrieve() throws Exception { JobInstance fooInstance = dao.createJobInstance(fooJob, fooParams); assertNotNull(fooInstance.getId()); assertEquals(fooJob, fooInstance.getJobName()); JobInstance retrievedInstance = dao.getJobInstance(fooJob, fooParams); assertEquals(fooInstance, retrievedInstance); assertEquals(fooJob, retrievedInstance.getJobName()); }
private void checkRepository(BatchStatus status, ExitStatus exitStatus) { // because map dao stores in memory, it can be checked directly JobInstance jobInstance = jobExecution.getJobInstance(); JobExecution other = jobExecutionDao.findJobExecutions(jobInstance).get(0); assertEquals(jobInstance.getId(), other.getJobId()); assertEquals(status, other.getStatus()); if (exitStatus != null) { assertEquals(exitStatus.getExitCode(), other.getExitStatus().getExitCode()); } }
private void checkRepository(BatchStatus status, ExitStatus exitStatus) { // because map DAO stores in memory, it can be checked directly JobInstance jobInstance = jobExecution.getJobInstance(); JobExecution other = jobExecutionDao.findJobExecutions(jobInstance).get(0); assertEquals(jobInstance.getId(), other.getJobId()); assertEquals(status, other.getStatus()); if (exitStatus != null) { assertEquals(exitStatus.getExitCode(), other.getExitStatus().getExitCode()); } }
@Transactional @Test public void testCreateAndRetrieveWithNullParameter() throws Exception { JobParameters jobParameters = new JobParametersBuilder().addString("foo", null).toJobParameters(); JobInstance fooInstance = dao.createJobInstance(fooJob, jobParameters); assertNotNull(fooInstance.getId()); assertEquals(fooJob, fooInstance.getJobName()); JobInstance retrievedInstance = dao.getJobInstance(fooJob, jobParameters); assertEquals(fooInstance, retrievedInstance); assertEquals(fooJob, retrievedInstance.getJobName()); }
private void checkRepository(BatchStatus status, ExitStatus exitStatus) { assertEquals(jobInstance, jobInstanceDao.getJobInstance(job.getName(), jobParameters)); // because map DAO stores in memory, it can be checked directly JobExecution jobExecution = jobExecutionDao.findJobExecutions(jobInstance).get(0); assertEquals(jobInstance.getId(), jobExecution.getJobId()); assertEquals(status, jobExecution.getStatus()); if (exitStatus != null) { assertEquals(exitStatus.getExitCode(), jobExecution.getExitStatus().getExitCode()); } }