1. import java.io.File;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.concurrent.ConcurrentHashMap;
  5. import java.util.concurrent.Executors;
  6. import java.util.concurrent.ScheduledExecutorService;
  7. import java.util.concurrent.TimeUnit;
  8. public class DirectoryMonitor {
  9. private final Map<String, Long> lastChecked = new ConcurrentHashMap<>(); // Directory path -> last checked timestamp
  10. private final Map<String, Long> rateLimit = new ConcurrentHashMap<>(); // Directory path -> rate limit info
  11. private final long checkIntervalSeconds;
  12. private final int maxChecksPerSecond;
  13. private final ScheduledExecutorService scheduler;
  14. public DirectoryMonitor(long checkIntervalSeconds, int maxChecksPerSecond) {
  15. this.checkIntervalSeconds = checkIntervalSeconds;
  16. this.maxChecksPerSecond = maxChecksPerSecond;
  17. this.scheduler = Executors.newSingleThreadScheduledExecutor();
  18. }
  19. public void start() {
  20. scheduler.scheduleAtFixedRate(this::checkDirectories, 0, checkIntervalSeconds, TimeUnit.SECONDS);
  21. }
  22. public void stop() {
  23. scheduler.shutdown();
  24. }
  25. private void checkDirectories() {
  26. File rootDir = new File("."); // Monitor current directory by default
  27. checkDirectory(rootDir);
  28. }
  29. public void checkDirectory(File dir) {
  30. try {
  31. if (!dir.exists()) {
  32. System.err.println("Directory does not exist: " + dir.getAbsolutePath());
  33. return; // Don't repeatedly check non-existent directories
  34. }
  35. long now = System.currentTimeMillis();
  36. String dirPath = dir.getAbsolutePath();
  37. // Rate limiting
  38. if (rateLimit.containsKey(dirPath)) {
  39. long lastCheckTime = rateLimit.get(dirPath);
  40. long timeSinceLastCheck = now - lastCheckTime;
  41. if (timeSinceLastCheck < 1000 / maxChecksPerSecond) { // Allow checks at most 1/maxChecksPerSecond seconds apart
  42. return; // Skip check if rate limit is exceeded
  43. }
  44. }
  45. // Actual directory check
  46. File[] files = dir.listFiles();
  47. if (files != null) {
  48. for (File file : files) {
  49. if (file.isDirectory()) {
  50. checkDirectory(file); // Recursive check
  51. } else {
  52. System.out.println("File changed: " + file.getAbsolutePath()); //Example change detection
  53. }
  54. }
  55. } else {
  56. System.out.println("Directory changed: " + dir.getAbsolutePath());//Example change detection
  57. }
  58. // Update last checked timestamp and rate limit
  59. lastChecked.put(dirPath, now);
  60. rateLimit.put(dirPath, now);
  61. } catch (Exception e) {
  62. System.err.println("Error checking directory " + dir.getAbsolutePath() + ": " + e.getMessage());
  63. }
  64. }
  65. public static void main(String[] args) throws InterruptedException {
  66. DirectoryMonitor monitor = new DirectoryMonitor(5, 10); //Check every 5 seconds, limit to 10 checks/second
  67. monitor.start();
  68. Thread.sleep(60000); // Run for 60 seconds
  69. monitor.stop();
  70. }
  71. }

Add your comment