File watching utility written in Java?

B

Bomb Diggy

Hi,

I need a tool that will recognise new files in a directory (of an ftp
server) and then run some other program - say my java program which
will process the new file. Is there a shareware/freeware tool
available?

Thanks.
 
M

Marco Schmidt

Ahmed Moustafa:
Yes, but, the JVM will take a lot of the memory and the CPU,

A couple of MB. Not exactly a problem these days.
that's in
addition to the disk IO every second or something?

Checking a directory every couple of seconds is negligible.

However, there is a more elegant approach (see my other posting).

Regards,
Marco
 
M

Marco Schmidt

Bomb Diggy:
I need a tool that will recognise new files in a directory (of an ftp
server) and then run some other program - say my java program which
will process the new file. Is there a shareware/freeware tool
available?

Using JNI, you can use the Win32 API's notification service to be told
about directory content changes:
<http://groups.google.com/[email protected]>.

Regards,
Marco
 
A

Ahmed Moustafa

Implementing this functionality in *Java*, would not kill your machine!?
You can actually save a step; on most OS's, when a file is
created the timestamp of the directory is updated, so, if
all you want is new files, you can just have a thread that
poles the timestamp of the directory.

Is reading a directory timestamp considered a disk IO?
 
D

Drew Volpe

Last time we met said:
Yes, but, the JVM will take a lot of the memory and the CPU, that's in
addition to the disk IO every second or something?

No. If written in any reasonable manner, it should take ~1 MB of RAM
and have no noticeable CPU or disk hit; all you're doing is getting the
timestamp of 1 directory once a second. Nothing resource intensive about
that.


Let's test this...

To prove it to myself, I wrote the program below which does what the
user would like, except that it only lists the dir contents instead
of running a program on a file. When I run it on my machine, which
is slow by today's standards (400Mhz PII, 384 MB of RAM, 5400 rpm ide
hd), the program takes up 1.6 MB of RAM and the CPU for its process
never goes above 0.0. Even when I up the polling to 100 times a
second, the CPU goes to 0.19% every now and then, and then back to 0.0.

So, actually, Java seems like a good language for writing programs
like this. It took me ~2 minutes to code this up and, while the
RAM used is probably more than it would be in other languages,
1.6 MB is very reasonable.


---
import java.io.*;

public class TestPoller extends Thread {

public void run() {

File directory = new File("/home/drew/stuff/javatemp/testpoller");
long currentLastModified = 0;
while (true) {
if (directory.lastModified() > currentLastModified) {
currentLastModified = directory.lastModified();
String[] files = directory.list();
System.out.println("Directory contents changed. Now:");
for (int i=0; i < files.length; i++) {
System.out.println(" " + files);
}
}
try {
this.sleep(1000);
}
catch (InterruptedException e) {
System.out.println("Interrupted!");
}
}
}

public static void main(String[] args) throws Exception {
TestPoller tp = new TestPoller();
tp.setPriority(Thread.MIN_PRIORITY);
tp.start();

}
}
---


--
--------------------------------------------------------------------------
The geographical center of Boston is in Roxbury. Due north of the
center we find the South End. This is not to be confused with South
Boston which lies directly east from the South End. North of the South
End is East Boston and southwest of East Boston is the North End.

Drew Volpe, mylastname at hcs o harvard o edu
 
D

Drew Volpe

Last time we met said:
Is reading a directory timestamp considered a disk IO?

It has to read from disk, so yes. But it's only reading
a timestamp, so it's very little overhead.


dv

--
--------------------------------------------------------------------------
The geographical center of Boston is in Roxbury. Due north of the
center we find the South End. This is not to be confused with South
Boston which lies directly east from the South End. North of the South
End is East Boston and southwest of East Boston is the North End.

Drew Volpe, mylastname at hcs o harvard o edu
 
J

Jon A. Cruz

Drew said:
You can actually save a step; on most OS's, when a file is
created the timestamp of the directory is updated, so, if
all you want is new files, you can just have a thread that
poles the timestamp of the directory.

Actually... maybe not.

On all the Windows OS's it's also a function of the filesystem in use.
 
D

Drew Volpe

Last time we met said:
Actually... maybe not.

On all the Windows OS's it's also a function of the filesystem in use.

it seems to work for NTFS (w/ win2k). Which does it not work on ?


dv

--
--------------------------------------------------------------------------
The geographical center of Boston is in Roxbury. Due north of the
center we find the South End. This is not to be confused with South
Boston which lies directly east from the South End. North of the South
End is East Boston and southwest of East Boston is the North End.

Drew Volpe, mylastname at hcs o harvard o edu
 
M

Marco Schmidt

Drew Volpe:
It has to read from disk, so yes.

Not necessarily. The information may well be in the file system cache
in memory, so even that small I/O may not be necessary. In fact, after
the timestamp has been retrieved I'm relatively sure that it is kept
in memory and stays there, esp. given that it is queried so often.

Regards,
Marco
 
V

Van Glass

You could do this by routinely connecting to the FTP server and
comparing the list of files in the directory to the last time you
connected. Try using the FTP component included in iNet Factory and
available for download at http://www.jscape.com/inetfactory/index.html

In particular look at the Ftp#getDirListing method which returns an
Enumeration of FtpFile.
 
I

Ivan S Kirkpatrick

I wrote a java service called DeployServer that reads a database for a
set of directories to scan and specific files to look for. It reads
the timestamps adn compares it to the one in memory. If it is newer the
file is deployed (moved to a new location or deployed via 9ias and then
the timestamp updated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top