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.RepeatContext;
20  import org.springframework.batch.repeat.context.RepeatContextSupport;
21  
22  /**
23   * Termination policy that times out after a fixed period. Allows graceful exit
24   * from a batch if the latest result comes in after the timeout expires (i.e.
25   * does not throw a timeout exception).<br/>
26   * 
27   * N.B. It may often be the case that the batch governed by this policy will be
28   * transactional, and the transaction might have its own timeout. In this case
29   * the transaction might throw a timeout exception on commit if its timeout
30   * threshold is lower than the termination policy.
31   * 
32   * @author Dave Syer
33   * 
34   */
35  public class TimeoutTerminationPolicy extends CompletionPolicySupport {
36  
37  	/**
38  	 * Default timeout value in millisecs (the value equivalent to 30 seconds).
39  	 */
40  	public static final long DEFAULT_TIMEOUT = 30000L;
41  
42  	private long timeout = DEFAULT_TIMEOUT;
43  
44  	/**
45  	 * Default constructor.
46  	 */
47  	public TimeoutTerminationPolicy() {
48  		super();
49  	}
50  
51  	/**
52  	 * Construct a {@link TimeoutTerminationPolicy} with the specified timeout
53  	 * value (in milliseconds).
54  	 * 
55  	 * @param timeout
56  	 */
57  	public TimeoutTerminationPolicy(long timeout) {
58  		super();
59  		this.timeout = timeout;
60  	}
61  
62  	/**
63  	 * Check the timeout and complete gracefully if it has expires.
64  	 * 
65  	 * @see org.springframework.batch.repeat.CompletionPolicy#isComplete(org.springframework.batch.repeat.RepeatContext)
66  	 */
67  	@Override
68  	public boolean isComplete(RepeatContext context) {
69  		return ((TimeoutBatchContext) context).isComplete();
70  	}
71  
72  	/**
73  	 * Start the clock on the timeout.
74  	 * 
75  	 * @see org.springframework.batch.repeat.CompletionPolicy#start(RepeatContext)
76  	 */
77  	@Override
78  	public RepeatContext start(RepeatContext context) {
79  		return new TimeoutBatchContext(context);
80  	}
81  
82  	protected class TimeoutBatchContext extends RepeatContextSupport {
83  
84  		private volatile long time = System.currentTimeMillis();
85  
86  		private final long timeout = TimeoutTerminationPolicy.this.timeout;
87  
88  		public TimeoutBatchContext(RepeatContext context) {
89  			super(context);
90  		}
91  
92  		public boolean isComplete() {
93  			return (System.currentTimeMillis() - time) > timeout;
94  		}
95  
96  	}
97  
98  }