View Javadoc

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.repeat.policy;
18  
19  import org.springframework.batch.repeat.RepeatStatus;
20  import org.springframework.batch.repeat.RepeatContext;
21  import org.springframework.batch.repeat.context.RepeatContextSupport;
22  import org.springframework.batch.repeat.support.RepeatTemplate;
23  import org.springframework.util.ClassUtils;
24  
25  /**
26   * Policy for terminating a batch after a fixed number of operations. Internal
27   * state is maintained and a counter incremented, so successful use of this
28   * policy requires that isComplete() is only called once per batch item. Using
29   * the standard {@link RepeatTemplate} should ensure this contract is kept, but it needs
30   * to be carefully monitored.
31   * 
32   * @author Dave Syer
33   * 
34   */
35  public class SimpleCompletionPolicy extends DefaultResultCompletionPolicy {
36  
37  	public static final int DEFAULT_CHUNK_SIZE = 5;
38  
39  	int chunkSize = 0;
40  
41  	public SimpleCompletionPolicy() {
42  		this(DEFAULT_CHUNK_SIZE);
43  	}
44  
45  	public SimpleCompletionPolicy(int chunkSize) {
46  		super();
47  		this.chunkSize = chunkSize;
48  	}
49  
50  	public void setChunkSize(int chunkSize) {
51  		this.chunkSize = chunkSize;
52  	}
53  
54  	/**
55  	 * Reset the counter.
56  	 * 
57  	 * @see org.springframework.batch.repeat.CompletionPolicy#start(RepeatContext)
58  	 */
59      @Override
60  	public RepeatContext start(RepeatContext context) {
61  		return new SimpleTerminationContext(context);
62  	}
63  
64  	/**
65  	 * Terminate if the chunk size has been reached, or the result is null.
66  	 * 
67  	 * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(RepeatContext,
68  	 * RepeatStatus)
69  	 * @throws RuntimeException (normally terminating the batch) if the result is
70  	 * itself an exception.
71  	 */
72      @Override
73  	public boolean isComplete(RepeatContext context, RepeatStatus result) {
74  		return super.isComplete(context, result) || ((SimpleTerminationContext) context).isComplete();
75  	}
76  
77  	/**
78  	 * Terminate if the chunk size has been reached.
79  	 * 
80  	 * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(RepeatContext)
81  	 */
82      @Override
83  	public boolean isComplete(RepeatContext context) {
84  		return ((SimpleTerminationContext) context).isComplete();
85  	}
86  
87  	/**
88  	 * Increment the counter in the context.
89  	 * 
90  	 * @see org.springframework.batch.repeat.CompletionPolicy#update(RepeatContext)
91  	 */
92      @Override
93  	public void update(RepeatContext context) {
94  		((SimpleTerminationContext) context).update();
95  	}
96  
97  	protected class SimpleTerminationContext extends RepeatContextSupport {
98  
99  		public SimpleTerminationContext(RepeatContext context) {
100 			super(context);
101 		}
102 
103 		public void update() {
104 			increment();
105 		}
106 
107 		public boolean isComplete() {
108 			return getStartedCount() >= chunkSize;
109 		}
110 	}
111 	
112 	/* (non-Javadoc)
113 	 * @see java.lang.Object#toString()
114 	 */
115     @Override
116 	public String toString() {
117 		return ClassUtils.getShortName(SimpleCompletionPolicy.class)+": chunkSize="+chunkSize;
118 	}
119 
120 }