From d81ab76823b7a620d22912061f37442c3263a78d 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 | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) 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 b45cddbff..c53c708f2 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 @@ -60,6 +60,8 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** @@ -365,6 +367,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 {