TimerTask as Filewatcher.

S

smartnhandsome

Hi All,
I want to use TimerTask to watch for a file in a particular folder.
and proceed to another line of code only after i get the file in a
particular folder. The TimerTask does it well but a seperate thread i
guess I want to

new FileWatcher("c://temp3//",file,1000);
do this line1;
do this line2;

I want to do this line1 and do this line2 only after i get the file
i.e. I want to return from watchFile method only after get the file.
This code now starts to watch for a file as a seperate thread so the
do this line1 do this line2 also Execute while the filewatcher class
is still looking for the class.

Any Suggestions. Any Help Is appreciated.



public class FileWatcher {

Timer timer;

private String fileToWatch;

private String directorytoWatch;

private int timeDelay;

public FileWatcher(String directorytoWatch, String file, int
timeInterval) {
this.fileToWatch = file;
this.directorytoWatch = directorytoWatch;
this.timeDelay = timeInterval;
timer = new Timer();
TimerTask timerTask =new FileWatcherTask();

timer.schedule(timerTask, 0, // initial delay
1 * timeDelay); // subsequent rate




}



class FileWatcherTask extends TimerTask {

public void run() {
File theDirectory = new File(directorytoWatch);
File[] children = theDirectory.listFiles();

// Store all the current files and their timestamps
for (int i = 0; i < children.length; i++) {

File file = children;
if (file.getName().equals(fileToWatch)) {
System.out.println("File Found");
System.exit(0);
} else {
System.out.println("File " + fileToWatch+ " not found yet");
}

}
theDirectory = null;
children = null;
System.gc();
}
}

}
 
K

Knute Johnson

smartnhandsome said:
Hi All,
I want to use TimerTask to watch for a file in a particular folder.
and proceed to another line of code only after i get the file in a
particular folder. The TimerTask does it well but a seperate thread i
guess I want to

new FileWatcher("c://temp3//",file,1000);
do this line1;
do this line2;

I want to do this line1 and do this line2 only after i get the file
i.e. I want to return from watchFile method only after get the file.
This code now starts to watch for a file as a seperate thread so the
do this line1 do this line2 also Execute while the filewatcher class
is still looking for the class.

Any Suggestions. Any Help Is appreciated.



public class FileWatcher {

Timer timer;

private String fileToWatch;

private String directorytoWatch;

private int timeDelay;

public FileWatcher(String directorytoWatch, String file, int
timeInterval) {
this.fileToWatch = file;
this.directorytoWatch = directorytoWatch;
this.timeDelay = timeInterval;
timer = new Timer();
TimerTask timerTask =new FileWatcherTask();

timer.schedule(timerTask, 0, // initial delay
1 * timeDelay); // subsequent rate




}



class FileWatcherTask extends TimerTask {

public void run() {
File theDirectory = new File(directorytoWatch);
File[] children = theDirectory.listFiles();

// Store all the current files and their timestamps
for (int i = 0; i < children.length; i++) {

File file = children;
if (file.getName().equals(fileToWatch)) {
System.out.println("File Found");
System.exit(0);
} else {
System.out.println("File " + fileToWatch+ " not found yet");
}

}
theDirectory = null;
children = null;
System.gc();
}
}

}


import java.io.*;
import java.util.*;

public class test {
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
File f = new File("c://temp3//");
if (f.exists()) {
cancel(); // so it doesn't do it again
// do your thing here
}
}
};
timer.schedule(task,1000,1000); // try every second
}
}
 
L

Lew

Knute said:
import java.io.*;
import java.util.*;

public class test {
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
File f = new File("c://temp3//");
if (f.exists()) {
cancel(); // so it doesn't do it again
// do your thing here
}
}
};
timer.schedule(task,1000,1000); // try every second
}
}

Now, finding out if that file not only exists but is complete, that's another
issue. You could find the file and another process might still be writing to
it.
 
K

Knute Johnson

Lew said:
Knute said:
import java.io.*;
import java.util.*;

public class test {
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
File f = new File("c://temp3//");
if (f.exists()) {
cancel(); // so it doesn't do it again
// do your thing here
}
}
};
timer.schedule(task,1000,1000); // try every second
}
}

