About Question enthuware.ocpjp.v7.2.1673 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
mobby001
Posts: 4
Joined: Thu Oct 17, 2013 3:42 pm
Contact:

About Question enthuware.ocpjp.v7.2.1673 :

Post 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.

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
.
If you like our products and services, please help us by posting your review here.

mobby001
Posts: 4
Joined: Thu Oct 17, 2013 3:42 pm
Contact:

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

Post 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.

mobby001
Posts: 4
Joined: Thu Oct 17, 2013 3:42 pm
Contact:

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

Post 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.

mobby001
Posts: 4
Joined: Thu Oct 17, 2013 3:42 pm
Contact:

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

Post 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

Jimothy
Posts: 2
Joined: Sat Oct 31, 2015 4:19 am
Contact:

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

Post 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().

Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests