Explicitly considering Start Time not being null for running Job Executions

This is to exclude from capturing erroneous Job Executions. An example is
whenever a TaskRejectedException is thrown after submitting to the
taskExecutor in SimpleJobLauncher#run(), the JobExecution is left without
a Start or End Time. Also related tests are fixed.

Resolves BATCH-2675
This commit is contained in:
Dimitrios Liapis
2018-10-18 22:51:04 +02:00
committed by Mahmoud Ben Hassine
parent 76f8cad29a
commit f8f8a02a4d
11 changed files with 72 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2014 the original author or authors.
* Copyright 2006-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.springframework.util.SerializationUtils;
/**
* @author Dave Syer
* @author Dimitrios Liapis
*
*/
public class JobExecutionTests {
@@ -65,6 +66,7 @@ public class JobExecutionTests {
*/
@Test
public void testIsRunning() {
execution.setStartTime(new Date());
assertTrue(execution.isRunning());
execution.setEndTime(new Date(100L));
assertFalse(execution.isRunning());
@@ -76,6 +78,7 @@ public class JobExecutionTests {
*/
@Test
public void testIsRunningWithStoppedExecution() {
execution.setStartTime(new Date());
assertTrue(execution.isRunning());
execution.stop();
assertTrue(execution.isRunning());

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010-2014 the original author or authors.
* Copyright 2010-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,11 +18,14 @@ package org.springframework.batch.core.explore.support;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import java.util.Date;
/**
* Tests for {@link MapJobExplorerFactoryBean}.
*/
@@ -36,8 +39,13 @@ public class MapJobExplorerFactoryBeanTests {
public void testCreateExplorer() throws Exception {
MapJobRepositoryFactoryBean repositoryFactory = new MapJobRepositoryFactoryBean();
repositoryFactory.getObject().createJobExecution("foo", new JobParameters());
JobRepository jobRepository = repositoryFactory.getObject();
JobExecution jobExecution = jobRepository.createJobExecution("foo", new JobParameters());
//simulating a running job execution
jobExecution.setStartTime(new Date());
jobRepository.update(jobExecution);
MapJobExplorerFactoryBean tested = new MapJobExplorerFactoryBean(repositoryFactory);
tested.afterPropertiesSet();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -74,6 +74,9 @@ import static org.junit.Assert.fail;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Tests for {@link JsrJobOperator}.
*/
public class JsrJobOperatorTests extends AbstractJsrTestCase {
private JobOperator jsrJobOperator;
@@ -210,6 +213,8 @@ public class JsrJobOperatorTests extends AbstractJsrTestCase {
@Test(expected=JobExecutionIsRunningException.class)
public void testAbandonJobRunning() throws Exception {
JobExecution jobExecution = new JobExecution(5L);
jobExecution.setStartTime(new Date(1L));
when(jobExplorer.getJobExecution(5L)).thenReturn(jobExecution);
jsrJobOperator.abandon(5L);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,6 +39,9 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Parent Test Class for {@link JdbcJobExecutionDao} and {@link MapJobExecutionDao}.
*/
public abstract class AbstractJobExecutionDaoTests {
protected JobExecutionDao dao;
@@ -194,15 +197,25 @@ public abstract class AbstractJobExecutionDaoTests {
@Transactional
@Test
public void testFindRunningExecutions() {
//Normally completed JobExecution as EndTime is populated
JobExecution exec = new JobExecution(jobInstance, jobParameters);
exec.setCreateTime(new Date(0));
exec.setEndTime(new Date(1L));
exec.setStartTime(new Date(1L));
exec.setEndTime(new Date(2L));
exec.setLastUpdated(new Date(5L));
dao.saveJobExecution(exec);
//BATCH-2675
//Abnormal JobExecution as both StartTime and EndTime are null
//This can occur when SimpleJobLauncher#run() submission to taskExecutor throws a TaskRejectedException
exec = new JobExecution(jobInstance, jobParameters);
exec.setLastUpdated(new Date(5L));
dao.saveJobExecution(exec);
//Running JobExecution as StartTime is populated but EndTime is null
exec = new JobExecution(jobInstance, jobParameters);
exec.setStartTime(new Date(2L));
exec.setLastUpdated(new Date(5L));
exec.createStepExecution("step");
dao.saveJobExecution(exec);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -73,6 +73,8 @@ public class OptimisticLockingFailureTests {
.addLong("test", 1L)
.toJobParameters());
Thread.sleep(1000);
while(restartJobExecution.isRunning()) {
// wait for async launched job to complete execution
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,11 +17,14 @@ package org.springframework.batch.core.repository.support;
import org.junit.Test;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.job.JobSupport;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobRepository;
import java.util.Date;
import static org.junit.Assert.fail;
/**
@@ -42,7 +45,11 @@ public class MapJobRepositoryFactoryBeanTests {
Job job = new JobSupport("jobName");
JobParameters jobParameters = new JobParameters();
repository.createJobExecution(job.getName(), jobParameters);
JobExecution jobExecution = repository.createJobExecution(job.getName(), jobParameters);
// simulate a running execution
jobExecution.setStartTime(new Date());
repository.update(jobExecution);
try {
repository.createJobExecution(job.getName(), jobParameters);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@ import static org.junit.Assert.fail;
* Repository tests using JDBC DAOs (rather than mocks).
*
* @author Robert Kasanicky
* @author Dimitrios Liapis
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/org/springframework/batch/core/repository/dao/sql-dao-test.xml")
@@ -180,7 +181,11 @@ public class SimpleJobRepositoryIntegrationTests {
@Test
public void testOnlyOneJobExecutionAllowedRunning() throws Exception {
job.setRestartable(true);
jobRepository.createJobExecution(job.getName(), jobParameters);
JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters);
//simulating a running job execution
jobExecution.setStartTime(new Date());
jobRepository.update(jobExecution);
try {
jobRepository.createJobExecution(job.getName(), jobParameters);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2014 the original author or authors.
* Copyright 2006-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,6 +55,7 @@ import org.springframework.batch.core.step.StepSupport;
*
* @author Lucas Ward
* @author Will Schipp
* @author Dimitrios Liapis
*
*/
public class SimpleJobRepositoryTests {
@@ -240,6 +241,7 @@ public class SimpleJobRepositoryTests {
@Test(expected = JobExecutionAlreadyRunningException.class)
public void testCreateJobExecutionAlreadyRunning() throws Exception {
jobExecution.setStatus(BatchStatus.STARTED);
jobExecution.setStartTime(new Date());
jobExecution.setEndTime(null);
when(jobInstanceDao.getJobInstance("foo", new JobParameters())).thenReturn(jobInstance);