Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Java Timer issue

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Marshal
Posts: 75700
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Henry. it's working now!

THANK YOU ALL FOR YOUR ADVICES
 
bacon. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic