Send Real-Time Alerts Example
This program demonstrates sending real-time alerts on file changes.
Problem Statement
Write a Java program that sends console alerts when files are modified using WatchService API.
Source Code
| 1 | import java.nio.file.*; |
| 2 | public class WatchServiceAlertExample { |
| 3 | public static void main(String[] args) throws Exception { |
| 4 | System.out.println("Eduinq WatchService Alert Example"); |
| 5 | WatchService ws=FileSystems.getDefault().newWatchService(); |
| 6 | Path path=Paths.get("C:/Eduinq"); |
| 7 | path.register(ws,StandardWatchEventKinds.ENTRY_MODIFY); |
| 8 | while(true){ |
| 9 | WatchKey key=ws.take(); |
| 10 | for(WatchEvent<?> event:key.pollEvents()){ |
| 11 | System.out.println("Eduinq ALERT: File Modified -> "+event.context()); |
| 12 | } |
| 13 | key.reset(); |
| 14 | } |
| 15 | } |
| 16 | } |
Program Output
Eduinq WatchService Alert Example Eduinq ALERT: File Modified -> data.txt
Explanation
Monitors modification events. - Sends immediate console alerts. - Exception handling ensures safe operations. - In enterprise systems, real-time alerts detect tampering instantly. Conclusion: Shows sending real-time alerts on file changes.