wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java Timer issue 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 » Beginning Java
Bookmark "Java Timer issue" Watch "Java Timer issue" New topic
Author

Java Timer issue

bryan lim
Ranch Hand

Joined: Dec 26, 2008
Posts: 140
Hi all,

i created a timer and schedule to do a task at a specific timing each day. i got the code here and for easy referencing i copy the code below:

myquestion:let's say i set the timer at 8am daily to send an email to my mailing list.

and now, if something were to happen after 8am to cause me to close my application. Once i restart my application it will trigger the timer again, which will cause the application to send the mail to everyone in my mailing list even thought the task has been perform early in the morning.........how do i fix this problem?


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Are you sure it will fetch mail twice? Try it with a toy implementation like what you have here, and alter the times so it starts in 5 minutes, then stop and restart it.
Naga Niranjan
Greenhorn

Joined: Aug 02, 2005
Posts: 25
Check the current time with the specified time in getTomorrowMorning4am().
If the current time is after the specified time, then schedule your task to execute from next day onwards.
bryan lim
Ranch Hand

Joined: Dec 26, 2008
Posts: 140
Thanks! I see. Because I currently setting fONE_DAY = 0 which represent sending it today.

by setting it to 1 (sending the next day), it will always send the next day. that can be a solution. but is there a timer fix if the fONE_DAY = 0?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40


You also have a minor issue -- you assume that the first run will always be 4am tomorrow. If the application is restarted at 2am today, the next run will be 26 hours later.

A better solution would be to check current time with 4am today. If the current time is later, then use 4am tomorrow. Otherwise, use 4am today.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

but is there a timer fix if the fONE_DAY = 0?


There is nothing to fix. Basically, when you specify an initial time that is earlier than the current time, the timer just assumes that you want it to run ASAP, as you already past the specified time.

Henry
bryan lim
Ranch Hand

Joined: Dec 26, 2008
Posts: 140
thanks Henry. it's working now!

THANK YOU ALL FOR YOUR ADVICES
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Timer issue