1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.ScheduledExecutorService;
  8. import java.util.concurrent.TimeUnit;
  9. public class UrlBootstrapper {
  10. private final String urlListFile;
  11. private final int retryIntervalSeconds;
  12. private final List<String> urls = new ArrayList<>();
  13. private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
  14. public UrlBootstrapper(String urlListFile, int retryIntervalSeconds) {
  15. this.urlListFile = urlListFile;
  16. this.retryIntervalSeconds = retryIntervalSeconds;
  17. }
  18. public void loadUrls() {
  19. try (BufferedReader reader = new BufferedReader(new FileReader(urlListFile))) {
  20. String line;
  21. while ((line = reader.readLine()) != null) {
  22. if (line.trim().length() > 0) { // Avoid empty lines
  23. urls.add(line.trim());
  24. }
  25. }
  26. } catch (IOException e) {
  27. System.err.println("Error reading URL list: " + e.getMessage());
  28. }
  29. }
  30. public void startBootstrapping() {
  31. loadUrls();
  32. if (urls.isEmpty()) {
  33. System.out.println("No URLs to bootstrap.");
  34. scheduler.shutdown();
  35. return;
  36. }
  37. for (String url : urls) {
  38. BootstrappingTask task = new BootstrappingTask(url);
  39. scheduler.scheduleAtFixedRate(task, 0, retryIntervalSeconds, TimeUnit.SECONDS);
  40. }
  41. }
  42. // Inner class to handle the bootstrapping task
  43. private static class BootstrappingTask implements Runnable {
  44. private final String url;
  45. public BootstrappingTask(String url) {
  46. this.url = url;
  47. }
  48. @Override
  49. public void run() {
  50. try {
  51. // Simulate bootstrapping logic (replace with your actual logic)
  52. System.out.println("Bootstrapping URL: " + url);
  53. // Add your URL bootstrapping code here
  54. } catch (Exception e) {
  55. System.err.println("Error bootstrapping URL " + url + ": " + e.getMessage());
  56. }
  57. }
  58. }
  59. public void stopBootstrapping() {
  60. scheduler.shutdown();
  61. }
  62. public static void main(String[] args) {
  63. // Example usage:
  64. String urlListFile = "url_list.txt"; // Replace with your URL list file
  65. int retryIntervalSeconds = 5; // Retry every 5 seconds
  66. UrlBootstrapper bootstrapper = new UrlBootstrapper(urlListFile, retryIntervalSeconds);
  67. bootstrapper.startBootstrapping();
  68. // Keep the program running for a while, then stop the bootstrapping
  69. try {
  70. Thread.sleep(60000); // Run for 60 seconds
  71. } catch (InterruptedException e) {
  72. e.printStackTrace();
  73. }
  74. bootstrapper.stopBootstrapping();
  75. }
  76. }

Add your comment