forked from Ericsson/ecchronos
-
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.
Retry Policy for Jmx Connection Ericsson#700
- Loading branch information
sajid riaz
committed
Sep 3, 2024
1 parent
957b09f
commit fb5b239
Showing
12 changed files
with
487 additions
and
28 deletions.
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
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
122 changes: 122 additions & 0 deletions
122
...com/ericsson/bss/cassandra/ecchronos/application/config/connection/RetryPolicyConfig.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,122 @@ | ||
/* | ||
* Copyright 2024 Telefonaktiebolaget LM Ericsson | ||
* | ||
* 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 com.ericsson.bss.cassandra.ecchronos.application.config.connection; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public final class RetryPolicyConfig | ||
{ | ||
|
||
private static final int DEFAULT_MAX_ATTEMPTS = 5; | ||
private static final long DEFAULT_DELAY = 5000; | ||
private static final long DEFAULT_MAX_DELAY = 30000; | ||
|
||
@JsonProperty ("maxAttempts") | ||
private Integer myMaxAttempts = DEFAULT_MAX_ATTEMPTS; | ||
|
||
@JsonProperty ("delay") | ||
private long myDelay = DEFAULT_DELAY; | ||
|
||
@JsonProperty ("maxDelay") | ||
private long myMaxDelay = DEFAULT_MAX_DELAY; | ||
|
||
@JsonProperty ("unit") | ||
private String myUnit = "seconds"; // Default to seconds | ||
|
||
public RetryPolicyConfig() | ||
{ | ||
} | ||
|
||
public RetryPolicyConfig(final Integer maxAttempts, final Integer delay, final Integer maxDelay, final String unit) | ||
{ | ||
this.myMaxAttempts = maxAttempts; | ||
this.myDelay = convertToMillis(delay, unit); | ||
this.myMaxDelay = convertToMillis(maxDelay, unit); | ||
this.myUnit = unit; | ||
} | ||
|
||
@JsonProperty ("maxAttempts") | ||
public Integer getMaxAttempts() | ||
{ | ||
return myMaxAttempts; | ||
} | ||
|
||
@JsonProperty ("maxAttempts") | ||
public void setMaxAttempts(final Integer maxAttempts) | ||
{ | ||
this.myMaxAttempts = maxAttempts; | ||
} | ||
|
||
@JsonProperty ("delay") | ||
public long getDelay() | ||
{ | ||
return myDelay; | ||
} | ||
|
||
@JsonProperty ("delay") | ||
public void setDelay(final Integer delay) | ||
{ | ||
this.myDelay = convertToMillis(delay, myUnit); | ||
} | ||
|
||
@JsonProperty ("maxDelay") | ||
public long getMaxDelay() | ||
{ | ||
return myMaxDelay; | ||
} | ||
|
||
@JsonProperty ("maxDelay") | ||
public void setMaxDelay(final Integer maxDelay) | ||
{ | ||
this.myMaxDelay = convertToMillis(maxDelay, myUnit); | ||
} | ||
|
||
@JsonProperty ("unit") | ||
public String getUnit() | ||
{ | ||
return myUnit; | ||
} | ||
|
||
@JsonProperty ("unit") | ||
public void setUnit(final String unit) | ||
{ | ||
this.myUnit = unit; | ||
// Recalculate delays with the new unit | ||
this.myDelay = convertToMillis((int) TimeUnit.MILLISECONDS.toSeconds(this.myDelay), unit); | ||
this.myMaxDelay = convertToMillis((int) TimeUnit.MILLISECONDS.toSeconds(this.myMaxDelay), unit); | ||
} | ||
|
||
private long convertToMillis(final Integer value, final String unit) | ||
{ | ||
return switch (unit.toLowerCase()) | ||
{ | ||
case "milliseconds" -> value; | ||
case "seconds" -> TimeUnit.SECONDS.toMillis(value); | ||
case "minutes" -> TimeUnit.MINUTES.toMillis(value); | ||
default -> throw new IllegalArgumentException("Unsupported time unit: " + unit); | ||
}; | ||
} | ||
|
||
public long currentDelay(final Integer count) | ||
{ | ||
long currentDelay = myDelay * count; | ||
if (currentDelay > myMaxDelay) | ||
{ | ||
currentDelay = myMaxDelay; | ||
} | ||
return currentDelay; | ||
} | ||
} |
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
Oops, something went wrong.