1 /*
2 * Copyright 2006-2013 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.springframework.batch.core.launch.support;
17
18 import org.springframework.batch.core.JobParameters;
19 import org.springframework.batch.core.JobParametersBuilder;
20 import org.springframework.batch.core.JobParametersIncrementer;
21
22 /**
23 * @author Dave Syer
24 */
25 public class RunIdIncrementer implements JobParametersIncrementer {
26
27 private static String RUN_ID_KEY = "run.id";
28
29 private String key = RUN_ID_KEY;
30
31 /**
32 * The name of the run id in the job parameters. Defaults to "run.id".
33 *
34 * @param key the key to set
35 */
36 public void setKey(String key) {
37 this.key = key;
38 }
39
40 /**
41 * Increment the run.id parameter (starting with 1).
42 */
43 @Override
44 public JobParameters getNext(JobParameters parameters) {
45
46 JobParameters params = (parameters == null) ? new JobParameters() : parameters;
47
48 long id = params.getLong(key, 0L) + 1;
49 return new JobParametersBuilder(params).addLong(key, id).toJobParameters();
50 }
51
52 }