public class RetryTemplate extends Object
try {
new RetryTemplate(10, 5).execute(new RetryTemplate.RetryCallback() {
public Object doWithRetry() throws Exception {
// dosome processing
return null;
}
});
} catch (Exception e) {
log.fatal(e);
throw e;
}
If you wish to return a value, you can return it as an object and cast it.
String hello = (String) new RetryTemplate(10, 5).execute(new RetryTemplate.RetryCallback() {
public Object doWithRetry() throws Exception {
return "hello world";
}
});
System.out.println(hello);
To use variables within the function either declare it final, or extend RetryCallback. Eg,
final String hello = "hello world";
new RetryTemplate(10, 5).execute(new RetryTemplate.RetryCallback() {
public Object doWithRetry() throws Exception {
System.out.println(hello);
return null;
}
});
or
public class PrintCallback extends RetryTemplate.RetryCallback {
private String str = null;
public PrintCallback(String str) {
this.str = str;
}
public Object doWithRetry() throws Exception {
System.out.println(str);
return null;
}
}
new RetryTemplate(10, 5).execute(new PrintCallback("hello world"));
You can optionally override the RetryCallback onException function if for example you wish to log each exception or
close and reopen a connection. Eg:
SomeConnectionClass connection = bla bla;
new RetryTemplate(10,5).execute(new RetryTemplate.RetryCallback(){
public Object doWithRetry() throws Exception {
// dosome processing
return null;
}
public void onException(Exception e) {
log.warn(e);
connection.close();
connection.open();
}
});
| Modifier and Type | Class and Description |
|---|---|
static class |
RetryTemplate.RetryCallback<T>
Abstract base class for use with RetryTemplate
|
| Constructor and Description |
|---|
RetryTemplate() |
RetryTemplate(int maxAttempts,
int sleepSecs) |
| Modifier and Type | Method and Description |
|---|---|
<T> T |
execute(RetryTemplate.RetryCallback<T> callback)
Calls doWithRetry on the callback object while an exception occurs or we reach the maximum retries.
|
protected void |
retrySleep() |
public RetryTemplate()
public RetryTemplate(int maxAttempts,
int sleepSecs)
public <T> T execute(RetryTemplate.RetryCallback<T> callback) throws Exception
Exceptionprotected void retrySleep()
Copyright © 2012 Last.fm. All Rights Reserved.