-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Andrei-Luksha2/EGD-5267-read-default-proce…
…ssingLoopInterval-for-DefaultAsyncMessageProcessor-from-env-var Egd 5267 read default processing loop interval for default async message processor from env var
- Loading branch information
Showing
5 changed files
with
101 additions
and
5 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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/homeaway/datatools/photon/utils/EnvironmentVariablesUtils.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,55 @@ | ||
package com.homeaway.datatools.photon.utils; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import java.util.function.Function; | ||
|
||
public class EnvironmentVariablesUtils { | ||
public static <T> T getEnvVarOrUseDefault( | ||
final String envVarName, | ||
T defaultValue, | ||
Logger log, | ||
Function<String, T> parsingFunction | ||
) { | ||
T result; | ||
final String envVarValue = System.getenv(envVarName); | ||
if (envVarValue == null) { | ||
log.info( | ||
"Environment variable {} is not set. Will use the default {} value.", | ||
envVarName, | ||
defaultValue | ||
); | ||
result = defaultValue; | ||
} else { | ||
log.info( | ||
"Environment variable {} is set to {}", | ||
envVarName, | ||
envVarValue | ||
); | ||
result = parsingFunction.apply(envVarValue); | ||
} | ||
return result; | ||
} | ||
|
||
public static long getLongOrUseDefault(final String envVarName, Long defaultValue, Logger log) { | ||
Function<String, Long> parsingFunction = (String arg) -> parseLong(envVarName, arg); | ||
return getEnvVarOrUseDefault(envVarName, defaultValue, log, parsingFunction); | ||
} | ||
|
||
public static long parseLong(final String envVarName, final String envVarValue) { | ||
long result; | ||
try { | ||
result = Long.parseLong(envVarValue); | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Failed to parse value %s of environment variable %s as a Long value.", | ||
envVarValue, | ||
envVarName | ||
), | ||
e | ||
); | ||
} | ||
return result; | ||
} | ||
} |
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