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
- Removed unused imports - Increased checkstyle line length - Fixed PMD warning
- Loading branch information
sajid riaz
committed
Sep 5, 2024
1 parent
bdb23fd
commit e5073ff
Showing
13 changed files
with
795 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
157 changes: 157 additions & 0 deletions
157
...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,157 @@ | ||
/* | ||
* 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.Locale; | ||
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; | ||
private static final long DEFAULT_INITIAL_DELAY = 86400; | ||
private static final long DEFAULT_FIXED_DELAY = 86400; | ||
|
||
@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"; | ||
@JsonProperty ("initialDelay") | ||
private long myInitialDelay = DEFAULT_INITIAL_DELAY; | ||
@JsonProperty ("fixedDelay") | ||
private long myFixedDelay = DEFAULT_FIXED_DELAY; | ||
|
||
public RetryPolicyConfig() | ||
{ | ||
} | ||
|
||
public RetryPolicyConfig(final Integer maxAttempts, | ||
final Integer delay, | ||
final Integer maxDelay, | ||
final String unit, | ||
final long initialDelay, | ||
final long fixedDelay) | ||
{ | ||
this.myMaxAttempts = maxAttempts; | ||
this.myDelay = convertToMillis(delay, unit); | ||
this.myMaxDelay = convertToMillis(maxDelay, unit); | ||
this.myUnit = unit; | ||
this.myInitialDelay = initialDelay; | ||
this.myFixedDelay = fixedDelay; | ||
} | ||
|
||
@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); | ||
} | ||
|
||
@JsonProperty ("initialDelay") | ||
public long getInitialDelay() | ||
{ | ||
return myInitialDelay; | ||
} | ||
|
||
@JsonProperty ("initialDelay") | ||
public void setInitialDelay(final Integer initialDelay) | ||
{ | ||
this.myInitialDelay = convertToMillis(initialDelay, myUnit); | ||
} | ||
|
||
@JsonProperty ("fixedDelay") | ||
public long getFixedDelay() | ||
{ | ||
return myFixedDelay; | ||
} | ||
|
||
@JsonProperty ("fixedDelay") | ||
public void setFixedDelay(final Integer fixedDelay) | ||
{ | ||
this.myFixedDelay = convertToMillis(fixedDelay, myUnit); | ||
} | ||
|
||
private long convertToMillis(final Integer value, final String unit) | ||
{ | ||
return switch (unit.toLowerCase(Locale.ENGLISH)) | ||
{ | ||
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.