Now, finding out if that file not only exists but is complete, that's
another issue. You could find the file and another process might still
be writing to it.

See the "//do your thing" comment above :).
 
M

Martin Gregorie

Lew said:
Now, finding out if that file not only exists but is complete, that's
another issue. You could find the file and another process might still
be writing to it.
Shouldn't be a problem provided that the writer completes the file,
closes it and then renames it to something that the reader is expecting.

We used that approach in a production system where files were delivered
by FTP and renamed when the PUT operation completed. It worked fine: we
had no problems with incomplete files.
 
R

Roedy Green

I want to do this line1 and do this line2 only after i get the file
i.e. I want to return from watchFile method only after get the file.
This code now starts to watch for a file as a seperate thread so the
do this line1 do this line2 also Execute while the filewatcher class
is still looking for the class.

You would have to sleep until you found what you wanted.
see http://mindprod.com/jgloss/sleep.html

A more Javaesque solution would be to pass a delegate object to your
watcher. The watcher calls a method in the delegate object every time
it finds the requested file.
http://mindprod.com/jgloss/callback.html
 
S

smartnhandsome

Thanks every one for there replies, but the code still works like the
same way as I had written, when we create a TimerTask and override the
run method and pass this to timer it creates a new thread and then
pass back the control to the main thread, so the main thread still
keeps doing next steps while the timer thread that was created earlier
would run back ground. I wanted my code (main thread ) to wait till
timer task thread finishes its file watch. How can this be achieved??

Thanks again for every ones posts.
 
K

Knute Johnson

smartnhandsome said:
Thanks every one for there replies, but the code still works like the
same way as I had written, when we create a TimerTask and override the
run method and pass this to timer it creates a new thread and then
pass back the control to the main thread, so the main thread still
keeps doing next steps while the timer thread that was created earlier
would run back ground. I wanted my code (main thread ) to wait till
timer task thread finishes its file watch. How can this be achieved??

Thanks again for every ones posts.

do {
File f = new File("????");
if (f.exists())
break;
try {
Thread.sleep(1000);
} catch (InterruptedException ie) { }
} while (true) ;

But if the file never shows up your program never runs again. I would
use this instead of a TimerTask, it is inline and simpler if you want to
halt your program until the file arrives.

If you insist on a TimerTask;

use a boolean flag and a wait object

boolean flag;
final Object o = new Object();

// main thread
synchronized (o) {
while (!flag)
o.wait();
flag = false;
}

// TimerTask...
if (f.exists())
synchronized (o) {
flag = true;
o.notify();
}
 
D

Daniel Pitts

Lew said:
Knute said:
import java.io.*;
import java.util.*;

public class test {
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
File f = new File("c://temp3//");
if (f.exists()) {
cancel(); // so it doesn't do it again
// do your thing here
}
}
};
timer.schedule(task,1000,1000); // try every second
}
}

Now, finding out if that file not only exists but is complete, that's
another issue. You could find the file and another process might still
be writing to it.
Generally, most OSes have a way to rename a file as an atomic operation.
Doing this allows you to create the file under a different name, fill
it appropriately, and then rename it to what its supposed to. Barring
that, you should obtain a lock on the file (not sure Java supports that).
 
S

smartnhandsome

Knute
Thanks for your post i tried using your suggestion on synchronized
block with TimerTask but i was getting Illegal State Exception. I did
not understand why you were passing Object o into the block?.But your
simple inline sleep code was working fine. Thanks again.
 
K

Knute Johnson

smartnhandsome said:
Knute
Thanks for your post i tried using your suggestion on synchronized
block with TimerTask but i was getting Illegal State Exception. I did
not understand why you were passing Object o into the block?.But your
simple inline sleep code was working fine. Thanks again.

I'm a firm believer in the KISS method.

The Object o is just so that you can get a lock from a common object.
The lock is used to synchronize the actions. Thread interactions are
very complex and a science into themselves.
 

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

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,081
Latest member
AnyaMerry

Latest Threads

Top