Java timer that prevents shutdown if and only if tasks are queued
I have a java.util.Timer that I use for throttling email sends (if a
status email has been sent too recently, then don't send one now but
instead create a timer task to send it later with any new statuses).
What I would like is to ensure any queued email is eventually sent, but
that if none are queued then the program can shut down normally.
Unfortunately I'm unable to decide how to go about it:
If I leave the Timer in default (non-daemon) mode, then it will keep the
process from exiting until it's explicitly cancelled.
If I set the Timer to daemon mode, then it will allow shutdown, but any
queued tasks (i.e. unsent emails) will be abandoned.
What I've done so far is to explicitly cancel the Timer (and let it be
garbage collected) after the email is sent, and then recreate a new Timer
when needed for future delayed emails. This seems like needless churn.
Is there a way to control the Timer's daemon state? Or an alternative to
Timers that would achieve this delayed email scheme?
No comments:
Post a Comment