|
27 | 27 | import org.springframework.batch.core.BatchStatus; |
28 | 28 | import org.springframework.batch.core.job.JobExecution; |
29 | 29 | import org.springframework.batch.core.job.JobInstance; |
| 30 | +import org.springframework.batch.core.job.JobInterruptedException; |
30 | 31 | import org.springframework.batch.core.job.parameters.JobParameters; |
31 | 32 | import org.springframework.batch.core.repository.JobRepository; |
32 | 33 | import org.springframework.batch.core.step.StepExecution; |
33 | 34 | import org.springframework.batch.core.partition.StepExecutionSplitter; |
| 35 | +import org.springframework.batch.core.step.StepInterruptionPolicy; |
34 | 36 | import org.springframework.integration.MessageTimeoutException; |
35 | 37 | import org.springframework.integration.core.MessagingTemplate; |
36 | 38 | import org.springframework.messaging.Message; |
@@ -251,4 +253,73 @@ void testHandleWithJobRepositoryPollingTimeout() throws Exception { |
251 | 253 | () -> messageChannelPartitionHandler.handle(stepExecutionSplitter, managerStepExecution)); |
252 | 254 | } |
253 | 255 |
|
| 256 | + @Test |
| 257 | + void testShutdownCancelsHandle() throws Exception { |
| 258 | + // execute with no default set |
| 259 | + messageChannelPartitionHandler = new MessageChannelPartitionHandler(); |
| 260 | + // mock |
| 261 | + JobExecution jobExecution = new JobExecution(5L, new JobInstance(1L, "job"), new JobParameters()); |
| 262 | + StepExecution managerStepExecution = new StepExecution(1L, "step1", jobExecution); |
| 263 | + StepExecutionSplitter stepExecutionSplitter = mock(); |
| 264 | + MessagingTemplate operations = mock(); |
| 265 | + JobRepository jobRepository = mock(); |
| 266 | + // when |
| 267 | + HashSet<StepExecution> stepExecutions = new HashSet<>(); |
| 268 | + StepExecution partition1 = new StepExecution(2L, "step1:partition1", jobExecution); |
| 269 | + partition1.setStatus(BatchStatus.STARTED); |
| 270 | + stepExecutions.add(partition1); |
| 271 | + when(stepExecutionSplitter.split(any(StepExecution.class), eq(1))).thenReturn(stepExecutions); |
| 272 | + JobExecution runningJobExecution = new JobExecution(5L, new JobInstance(1L, "job"), new JobParameters()); |
| 273 | + runningJobExecution.addStepExecutions(Arrays.asList(partition1)); |
| 274 | + when(jobRepository.getJobExecution(5L)).thenReturn(runningJobExecution); |
| 275 | + managerStepExecution.setTerminateOnly(); |
| 276 | + |
| 277 | + // set |
| 278 | + messageChannelPartitionHandler.setMessagingOperations(operations); |
| 279 | + messageChannelPartitionHandler.setJobRepository(jobRepository); |
| 280 | + messageChannelPartitionHandler.setStepName("step1"); |
| 281 | + messageChannelPartitionHandler.afterPropertiesSet(); |
| 282 | + |
| 283 | + // execute |
| 284 | + assertThrows(JobInterruptedException.class, |
| 285 | + () -> messageChannelPartitionHandler.handle(stepExecutionSplitter, managerStepExecution)); |
| 286 | + } |
| 287 | + |
| 288 | + @Test |
| 289 | + void testInterruptPolicy() throws Exception { |
| 290 | + String testExceptionMessage = "test exception message"; |
| 291 | + // execute with no default set |
| 292 | + messageChannelPartitionHandler = new MessageChannelPartitionHandler(); |
| 293 | + |
| 294 | + // mock |
| 295 | + JobExecution jobExecution = new JobExecution(5L, new JobInstance(1L, "job"), new JobParameters()); |
| 296 | + StepExecution managerStepExecution = new StepExecution(1L, "step1", jobExecution); |
| 297 | + StepExecutionSplitter stepExecutionSplitter = mock(); |
| 298 | + MessagingTemplate operations = mock(); |
| 299 | + JobRepository jobRepository = mock(); |
| 300 | + // when |
| 301 | + HashSet<StepExecution> stepExecutions = new HashSet<>(); |
| 302 | + StepExecution partition1 = new StepExecution(2L, "step1:partition1", jobExecution); |
| 303 | + partition1.setStatus(BatchStatus.STARTED); |
| 304 | + stepExecutions.add(partition1); |
| 305 | + when(stepExecutionSplitter.split(any(StepExecution.class), eq(1))).thenReturn(stepExecutions); |
| 306 | + JobExecution runningJobExecution = new JobExecution(5L, new JobInstance(1L, "job"), new JobParameters()); |
| 307 | + runningJobExecution.addStepExecutions(Arrays.asList(partition1)); |
| 308 | + when(jobRepository.getJobExecution(5L)).thenReturn(runningJobExecution); |
| 309 | + |
| 310 | + // set |
| 311 | + messageChannelPartitionHandler.setMessagingOperations(operations); |
| 312 | + messageChannelPartitionHandler.setJobRepository(jobRepository); |
| 313 | + messageChannelPartitionHandler.setStepName("step1"); |
| 314 | + messageChannelPartitionHandler.setStepInterruptionPolicy(stepExecution -> { |
| 315 | + throw new JobInterruptedException(testExceptionMessage); |
| 316 | + }); |
| 317 | + messageChannelPartitionHandler.afterPropertiesSet(); |
| 318 | + |
| 319 | + // execute |
| 320 | + JobInterruptedException exception = assertThrows(JobInterruptedException.class, |
| 321 | + () -> messageChannelPartitionHandler.handle(stepExecutionSplitter, managerStepExecution)); |
| 322 | + assertEquals(testExceptionMessage, exception.getMessage()); |
| 323 | + } |
| 324 | + |
254 | 325 | } |
0 commit comments