1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.concurrent.CompletableFuture;
  4. import java.util.concurrent.ExecutorService;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.TimeUnit;
  7. public class TokenMetricsCollector {
  8. private final ExecutorService executor = Executors.newFixedThreadPool(5); // Adjust thread pool size as needed
  9. private final Map<String, Long> tokenMetrics = new HashMap<>(); // Token -> Metrics (e.g., validation attempts, success/failure)
  10. public CompletableFuture<Void> collectTokenMetrics(String token, String validationEndpoint, int maxRetries) {
  11. return CompletableFuture.supplyAsync(() -> {
  12. long retries = 0;
  13. boolean success = false;
  14. while (retries < maxRetries) {
  15. try {
  16. // Simulate token validation call
  17. boolean isValid = validateToken(token, validationEndpoint);
  18. if (isValid) {
  19. success = true;
  20. break; // Token is valid, exit the retry loop
  21. } else {
  22. retries++;
  23. System.out.println("Validation failed, retry: " + retries);
  24. //Introduce a delay between retries
  25. try {
  26. TimeUnit.SECONDS.sleep(1);
  27. } catch (InterruptedException e) {
  28. Thread.currentThread().interrupt();
  29. return null;
  30. }
  31. }
  32. } catch (Exception e) {
  33. retries++;
  34. System.err.println("Error during validation, retry: " + retries + ". Error: " + e.getMessage());
  35. try {
  36. TimeUnit.SECONDS.sleep(2); // Longer delay on error
  37. } catch (InterruptedException ie) {
  38. Thread.currentThread().interrupt();
  39. return null;
  40. }
  41. }
  42. }
  43. if (success) {
  44. recordSuccess(token);
  45. return true;
  46. } else {
  47. recordFailure(token, retries);
  48. return false;
  49. }
  50. });
  51. }
  52. private boolean validateToken(String token, String endpoint) {
  53. // Replace with your actual token validation logic
  54. // This is a placeholder for a network call or other validation process
  55. // Simulate validation success or failure based on the token.
  56. if (token.equals("validToken")) {
  57. return true;
  58. } else {
  59. return false;
  60. }
  61. }
  62. private void recordSuccess(String token) {
  63. tokenMetrics.put(token, tokenMetrics.getOrDefault(token, 0L) + 1);
  64. System.out.println("Token " + token + " validated successfully.");
  65. }
  66. private void recordFailure(String token, int retries) {
  67. tokenMetrics.put(token, tokenMetrics.getOrDefault(token, 0L) + retries);
  68. System.out.println("Token " + token + " failed validation after " + retries + " retries.");
  69. }
  70. public Map<String, Long> getTokenMetrics() {
  71. return tokenMetrics;
  72. }
  73. public void shutdown() {
  74. executor.shutdown();
  75. try {
  76. executor.awaitTermination(5, TimeUnit.SECONDS); //Give tasks a chance to finish
  77. } catch (InterruptedException e) {
  78. Thread.currentThread().interrupt();
  79. }
  80. }
  81. public static void main(String[] args) throws Exception {
  82. TokenMetricsCollector collector = new TokenMetricsCollector();
  83. String token1 = "validToken";
  84. String token2 = "invalidToken";
  85. String token3 = "invalidToken";
  86. CompletableFuture<Void> future1 = collector.collectTokenMetrics(token1, "/validate", 3);
  87. CompletableFuture<Void> future2 = collector.collectTokenMetrics(token2, "/validate", 2);
  88. CompletableFuture<Void> future3 = collector.collectTokenMetrics(token3, "/validate", 5);
  89. future1.join();
  90. future2.join();
  91. future3.join();
  92. collector.shutdown();
  93. System.out.println("Final Token Metrics: " + collector.getTokenMetrics());
  94. }
  95. }

Add your comment