Commit 383cb52d authored by Bhargavi Ghanta's avatar Bhargavi Ghanta

Add RetryUtility with exponential backoff

parents
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="ms-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/RetryUtility.iml" filepath="$PROJECT_DIR$/RetryUtility.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
import java.util.concurrent.Callable;
public class RetryUtility {
public static <T> T retry(Callable<T> task, int maxRetries, long initialDelayMillis) throws Exception {
int attempt = 0;
while (true) {
try {
return task.call(); // Try to execute the task
} catch (Exception e) {
attempt++;
if (attempt > maxRetries) {
throw new Exception("All retry attempts failed", e);
}
long backoffTime = initialDelayMillis * (1L << (attempt - 1)); // Exponential backoff
System.out.println("Attempt " + attempt + " failed: " + e.getMessage());
System.out.println("Retrying in " + backoffTime + "ms...");
Thread.sleep(backoffTime);
}
}
}
public static void main(String[] args) {
try {
String result = retry(() -> {
// Simulate network call that fails randomly
if (Math.random() < 0.7) {
throw new RuntimeException("Network error!");
}
return "Success!";
}, 5, 1000); // Retry up to 5 times, starting with 1 second delay
System.out.println("Final Result: " + result);
} catch (Exception e) {
System.err.println("Operation failed after retries: " + e.getMessage());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment