diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/RunIdIncrementer.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/RunIdIncrementer.java index 9b6cff5d0..e796960a6 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/RunIdIncrementer.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/RunIdIncrementer.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2018 the original author or authors. + * Copyright 2006-2020 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. @@ -15,12 +15,18 @@ */ package org.springframework.batch.core.launch.support; +import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.JobParametersIncrementer; import org.springframework.lang.Nullable; /** + * This incrementer increments a "run.id" parameter of type {@link Long} + * from the given job parameters. If the parameter does not exist, it will + * be initialized to 1. The parameter name can be configured using + * {@link #setKey(String)}. + * * @author Dave Syer * @author Mahmoud Ben Hassine */ @@ -41,14 +47,27 @@ public class RunIdIncrementer implements JobParametersIncrementer { /** * Increment the run.id parameter (starting with 1). + * + * @param parameters the previous job parameters + * @return the next job parameters with an incremented (or initialized) run.id + * @throws IllegalArgumentException if the previous value of run.id is invalid */ @Override public JobParameters getNext(@Nullable JobParameters parameters) { JobParameters params = (parameters == null) ? new JobParameters() : parameters; - - long id = params.getLong(key, new Long(0)) + 1; - return new JobParametersBuilder(params).addLong(key, id).toJobParameters(); + JobParameter runIdParameter = params.getParameters().get(this.key); + long id = 1; + if (runIdParameter != null) { + try { + id = Long.parseLong(runIdParameter.getValue().toString()) + 1; + } + catch (NumberFormatException exception) { + throw new IllegalArgumentException("Invalid value for parameter " + + this.key, exception); + } + } + return new JobParametersBuilder(params).addLong(this.key, id).toJobParameters(); } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/RunIdIncrementerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/RunIdIncrementerTests.java index 9e85f3a24..461365292 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/RunIdIncrementerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/RunIdIncrementerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2013 the original author or authors. + * Copyright 2006-2020 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. @@ -24,6 +24,7 @@ import org.springframework.batch.core.JobParametersBuilder; /** * @author Dave Syer * @author Michael Minella + * @author Mahmoud Ben Hassine * */ public class RunIdIncrementerTests { @@ -51,4 +52,25 @@ public class RunIdIncrementerTests { assertEquals(1, next.getLong("foo").intValue()); } + @Test + public void testGetNextWhenRunIdIsString() { + // given + JobParameters parameters = new JobParametersBuilder() + .addString("run.id", "5") + .toJobParameters(); + + // when + JobParameters next = this.incrementer.getNext(parameters); + + // then + assertEquals(Long.valueOf(6), next.getLong("run.id")); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetNextWhenRunIdIsInvalidString() { + this.incrementer.getNext(new JobParametersBuilder() + .addString("run.id", "foo") + .toJobParameters()); + } + }