Newer
Older
piotr.kupczyk@id.ethz.ch
committed
/*
* Copyright 2011 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.ethz.sis.filetransfer;
piotr.kupczyk@id.ethz.ch
committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @author pkupczyk
*/
public class DefaultRetryProvider implements IRetryProvider
{
private ILogger logger;
private int maximumNumberOfRetries;
private int waitingTimeBetweenRetries;
private int waitingTimeBetweenRetriesIncreasingFactor;
public DefaultRetryProvider(ILogger logger)
{
this(logger, 5, 1000, 2);
}
public DefaultRetryProvider(ILogger logger, int maximumNumberOfRetries, int waitingTimeBetweenRetries,
int waitingTimeBetweenRetriesIncreasingFactor)
{
if (maximumNumberOfRetries < 0)
{
throw new IllegalArgumentException("MaximumNumberOfRetries must be >= 0");
}
if (waitingTimeBetweenRetries <= 0)
{
throw new IllegalArgumentException("WaitingTimeBetweenRetries must be > 0");
}
if (waitingTimeBetweenRetriesIncreasingFactor <= 0)
{
throw new IllegalArgumentException(
"WaitingTimeBetweenRetriesIncreasingFactor must be > 0");
}
this.logger = logger;
this.maximumNumberOfRetries = maximumNumberOfRetries;
this.waitingTimeBetweenRetries = waitingTimeBetweenRetries;
this.waitingTimeBetweenRetriesIncreasingFactor = waitingTimeBetweenRetriesIncreasingFactor;
}
@Override
public <T> T executeWithRetry(IRetryAction<T> action) throws DownloadException
{
return new Retry().callWithRetry(action);
}
private class Retry
{
private int retryCounter;
private int waitingTime;
public Retry()
{
this.waitingTime = waitingTimeBetweenRetries;
}
public <T> T callWithRetry(IRetryAction<T> action) throws DownloadException
{
while (true)
{
try
{
T result = action.execute();
retryCounter = 0;
return result;
} catch (Exception e)
{
if (isRetriable(e))
{
if (retryCounter < maximumNumberOfRetries)
{
if (logger.isEnabled(LogLevel.WARN))
{
logger.log(getClass(), LogLevel.WARN, "Call failed - will retry in " + (waitingTime / 1000) + " second(s)", e);
}
waitForRetry();
if (logger.isEnabled(LogLevel.WARN))
{
logger.log(getClass(), LogLevel.WARN, "Retrying (" + retryCounter + "/" + maximumNumberOfRetries + ")");
}
} else
{
if (logger.isEnabled(LogLevel.WARN))
{
logger.log(getClass(), LogLevel.WARN,
"Call failed - will NOT retry (maximum number of " + maximumNumberOfRetries
+ " reties has been already reached)",
e);
piotr.kupczyk@id.ethz.ch
committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
}
throw e;
}
} else
{
throw e;
}
}
}
}
private void waitForRetry()
{
try
{
Thread.sleep(waitingTime);
waitingTime *= waitingTimeBetweenRetriesIncreasingFactor;
retryCounter++;
} catch (InterruptedException e)
{
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
protected boolean isRetriable(Exception e)
{
if (e instanceof DownloadException)
{
DownloadException de = (DownloadException) e;
return de.isRetriable() != null && de.isRetriable();
} else
{
return false;
}