FileMonitorNotifiers v Polling in Java

I am looking for some suggestions.

I have a class called FileMonitorNotifer.

The run() inside the class is used to loop over the registered files and see which have changed. It uses the Modified DateTimeStamp to determine if there was a change.  

The code works fine. That is not the issue.

Basically, I run this java monitor process on an AIX box where the process runs to see if there are files in a specific directory, if there are 2 matching files in that dir it processes them, if not, it sleeps and runs again.

My issue happens after the sleep.
If two new files come into the directory at once while the java monitor process is a sleep they have the same DateTimeStamp.
When the run() gets to these 2 files and since the DTS has not changed the 2 files sit in this dir and I have to manually go in and touch the file for them  to process.

Is there a better option for this?
I have heard of polling, but can that work for my situation.
If so, can someone give me a head start, any code snippets or articles would help.

Thanks in advance. Annie


private class FileMonitorNotifier extends TimerTask
  {
    public void run()
    {
      // Loop over the registered files and see which have changed. 
      // Uses the Modifed DateTimeStamp to determine if there was a change.
      // Use a copy of the list in case listener wants to alter the
      // list within its fileChanged method.
        try
        {
            Collection files = new ArrayList (hmFiles.keySet());
     
            for (Iterator i = files.iterator(); i.hasNext(); )
            {
                File file = (File) i.next();
                long lastModifiedTime = ((Long) hmFiles.get (file)).longValue();
                long newModifiedTime  = file.exists() ? file.lastModified() : -1;
							
                // 	Check if file has changed
               if (newModifiedTime != lastModifiedTime)
               {
                    // 	Register new modified time
                    hmFiles.put(file, new Long (newModifiedTime));
 
                    // Notify listeners
                    for (Iterator j = colListeners.iterator(); j.hasNext(); ) 
                    {
                        WeakReference reference = (WeakReference) j.next();
                        FileListener listener = (FileListener) reference.get();
 
                        // Remove from list if the back-end object has been GC'd
                        if (listener == null)
                            j.remove();
                        else
                            listener.fileChanged (file);
                    }
               }
            }
        }
        catch(Exception ex)
        {
        	logError(ERROR_LEVEL_MIDLEVEL, ex);
        }
     }
  }

Open in new window

annie613Asked:
Who is Participating?
 
CEHJCommented:
Furthermore, you need to monitor the directory itself too most probably
0
 
CEHJCommented:
Sounds like you're not using it correctly. First of all there should be no explicit sleep at all - your monitor should run in java.util.Timer
0
 
annie613Author Commented:
I put a sleep in because the process was eating up CPU and paging space.

You said something about the Timer, Should I have an interval set up there and elimiate the Sleep?
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CEHJCommented:
>>Should I have an interval set up there and elimiate the Sleep?

Definitely. Doing that alone will probably not solve the problems though, for the reason i gave
0
 
objectsCommented:
you need to get a list of files in the loop. that way you can check for new file, deleted files, and changed files.

> You said something about the Timer, Should I have an interval set up there and elimiate the Sleep?

Having a sleep is fine (and desirable). A Timer won't change anything, what u have is fine.

0
 
annie613Author Commented:
At first I didnt have the SLEEP in the code. I ran the code as is. And it worked fine. All files were processed, however the monitor process was eating up CPU and paging space because it would never release any resources.

I thought by using the SLEEP for a few ms, that would work. It did because on my AIX box I get the idle time I need. However after the SLEEP if 2 new files come into the folder while the ps was sleeping, there is no DateTimeStamp change which doenst trigger the monitor to react.

I thought I did have the loop that was needed to check for the changed files.

So if I eleminate the SLEEP and use the polling interval to poll the directory and for files in that directory will that still run my CPU 100%
0
 
CEHJCommented:
There are two issues here:

a. efficient polling - use a Timer as i suggested
b. correct use of the monitor

>>if 2 new files come into the folder while the ps was sleeping, there is no DateTimeStamp change which doenst trigger the monitor to react.

It doesn't matter if it's sleeping or not. Any monitoring of the directory containing those files will register a change in it when it wakes up. Similarly, any List containing files from that directory will be different before and after that sleep
0
 
objectsCommented:
as I mentioned above to detect new file that occur during the sleep you need to get a list of file in the the directory and compare that to the list of files before the sleep.
and the sleep is necessary.

> I thought I did have the loop that was needed to check for the changed files.

you do, but it needs to also check for new (and possibly) deleted files as well.
0
 
annie613Author Commented:
I have changed the method to poll the dir and for files in the dir as well as comparing the dir before and after the sleep. The process is working now and the CPU and paging space are normal for processing.

Thanks! cheers!
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.