Page 1 of 1

About Question enthuware.ocpjp.v7.2.1673 :

Posted: Thu Oct 17, 2013 4:21 pm
by mobby001
The question is...

Code: Select all

Given the following code for monitoring a directory:

    Path path = Paths.get(".");
    WatchService watchService = FileSystems.getDefault().newWatchService();
    path.register(watchService, 
                           StandardWatchEventKinds.ENTRY_CREATE,
                           StandardWatchEventKinds.ENTRY_MODIFY,
                           StandardWatchEventKinds.ENTRY_DELETE);

    while(true) {
        WatchKey key = watchService.take();
        System.out.println(key.isValid());
        for (WatchEvent<?> watchEvent : key.pollEvents()) {
            Kind<?> kind = watchEvent.kind();
            System.out.println(kind);
        }
    }

A file is created and then deleted from the monitored directory. How many events will be printed by the above code?
The provided answer is - 1, given the explanation...
Once you retrieve a key from WatchService, you can't get further events until you call key.reset().
In this case, the code will get the create event but since it does not call reset() on the key before repeating the loop, it will not receive the delete event.
In practice, certainly on my machine at least (Ubuntu), when a new file is created I get both an ENTRY_CREATE and an ENTRY_MODIFY. This may differ on Windows and I suspect the events you receive would be platform dependent as file operations will behave differently.

Am I right or have I missed something?

Thanks for reading and for any help you may be able to provide.

Re: About Question enthuware.ocpjp.v7.2.1673 :

Posted: Fri Oct 18, 2013 6:45 am
by admin
As per the JavaDoc API documentations, it says,
Once signalled, a key remains in this state until its reset method is invoked to return the key to the ready state. Events detected while the key is in the signalled state are queued but do not cause the key to be re-queued for retrieval from the watch service.
So ideally, events that happen after the key is in signalled state but before it is reset, should not be available from the key.
I just tested the code on windows and I don't get the DELETE event until I call reset on the key. I get only CREATE event upon file creation.

Linux is probably generating two notifications CREATE and MODIFY for the same action (i.e. creating a file) and that is probably why you are getting both of them without reset. But are you getting DELETE also without reset?

HTH,
Paul.
.

Re: About Question enthuware.ocpjp.v7.2.1673 :

Posted: Fri Oct 18, 2013 8:17 am
by mobby001
Thank you Paul for your response.

I'll have to update my test code to automate the creation and deletion of the file, I'll get back to you with the results. I suspect that the question as it stands may be ambiguous as certainly you might receive 1 or 2 events on the file creation, depending on the platform.

Re: About Question enthuware.ocpjp.v7.2.1673 :

Posted: Fri Oct 18, 2013 8:17 am
by mobby001
Thank you Paul for your response.

I'll have to update my test code to automate the creation and deletion of the file, I'll get back to you with the results. I suspect that the question as it stands may be ambiguous as certainly you might receive 1 or 2 events on the file creation, depending on the platform.

Re: About Question enthuware.ocpjp.v7.2.1673 :

Posted: Fri Oct 18, 2013 11:16 am
by mobby001
Ok so I can confirm that I don't see the ENTRY_DELETE if the event happens after the "take" and the key isn't reset.

Something interesting is that if the file is created using Java you get only one event, CREATE. If you create the file on the command line (touch file.txt) then you get two, CREATE and MODIFY.

HTH

Cheers,
Alex

Re: About Question enthuware.ocpjp.v7.2.1673 :

Posted: Sun Nov 15, 2015 7:29 am
by Jimothy
Another linux user here, I get the output:

true
ENTRY_CREATE
ENTRY_MODIFY

So I don't think this type of question would be on the exam as it is very filesystem implementation dependent.

Question probably needs an update to test the understanding of what happens if you don't call key.reset().