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