• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to run a java application every 30 seconds?.

 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the documentation of the class java.util.Timer.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want a non-java solution.

In such case, it would depend on the environment (Unix, Windows) you are in.
 
Malayathi Partha Saradhi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all of you for your replies.Got it.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic