Solved

FileMonitorNotifiers v Polling in Java

Posted on 2007-11-26
10
2,626 Views
Last Modified: 2008-02-01
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

0
Comment
Question by:annie613
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20350348
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 65 total points
ID: 20350408
Furthermore, you need to monitor the directory itself too most probably
0
 

Author Comment

by:annie613
ID: 20350433
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 86

Expert Comment

by:CEHJ
ID: 20352095
>>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
 
LVL 92

Expert Comment

by:objects
ID: 20353585
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
 

Author Comment

by:annie613
ID: 20358931
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20360029
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
 
LVL 92

Assisted Solution

by:objects
objects earned 65 total points
ID: 20361330
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
 

Author Comment

by:annie613
ID: 20365345
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20365548
:-)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question