import java.util.HashMap;
import java.util.Map;
public class ConfigUtil {
private static final Map<String, String> config = new HashMap<>();
public static void loadConfig() {
// Simulate loading config from a file or environment
config.put("timeout", "10");
config.put("retries", "3");
config.put("url", "http://localhost:8080");
}
public static String get(String key) {
try {
return config.get(key);
} catch (Exception e) {
//Suppress configuration value errors.
System.err.println("Error getting config value for key: " + key + ". Using default.");
return "default_value"; //Provide a default value
}
}
public static String execute(String operation) {
// Simulate an operation with retry logic
int retries = Integer.parseInt(get("retries"));
int attempt = 0;
while (attempt < retries) {
try {
// Perform the operation
System.out.println("Attempting operation: " + operation);
return "Success"; // Simulate success
} catch (Exception e) {
attempt++;
if (attempt < retries) {
System.out.println("Operation failed. Retrying attempt: " + attempt);
try {
Thread.sleep(1000); // Wait before retrying
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
} else {
System.err.println("Operation failed after " + retries + " attempts.");
return "Failure"; // Simulate failure after all retries
}
}
}
return "Failure";
}
public static void main(String[] args) {
loadConfig();
String timeout = get("timeout");
System.out.println("Timeout: " + timeout);
String result = execute("Simulated operation");
System.out.println("Operation result: " + result);
String nonExistentValue = get("nonExistentKey");
System.out.println("Non existent Key: " + nonExistentValue);
}
}
Add your comment