From 7b3d4388f1e546c3e9260ab0df8d828355ea8f55 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Tue, 16 Jan 2018 10:58:14 +0100 Subject: [PATCH] BATCH-2667: update warning message to be more explicit Before this commit, when a job execution is stopped from a different JVM than the one running the job, a warning says that the job cannot be found. This was confusing to some users since the job can be found in the database (but is actually not defined in the job registry of the application context of the second JVM). After this commit is applied, the warning will be more explicit to inform the user that the job cannot be found in the job registry (to not be confused with the database) Resolves BATCH-2667 --- .../launch/support/SimpleJobOperator.java | 2 +- .../support/SimpleJobOperatorTests.java | 37 +++++++++++++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java index f892f93bd..08d94dc0c 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java @@ -425,7 +425,7 @@ public class SimpleJobOperator implements JobOperator, InitializingBean { } } catch (NoSuchJobException e) { - logger.warn("Cannot find Job object",e); + logger.warn("Cannot find Job object in the job registry. StoppableTasklet#stop() will not be called",e); } return true; diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java index fd88f88d6..4e19d50de 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java @@ -15,14 +15,6 @@ */ package org.springframework.batch.core.launch.support; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -34,6 +26,7 @@ import java.util.Set; import org.junit.Before; import org.junit.Test; + import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; @@ -64,6 +57,16 @@ import org.springframework.batch.core.step.tasklet.TaskletStep; import org.springframework.batch.repeat.RepeatStatus; import org.springframework.batch.support.PropertiesConverter; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + /** * @author Dave Syer * @author Will Schipp @@ -383,6 +386,24 @@ public class SimpleJobOperatorTests { jobOperator.stop(111L); assertEquals(BatchStatus.STOPPING, jobExecution.getStatus()); } + + @Test + public void testStopTaskletWhenJobNotRegistered() throws Exception { + JobInstance jobInstance = new JobInstance(123L, job.getName()); + JobExecution jobExecution = new JobExecution(jobInstance, 111L, jobParameters, null); + StoppableTasklet tasklet = mock(StoppableTasklet.class); + JobRegistry jobRegistry = mock(JobRegistry.class); + TaskletStep step = mock(TaskletStep.class); + + when(step.getTasklet()).thenReturn(tasklet); + when(jobRegistry.getJob(job.getName())).thenThrow(new NoSuchJobException("Unable to find job")); + when(jobExplorer.getJobExecution(111L)).thenReturn(jobExecution); + + jobOperator.setJobRegistry(jobRegistry); + jobOperator.stop(111L); + assertEquals(BatchStatus.STOPPING, jobExecution.getStatus()); + verify(tasklet, never()).stop(); + } @Test public void testStopTaskletException() throws Exception {