Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Timer issue

 
bryan lim
Ranch Hand
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47270
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Henry. it's working now!

THANK YOU ALL FOR YOUR ADVICES
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic