1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class ConfigUtil {
  4. private static final Map<String, String> config = new HashMap<>();
  5. public static void loadConfig() {
  6. // Simulate loading config from a file or environment
  7. config.put("timeout", "10");
  8. config.put("retries", "3");
  9. config.put("url", "http://localhost:8080");
  10. }
  11. public static String get(String key) {
  12. try {
  13. return config.get(key);
  14. } catch (Exception e) {
  15. //Suppress configuration value errors.
  16. System.err.println("Error getting config value for key: " + key + ". Using default.");
  17. return "default_value"; //Provide a default value
  18. }
  19. }
  20. public static String execute(String operation) {
  21. // Simulate an operation with retry logic
  22. int retries = Integer.parseInt(get("retries"));
  23. int attempt = 0;
  24. while (attempt < retries) {
  25. try {
  26. // Perform the operation
  27. System.out.println("Attempting operation: " + operation);
  28. return "Success"; // Simulate success
  29. } catch (Exception e) {
  30. attempt++;
  31. if (attempt < retries) {
  32. System.out.println("Operation failed. Retrying attempt: " + attempt);
  33. try {
  34. Thread.sleep(1000); // Wait before retrying
  35. } catch (InterruptedException ie) {
  36. Thread.currentThread().interrupt();
  37. }
  38. } else {
  39. System.err.println("Operation failed after " + retries + " attempts.");
  40. return "Failure"; // Simulate failure after all retries
  41. }
  42. }
  43. }
  44. return "Failure";
  45. }
  46. public static void main(String[] args) {
  47. loadConfig();
  48. String timeout = get("timeout");
  49. System.out.println("Timeout: " + timeout);
  50. String result = execute("Simulated operation");
  51. System.out.println("Operation result: " + result);
  52. String nonExistentValue = get("nonExistentKey");
  53. System.out.println("Non existent Key: " + nonExistentValue);
  54. }
  55. }

Add your comment