View Javadoc

1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/DefaultMethodRetryHandler.java,v 1.3 2004/04/18 23:51:34 jsdever Exp $
3    * $Revision: 1.3 $
4    * $Date: 2004/04/18 23:51:34 $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   */
29  
30  package org.apache.commons.httpclient;
31  
32  /***
33   * The default MethodRetryHandler used by HttpMethodBase.
34   * 
35   * @author Michael Becke
36   * 
37   * @see HttpMethodBase#setMethodRetryHandler(MethodRetryHandler)
38   */
39  public class DefaultMethodRetryHandler implements MethodRetryHandler {
40  
41      /*** the number of times a method will be retried */
42      private int retryCount;
43      
44      /*** Whether or not methods that have successfully sent their request will be retried */
45      private boolean requestSentRetryEnabled;
46      
47      /***
48       */
49      public DefaultMethodRetryHandler() {
50          this.retryCount = 3;
51          this.requestSentRetryEnabled = false;
52      }
53      
54      /*** 
55       * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
56       * if the given method should be retried.
57       * 
58       * @see MethodRetryHandler#retryMethod(HttpMethod, HttpConnection, HttpRecoverableException, int, boolean)
59       */
60      public boolean retryMethod(
61          HttpMethod method,
62          HttpConnection connection,
63          HttpRecoverableException recoverableException,
64          int executionCount,
65          boolean requestSent
66      ) {
67          return ((!requestSent || requestSentRetryEnabled) && (executionCount <= retryCount));
68      }
69      /***
70       * @return <code>true</code> if this handler will retry methods that have 
71       * successfully sent their request, <code>false</code> otherwise
72       */
73      public boolean isRequestSentRetryEnabled() {
74          return requestSentRetryEnabled;
75      }
76  
77      /***
78       * @return the maximum number of times a method will be retried
79       */
80      public int getRetryCount() {
81          return retryCount;
82      }
83  
84      /***
85       * @param requestSentRetryEnabled a flag indicating if methods that have 
86       * successfully sent their request should be retried
87       */
88      public void setRequestSentRetryEnabled(boolean requestSentRetryEnabled) {
89          this.requestSentRetryEnabled = requestSentRetryEnabled;
90      }
91  
92      /***
93       * @param retryCount the maximum number of times a method can be retried
94       */
95      public void setRetryCount(int retryCount) {
96          this.retryCount = retryCount;
97      }
98  
99  }