Solved

FileMonitorNotifiers v Polling in Java

Posted on 2007-11-26
10
2,630 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 39
throw exception 21 69
How to configure empty element in XML Document parser? 15 45
restrict decimal places for double datatype 10 34
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

726 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