Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.nio.file.WatchEvent;
- import java.nio.file.WatchKey;
- import java.nio.file.WatchService;
- /**
- * Created by Ivan on 3/16/2016.
- */
- public class MyWatchQueueReader implements Runnable {
- /** the watchService that is passed in from above */
- private WatchService myWatcher;
- private CommunicationServer server;
- public MyWatchQueueReader(WatchService myWatcher, CommunicationServer server) {
- this.myWatcher = myWatcher;
- this.server = server;
- }
- /**
- * In order to implement a file watcher, we loop forever
- * ensuring requesting to take the next item from the file
- * watchers queue.
- */
- @Override
- public void run() {
- try {
- // get the first event before looping
- WatchKey key = myWatcher.take();
- while(key != null) {
- // we have a polled event, now we traverse it and
- // receive all the states from it
- for (WatchEvent event : key.pollEvents()) {
- System.out.printf("Received %s event for file: %s\n",
- event.kind(), event.context() );
- System.out.printf("Received\n");
- String line = "Received " + event.kind() + " event for file: " + event.context() + "\n";
- server.StartCommunicationg(line + "\n");
- System.out.printf(" Not Received\n");
- }
- key.reset();
- key = myWatcher.take();
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("Stopping thread");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement