This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Reding File from a remote server after every 2 minutes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reding File from a remote server after every 2 minutes" Watch "Reding File from a remote server after every 2 minutes" New topic
Author

Reding File from a remote server after every 2 minutes

Gary Dwivdi
Greenhorn

Joined: Sep 12, 2011
Posts: 9
hi,
I have a requirement where in i have to read a log file from a remote server every two minutes to check the status of the logs.
i have wriiten the code to read the file but m stuck on scheduling it.This code will further be used in Ant script.
Please help me with this.
Is there a way this scheduling can be done using ANT or in java itself???
TIA
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Hi Garima,

welcome to the Ranch... please check if you can use simple threading concept like below.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
That might be inefficient, keeping the JVM idle for two minutes at a time (120000 rather than 2000 milliseconds), but I am not sure. You can try a Timer (probably the java.util sort). There are scheduling applications, eg cron on *nix, and there is something similar on Windows®.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Or why not try out the java.util.concurrent.ScheduledExecutorService...


http://download.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html
Gary Dwivdi
Greenhorn

Joined: Sep 12, 2011
Posts: 9
Thanks a ton guys for the pointers . ..But since i'm calling this class thru an ANT script. can i have something in ANT -any task in ANT which can be used for this.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Garima Dubey wrote:Thanks a ton guys for the pointers . ..But since i'm calling this class thru an ANT script. can i have something in ANT -any task in ANT which can be used for this.


IMHO, that is not the way Ant should be used. Ant is, sort of, a scripting language that should be used to run a task from point A to point B. Having an Ant-script that calls a Java program every 2 minutes is to use the wrong tool for the wrong job. Ok to start your program with Ant, but not trigger it every 2 minutes. Then I would use a cron job or actually have the program just running and execute a worker every 120 seconds.

But there are tasks to do what you want. And since Ant is extended using Java... all they do is setting up a scheduler of some sort (like the one from the Executor framework) and then call the class that needs to be executed. Sometimes, that is the only thing you can do, if you do not have any other control of the code.
Gary Dwivdi
Greenhorn

Joined: Sep 12, 2011
Posts: 9
@thanks a ton for your suggestions....
I scheduled it by setting up a flag which tests for a codn and if the condn is true ...the log file is read again after a time interval of 2 min ...by using Wait(n sec)..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reding File from a remote server after every 2 minutes