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.ExitStatus; |
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 | public RepeatContext start(RepeatContext context) { |
60 | return new SimpleTerminationContext(context); |
61 | } |
62 | |
63 | /** |
64 | * Terminate if the chunk size has been reached, or the result is null. |
65 | * |
66 | * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(RepeatContext, |
67 | * ExitStatus) |
68 | * @throws RuntimeException (normally terminating the batch) if the result is |
69 | * itself an exception. |
70 | */ |
71 | public boolean isComplete(RepeatContext context, ExitStatus result) { |
72 | return super.isComplete(context, result) || ((SimpleTerminationContext) context).isComplete(); |
73 | } |
74 | |
75 | /** |
76 | * Terminate if the chunk size has been reached. |
77 | * |
78 | * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(RepeatContext) |
79 | */ |
80 | public boolean isComplete(RepeatContext context) { |
81 | return ((SimpleTerminationContext) context).isComplete(); |
82 | } |
83 | |
84 | /** |
85 | * Increment the counter in the context. |
86 | * |
87 | * @see org.springframework.batch.repeat.CompletionPolicy#update(RepeatContext) |
88 | */ |
89 | public void update(RepeatContext context) { |
90 | ((SimpleTerminationContext) context).update(); |
91 | } |
92 | |
93 | protected class SimpleTerminationContext extends RepeatContextSupport { |
94 | |
95 | public SimpleTerminationContext(RepeatContext context) { |
96 | super(context); |
97 | } |
98 | |
99 | public void update() { |
100 | increment(); |
101 | } |
102 | |
103 | public boolean isComplete() { |
104 | return getStartedCount() >= chunkSize; |
105 | } |
106 | } |
107 | |
108 | /* (non-Javadoc) |
109 | * @see java.lang.Object#toString() |
110 | */ |
111 | public String toString() { |
112 | return ClassUtils.getShortName(SimpleCompletionPolicy.class)+": chunkSize="+chunkSize; |
113 | } |
114 | |
115 | } |