• Post Reply Bookmark Topic Watch Topic
  • New Topic

Watch Service API Issue  RSS feed

 
Sangel Kapoor
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are watching Directory for changes by using WatchService API. The issue we are getting is as follows

What we are Doing ?

We copy file from a certain location to Directory we are watching.
When we get CREATE event, we try to read the file and load its contents into memory.

Issue :

We get an Exception saying the file is used by another process.

What we think and Tried to resolve this ?
We think CREATE event is generated prior to the completion of "Copy Operation" by OS. Due to this fact, when our class tries to Load the contents we get an exception.
We tried sleeping our Loader Thread and things get resolved. It is workig, seems like our assumptions are true.

What we want to know

We dont want to sleep the thread, since we dont know how much time would it take for Copy Operation by OS to complete.
Is there any other better way for doing this ??


 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if FileChannel.open() will throw an exception if you try to use it while the file is being used, but if it doesn't, you can acquire a lock() on the file after the watcher notifies you. This method will block until the copy operation is done, and afterwards you can do with the file whatever you want.
 
Sangel Kapoor
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, Thanks for nice Suggestion !!! -)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!