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