View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.helper;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  
29  /**
30   * Give the ability to retry a function multiple times if it fails.
31   * <br/><br/>
32   * usage : 
33   * 
34   * <code>
35   * <pre>
36   * RetryOnErrorTemplate&lt;String&gt; retryOnError = new RetryOnErrorTemplate&lt;String&gt;() {
37   *	    public String tryThat() throws Exception {
38   *		return THE_RETURNED_VALUE;
39   *	    }
40   *	};
41   * retryOnError.times(3)
42   *</pre>
43   * </code>
44   * 
45   * @author <a href="mailto:david.masclet@gisgraphy.com">David Masclet</a>
46   */
47  public abstract class RetryOnErrorTemplate<T> {
48  
49      private int alreadyTry = 0;
50  
51      private int numberOfTimesToRetry = 1;
52      private T returnvalue = null;
53      
54      protected static final Logger logger = LoggerFactory
55      .getLogger(RetryOnErrorTemplate.class);
56      
57      private String loggingSentence = "?";
58  
59      private RetryOnErrorTemplate<T> retry() throws Exception {
60  	try {
61  	    returnvalue = tryThat();
62  	    alreadyTry++;
63  	    return this;
64  	} catch (Exception e) {
65  	    logger.error("try no "+alreadyTry+" for "+loggingSentence+" fails : " + e.getMessage(),e);
66  	    numberOfTimesToRetry--;
67  	    alreadyTry++;
68  	    if (numberOfTimesToRetry == 0) {
69  		throw e;
70  	    } else {
71  		return retry();
72  	    }
73  	}
74      }
75  
76     
77      public abstract T tryThat() throws Exception;
78  
79      public T times(int numberOfTry) throws Exception {
80  	this.numberOfTimesToRetry = numberOfTry;
81  	retry();
82  	return returnvalue;
83  
84      }
85  
86  
87      /**
88       * @return The number of times the code has already been try
89       */
90      public int getAlreadyTry() {
91          return alreadyTry;
92      }
93  
94  
95      /**
96       * @return The number of times the code should be try  
97       */
98      public int getNumberOfTimesToRetry() {
99          return numberOfTimesToRetry;
100     }
101 
102 
103     /**
104      * @return the loggingSentence
105      */
106     public String getLoggingSentence() {
107         return loggingSentence;
108     }
109 
110 
111     /**
112      * @param loggingSentence the loggingSentence to set
113      */
114     public void setLoggingSentence(String loggingSentence) {
115         this.loggingSentence = loggingSentence;
116     }
117 
118 
119   
120 
121 
122 }