1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.concurrent.Executors;
  4. import java.util.concurrent.ScheduledExecutorService;
  5. import java.util.concurrent.TimeUnit;
  6. public class CookieWrapper {
  7. private final CookieService cookieService; // Interface for cookie management
  8. private final int maxRetries;
  9. private final long retryDelayMillis;
  10. private final ScheduledExecutorService scheduler;
  11. private final Map<String, String> cache = new HashMap<>(); //Simple cache for demonstration
  12. public CookieWrapper(CookieService cookieService, int maxRetries, long retryDelayMillis) {
  13. this.cookieService = cookieService;
  14. this.maxRetries = maxRetries;
  15. this.retryDelayMillis = retryDelayMillis;
  16. this.scheduler = Executors.newSingleThreadScheduledExecutor();
  17. }
  18. public String getCookie(String name) {
  19. int retries = 0;
  20. while (retries <= maxRetries) {
  21. try {
  22. String cookieValue = cookieService.getCookie(name); // Call the original cookie logic
  23. if (cookieValue != null) {
  24. return cookieValue;
  25. } else {
  26. retries++;
  27. if (retries <= maxRetries) {
  28. try {
  29. Thread.sleep(retryDelayMillis); // Wait before retrying
  30. } catch (InterruptedException e) {
  31. Thread.currentThread().interrupt();
  32. return null; //Or handle the interruption appropriately
  33. }
  34. }
  35. }
  36. } catch (Exception e) {
  37. retries++;
  38. if (retries <= maxRetries) {
  39. try {
  40. Thread.sleep(retryDelayMillis);
  41. } catch (InterruptedException ie) {
  42. Thread.currentThread().interrupt();
  43. return null; //Or handle the interruption appropriately
  44. }
  45. }
  46. //Log the exception here. Important for debugging.
  47. //System.err.println("Error getting cookie: " + e.getMessage());
  48. return null; //Or handle the exception appropriately
  49. }
  50. }
  51. return null; // Return null if all retries fail
  52. }
  53. public void setCookie(String name, String value) {
  54. int retries = 0;
  55. while (retries <= maxRetries) {
  56. try {
  57. cookieService.setCookie(name, value); // Call the original cookie logic
  58. return; // Success, exit the loop
  59. } catch (Exception e) {
  60. retries++;
  61. if (retries <= maxRetries) {
  62. try {
  63. Thread.sleep(retryDelayMillis);
  64. } catch (InterruptedException ie) {
  65. Thread.currentThread().interrupt();
  66. return; //Exit the loop on interruption
  67. }
  68. }
  69. //Log the exception here. Important for debugging.
  70. //System.err.println("Error setting cookie: " + e.getMessage());
  71. }
  72. }
  73. //If all retries fail, handle the error appropriately (e.g., log, throw exception)
  74. //System.err.println("Failed to set cookie after multiple retries.");
  75. }
  76. public void startRetryScheduler() {
  77. scheduler.scheduleAtFixedRate(() -> {
  78. //Example: Periodically refresh cookies (can be customized)
  79. System.out.println("Refreshing cookies...");
  80. for (Map.Entry<String, String> entry : cache.entrySet()) {
  81. setCookie(entry.getKey(), entry.getValue());
  82. }
  83. }, 60, 60, TimeUnit.SECONDS); //Refresh every minute
  84. }
  85. public void stopRetryScheduler() {
  86. scheduler.shutdown();
  87. try {
  88. scheduler.awaitTermination(5, TimeUnit.SECONDS);
  89. } catch (InterruptedException e) {
  90. Thread.currentThread().interrupt();
  91. }
  92. }
  93. //Simple interface for cookie service. Replace with your actual implementation.
  94. interface CookieService {
  95. String getCookie(String name);
  96. void setCookie(String name, String value);
  97. }
  98. public static void main(String[] args) {
  99. //Example Usage
  100. CookieWrapper wrapper = new CookieWrapper(new SimpleCookieService(), 3, 1000);
  101. wrapper.startRetryScheduler();
  102. String cookieValue = wrapper.getCookie("myCookie");
  103. System.out.println("Cookie value: " + cookieValue);
  104. wrapper.setCookie("myCookie", "new

Add your comment