aspose file tools*
The moose likes Java in General and the fly likes How to run a java application every 30 seconds?. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to run a java application every 30 seconds?." Watch "How to run a java application every 30 seconds?." New topic
Author

How to run a java application every 30 seconds?.

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
I want to run a java program that checks for a file every 30 seconds?.It should check for the file every 30 seconds.Can anybody suggest me some ideas?.



Regards,
Nik
vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
Originally posted by Nik Arora:
Hi All,
I want to run a java program that checks for a file every 30 seconds?.It should check for the file every 30 seconds.Can anybody suggest me some ideas?.



Regards,
Nik

hi nick

if you have already written your program add your code in a thread that thread will look for your file and gets sleep for 30 seconds and then again the whole process will start all over again.


i don't think there is a way to make a program that will get executed after every 30 minutes without a thread.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Have a look at the documentation of the class java.util.Timer.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi,
Thanks both of you for your replies.Can you give me a example on the usage of timer?.



Regards,
Nik
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
You may want a non-java solution.

In such case, it would depend on the environment (Unix, Windows) you are in.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Malayathi Partha Saradhi
Greenhorn

Joined: Nov 30, 2007
Posts: 10
Hello,

Your java program,if it is a servlet,we can refresh page by using response headers.

response.setHeader("refresh",30);

response indicates HttpServletResponse.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Thanks all of you for your replies.Got it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Originally posted by Nik Arora:
Hi,
Thanks both of you for your replies.Can you give me a example on the usage of timer?.

http://www.exampledepot.com/egs/java.util/ScheduleRepeat.html

Search on Google for "java timer example" and you'll find more.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Originally posted by Jesper Young:

http://www.exampledepot.com/egs/java.util/ScheduleRepeat.html

Search on Google for "java timer example" and you'll find more.


Thanks Jesper
Amit Bathla
Ranch Hand

Joined: Feb 11, 2005
Posts: 51
Create one thread and call your procedure in which you are checking for a file in that thread..

then use Thread.Sleep(time in mili second)

and you are done


Amit
University of Florida
Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
Cleaner way would be use newScheduledThreadPool of Java 1.5 concurrent package, if you are anyhow using java 1.5 else other suggestions can very well solve your problem.


Chop your own wood, and it will warm you twice. - Henry Ford
Kartik Patel
Ranch Hand

Joined: Sep 12, 2005
Posts: 73
Cleaner way would be use newScheduledThreadPool of Java 1.5 concurrent package, if you are anyhow using java 1.5 else other suggestions can very well solve your problem.
Lukasz Bajzel
Greenhorn

Joined: Dec 03, 2007
Posts: 26
You can schedule jobs in Java using the java.util.Timer and java.util.TimerTask classes.

Lets say you have a TimerTask class

private class MyTimerTask extends TimerTask{
.....
public void run(){...}
}

then you can use code similar to
new Timer().scheduleAtFixedRate(new MyTimerTask(), delay_in_starting,1000*24*60*60);

Where delay_in_starting is the the time in milliseconds before the task starts and 1000*24*60*60 represents 24 hours (means the task will run once in every 24 hours)

The above code is just to show how to use these classes to achieve what you want. You may elaborate on that..

Hope this helps!

Sincerly,
Your friends at www.javaadvice.com
www.javaadvice.com - The one stop resource for all your Java questions and answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to run a java application every 30 seconds?.
 
Similar Threads
reading data from xml file asynchronously
reading data from xml file asynchronously
Refresh image
how to insert data in db some time interval.
reading data from xml file asynchronously