-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add util for retrying an api command automatically after delay (#…
…83)
- Loading branch information
1 parent
5701e3d
commit fb9cffc
Showing
2 changed files
with
30 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/bigboxer23/utils/command/RetryingCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.bigboxer23.utils.command; | ||
|
||
import java.io.IOException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** Run a command and retry it once after a pause */ | ||
public class RetryingCommand { | ||
private static final Logger logger = LoggerFactory.getLogger(RetryingCommand.class); | ||
|
||
public static <T> T execute(Command<T> command, String identifier) throws IOException { | ||
try { | ||
logger.info("Starting command " + identifier); | ||
return command.execute(); | ||
} catch (IOException e) { | ||
logger.error("error running command, attempting to retry " + identifier, e); | ||
try { | ||
Thread.sleep(5 * 1000); // 5 sec | ||
return command.execute(); | ||
} catch (IOException e2) { | ||
logger.error("error retrying command " + identifier, e2); | ||
throw e2; | ||
} catch (InterruptedException e2) { | ||
logger.error("error retrying command " + identifier, e2); | ||
} | ||
} | ||
return null; | ||
} | ||
} |