From 560ffc905661a8981f7845cc8e8d95e9bec835c6 Mon Sep 17 00:00:00 2001 From: james Date: Wed, 26 Aug 2015 12:22:44 +0200 Subject: [PATCH 1/2] Do not attempt to restart non-restartable jobs Closes gh-3830 --- .../batch/JobLauncherCommandLineRunner.java | 2 +- .../JobLauncherCommandLineRunnerTests.java | 22 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java index 0577fed22da..d7f0635d4e8 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java @@ -144,7 +144,7 @@ public class JobLauncherCommandLineRunner implements CommandLineRunner, parameters = incrementer.getNext(new JobParameters()); } } - else if (isStoppedOrFailed(previousExecution)) { + else if (isStoppedOrFailed(previousExecution) && job.isRestartable()) { // Retry a failed or stopped execution parameters = previousExecution.getJobParameters(); // Non-identifying additional parameters can be added to a retry diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunnerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunnerTests.java index 9442d06da4c..d10586bddba 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunnerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunnerTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.batch; +import static org.junit.Assert.assertEquals; + import org.junit.Before; import org.junit.Test; import org.springframework.batch.core.Job; @@ -43,8 +45,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.task.SyncTaskExecutor; import org.springframework.transaction.PlatformTransactionManager; -import static org.junit.Assert.assertEquals; - /** * Tests for {@link JobLauncherCommandLineRunner}. * @@ -124,6 +124,24 @@ public class JobLauncherCommandLineRunnerTests { assertEquals(1, this.jobExplorer.getJobInstances("job", 0, 100).size()); } + @Test + public void retryFailedExecutionOnNonRestartableJob() throws Exception { + this.job = this.jobs.get("job").preventRestart() + .start(this.steps.get("step").tasklet(new Tasklet() { + @Override + public RepeatStatus execute(StepContribution contribution, + ChunkContext chunkContext) throws Exception { + throw new RuntimeException("Planned"); + } + }).build()).incrementer(new RunIdIncrementer()).build(); + this.runner.execute(this.job, new JobParameters()); + this.runner.execute(this.job, new JobParameters()); + /* A failed job that is not restartable does not re-use the job params of + * the last execution, but creates a new job instance when running it again. + */ + assertEquals(2, this.jobExplorer.getJobInstances("job", 0, 100).size()); + } + @Test public void retryFailedExecutionWithNonIdentifyingParameters() throws Exception { this.job = this.jobs.get("job") From 1a60056f9e69d5f75bfd9dfc2ba3076f45525708 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 2 Sep 2015 16:52:31 +0200 Subject: [PATCH 2/2] Polish contribution Closes gh-3831 --- .../batch/JobLauncherCommandLineRunner.java | 3 ++- .../batch/JobLauncherCommandLineRunnerTests.java | 12 ++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java index d7f0635d4e8..6a5c99ed405 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunner.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -59,6 +59,7 @@ import org.springframework.util.StringUtils; * by providing a jobName * * @author Dave Syer + * @author Jean-Pierre Bergamin */ @Component public class JobLauncherCommandLineRunner implements CommandLineRunner, diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunnerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunnerTests.java index d10586bddba..14612fb5c81 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunnerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/JobLauncherCommandLineRunnerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -16,8 +16,6 @@ package org.springframework.boot.autoconfigure.batch; -import static org.junit.Assert.assertEquals; - import org.junit.Before; import org.junit.Test; import org.springframework.batch.core.Job; @@ -45,10 +43,13 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.task.SyncTaskExecutor; import org.springframework.transaction.PlatformTransactionManager; +import static org.junit.Assert.assertEquals; + /** * Tests for {@link JobLauncherCommandLineRunner}. * * @author Dave Syer + * @author Jean-Pierre Bergamin */ public class JobLauncherCommandLineRunnerTests { @@ -136,9 +137,8 @@ public class JobLauncherCommandLineRunnerTests { }).build()).incrementer(new RunIdIncrementer()).build(); this.runner.execute(this.job, new JobParameters()); this.runner.execute(this.job, new JobParameters()); - /* A failed job that is not restartable does not re-use the job params of - * the last execution, but creates a new job instance when running it again. - */ + // A failed job that is not restartable does not re-use the job params of + // the last execution, but creates a new job instance when running it again. assertEquals(2, this.jobExplorer.getJobInstances("job", 0, 100).size()); }