Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
ascendbatch2025
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Bhargavi Ghanta
ascendbatch2025
Commits
787f8429
Commit
787f8429
authored
Jun 11, 2025
by
Bhargavi Ghanta
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add RetryUtility with exponential backoff logic
parent
383cb52d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
4 deletions
+15
-4
RetryUtility.java
src/RetryUtility.java
+15
-4
No files found.
src/RetryUtility.java
View file @
787f8429
...
@@ -2,15 +2,26 @@ import java.util.concurrent.Callable;
...
@@ -2,15 +2,26 @@ import java.util.concurrent.Callable;
public
class
RetryUtility
{
public
class
RetryUtility
{
/**
* Retries a task up to maxRetries with exponential backoff.
*
* @param task The task to execute.
* @param maxRetries Number of retry attempts.
* @param initialDelayMillis Initial delay before retrying (in milliseconds).
* @return The result of the task if successful.
* @throws Exception If all retry attempts fail.
*/
public
static
<
T
>
T
retry
(
Callable
<
T
>
task
,
int
maxRetries
,
long
initialDelayMillis
)
throws
Exception
{
public
static
<
T
>
T
retry
(
Callable
<
T
>
task
,
int
maxRetries
,
long
initialDelayMillis
)
throws
Exception
{
int
attempt
=
0
;
int
attempt
=
0
;
while
(
true
)
{
while
(
true
)
{
try
{
try
{
return
task
.
call
();
// Try to execute
the task
return
task
.
call
();
// Attempt
the task
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
attempt
++;
attempt
++;
if
(
attempt
>
maxRetries
)
{
if
(
attempt
>
maxRetries
)
{
throw
new
Exception
(
"All retry attempts failed"
,
e
);
throw
new
Exception
(
"All retry attempts failed
after "
+
maxRetries
+
" tries
"
,
e
);
}
}
long
backoffTime
=
initialDelayMillis
*
(
1L
<<
(
attempt
-
1
));
// Exponential backoff
long
backoffTime
=
initialDelayMillis
*
(
1L
<<
(
attempt
-
1
));
// Exponential backoff
...
@@ -25,12 +36,12 @@ public class RetryUtility {
...
@@ -25,12 +36,12 @@ public class RetryUtility {
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
try
{
try
{
String
result
=
retry
(()
->
{
String
result
=
retry
(()
->
{
// Simulate
network call that fails randomly
// Simulate
an unreliable remote service
if
(
Math
.
random
()
<
0.7
)
{
if
(
Math
.
random
()
<
0.7
)
{
throw
new
RuntimeException
(
"Network error!"
);
throw
new
RuntimeException
(
"Network error!"
);
}
}
return
"Success!"
;
return
"Success!"
;
},
5
,
1000
);
//
Retry up to 5 times, starting with 1 second delay
},
5
,
1000
);
//
Try up to 5 times, starting with 1 second backoff
System
.
out
.
println
(
"Final Result: "
+
result
);
System
.
out
.
println
(
"Final Result: "
+
result
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment