• Post Reply Bookmark Topic Watch Topic
  • New Topic

question about timer  RSS feed

 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
in my project,i want to execute some tasks in five o'clock every day,but i don't know how to write the timer program to activate the operation.who can give me some suggestion?
thank you very much!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sort of thing is a task better suited for the OS than a Java application. For UNIX and Linux you use cron and hack the crontab file, for NT kernel Windows boxes you use the AT command. If you really want to do it in Java, you will have to get the current system time when the app starts up, calculate the time before 5PM, sleep for that amount of time minus some insurance factor, wake up check the time sleep if necessary and finally fire the event.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree it's probably better to do in the OS. To do it in Java, you first need to know that the JVM will be up and running at 5 PM every day. That would require either (a) you set up some sort of startup script in the OS to start the JVM whenever the machine starts, or at a set time each day, or (b) you manually start the JVM, and just leave it on for many days at a time. If you do (a), well, you might as well just tell the OS to exectue the tasks, rather than execute a JVM which will execute the tasks. If you do (b) though - I recommend you check out java.util.Timer. It can handle a lot of the scheduling details for you.
[ July 26, 2003: Message edited by: Jim Yingst ]
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
my situation as follow:
my project reqire the intercommunication between multiple website,in the five o'clock,every sub-website must submit the data to the main site(by using the webservice).
i think the OS can't solve such problem,how do the cron and AT call the webservice,i think the better solution is to integrate the function in my project,maybe the "java.util.Timmer" is the good idea,let me try!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by zb cong:
thanks
my situation as follow:
my project reqire the intercommunication between multiple website,in the five o'clock,every sub-website must submit the data to the main site(by using the webservice).
i think the OS can't solve such problem,how do the cron and AT call the webservice,i think the better solution is to integrate the function in my project,maybe the "java.util.Timmer" is the good idea,let me try!

Cron could start a java application for you at 5:00 which would in turn communicate with the web service, that is assuming you have control of the main web server.
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Morris
i have tried timer class,it works well.
you said that the cron can call the webservice,would you like to tell me how?
other question:
if i write a java class that use the timer to execute the task,i want to set it as the windows service,when i start the windows,the service is started,but i don't know if a java class can be set as windows service?
thank you!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not knowing the full architechture of your application it is difficult for me to say how you would access the web service but URLConnection comes to mind.
For your second question, instead of starting the Java app as a service, just use javaw and start it as a daemon process. You can do that by hacking the registry or putting a batch file in the Startup menu. Here are the steps to have the app startup from the registry:
 
VIJAY Yadlapati
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can comfortably do with Timers. But you have to start the timer Application as windows starts.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!