View Javadoc

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  
17  package org.springframework.batch.core.resource;
18  
19  import org.springframework.batch.core.JobParameters;
20  import org.springframework.batch.core.StepExecution;
21  import org.springframework.batch.core.StepExecutionListener;
22  import org.springframework.batch.core.listener.StepExecutionListenerSupport;
23  import org.springframework.batch.repeat.CompletionPolicy;
24  import org.springframework.batch.repeat.RepeatContext;
25  import org.springframework.batch.repeat.RepeatStatus;
26  import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
27  import org.springframework.util.Assert;
28  
29  /**
30   * <p>
31   * A {@link CompletionPolicy} that picks up a commit interval from
32   * {@link JobParameters} by listening to the start of a step. Use anywhere that
33   * a {@link CompletionPolicy} can be used (usually at the chunk level in a
34   * step), and inject as a {@link StepExecutionListener} into the surrounding
35   * step. N.B. only after the step has started will the completion policy be
36   * usable.
37   * </p>
38   *
39   * <p>
40   * It is easier and probably preferable to simply declare the chunk with a
41   * commit-interval that is a late-binding expression (e.g.
42   * <code>#{jobParameters['commit.interval']}</code>). That feature is available
43   * from of Spring Batch 2.1.7.
44   * </p>
45   *
46   * @author Dave Syer
47   *
48   * @see CompletionPolicy
49   */
50  public class StepExecutionSimpleCompletionPolicy extends StepExecutionListenerSupport implements CompletionPolicy {
51  
52  	private CompletionPolicy delegate;
53  
54  	private String keyName = "commit.interval";
55  
56  	/**
57  	 * Public setter for the key name of a Long value in the
58  	 * {@link JobParameters} that will contain a commit interval. Defaults to
59  	 * "commit.interval".
60  	 * @param keyName the keyName to set
61  	 */
62  	public void setKeyName(String keyName) {
63  		this.keyName = keyName;
64  	}
65  
66  	/**
67  	 * Set up a {@link SimpleCompletionPolicy} with a commit interval taken from
68  	 * the {@link JobParameters}. If there is a Long parameter with the given
69  	 * key name, the intValue of this parameter is used. If not an exception
70  	 * will be thrown.
71  	 *
72  	 * @see org.springframework.batch.core.listener.StepExecutionListenerSupport#beforeStep(org.springframework.batch.core.StepExecution)
73  	 */
74  	@Override
75  	public void beforeStep(StepExecution stepExecution) {
76  		JobParameters jobParameters = stepExecution.getJobParameters();
77  		Assert.state(jobParameters.getParameters().containsKey(keyName),
78  				"JobParameters do not contain Long parameter with key=[" + keyName + "]");
79  		delegate = new SimpleCompletionPolicy((int) jobParameters.getLong(keyName));
80  	}
81  
82  	/**
83  	 * @param context
84  	 * @param result
85  	 * @return true if the commit interval has been reached or the result
86  	 * indicates completion
87  	 * @see CompletionPolicy#isComplete(RepeatContext, RepeatStatus)
88  	 */
89  	@Override
90  	public boolean isComplete(RepeatContext context, RepeatStatus result) {
91  		Assert.state(delegate != null, "The delegate resource has not been initialised. "
92  				+ "Remember to register this object as a StepListener.");
93  		return delegate.isComplete(context, result);
94  	}
95  
96  	/**
97  	 * @param context
98  	 * @return if the commit interval has been reached
99  	 * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext)
100 	 */
101 	@Override
102 	public boolean isComplete(RepeatContext context) {
103 		Assert.state(delegate != null, "The delegate resource has not been initialised. "
104 				+ "Remember to register this object as a StepListener.");
105 		return delegate.isComplete(context);
106 	}
107 
108 	/**
109 	 * @param parent
110 	 * @return a new {@link RepeatContext}
111 	 * @see org.springframework.batch.repeat.CompletionPolicy#start(org.springframework.batch.repeat.RepeatContext)
112 	 */
113 	@Override
114 	public RepeatContext start(RepeatContext parent) {
115 		Assert.state(delegate != null, "The delegate resource has not been initialised. "
116 				+ "Remember to register this object as a StepListener.");
117 		return delegate.start(parent);
118 	}
119 
120 	/**
121 	 * @param context
122 	 * @see org.springframework.batch.repeat.CompletionPolicy#update(org.springframework.batch.repeat.RepeatContext)
123 	 */
124 	@Override
125 	public void update(RepeatContext context) {
126 		Assert.state(delegate != null, "The delegate resource has not been initialised. "
127 				+ "Remember to register this object as a StepListener.");
128 		delegate.update(context);
129 	}
130 
131 	/**
132 	 * Delegates to the wrapped {@link CompletionPolicy} if set, otherwise
133 	 * returns the value of {@link #setKeyName(String)}.
134 	 */
135 	@Override
136 	public String toString() {
137 		return (delegate == null) ? keyName : delegate.toString();
138 	}
139 
140 }