Solved

FileMonitorNotifiers v Polling in Java

Posted on 2007-11-26
10
2,623 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 82
XML Paring  Error - Premature end of file. 7 55
word0 challenge 3 56
Java Jpanels and Jframe 8 20
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now