Java timer class, to fire at a fixed time everyday - java

I want to write a simple timer class that fires , and makes a method call at a fixed time of every day, while the application is running.
I dont want to use Quartz as I think its a overkill for this simple problem, what are the different approaches I can try for ?
Thanks in Advance

Why not use java.util.Timer and java.util.TimerTask?

The util.concurrent's ScheduledExecutorService allows you to pretty easily schedule tasks to run on a fixed delay. The signature of the schedule method is as follows:
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
You could find the difference between the current time and the time in which you wish to have it first run. Set the difference as the value for initialDelay. Set the period to 1 and the TimeUnit unit to TimeUnit.DAYS. That will cause it to run every day at that time.

Related

Running Java scheduler depends on clock time

I have a particular requirement on scheduler. I need to run a scheduler after every 30 minutes. This can be done easily but the problem is this scheduler is depends on clock time. Like suppose I have start my program at 00:15 then with start my scheduler will not start. First scheduler will run at 00:30 and from then it will run with 30 minutes interval.
Need help on the same. I am using Java 8.
Timer and TimerTask classes can be used.
Timer class contains a method schedule() in that you can pass your task(TimerTask).
Sigtnature of the method as follows :
public void schedule(TimerTask task,long delay,long period)
First parameter : TimerTask object
Second paramter : delay in millisecond, after the mentioned milliseconds task will start to execute.
Third parameter: period in millicond, subsequent executions will happen at regular intervals of mentioned period of time.
Refer to : https://docs.oracle.com/javase/8/docs/api/java/util/Timer.html
What you looking for is called cron sceduling it can give you the ability to run your job for example every Monday at 10 am or every 30 minute of every hour
here are some links
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html
How to create a Java cron job
https://www.mkyong.com/java/java-cron-job-to-run-a-jar-file/
This requirement is called crn job. The below cron settings needed to achieve the above requirement.
*/30 * * * *

How to stop/cancel a task in ScheduledExecutorService at particular time?

For example, I have a task which should be executed between 8:00-20:00 every minites every day.
so i calculate the time gap between 8:00 and the time which my app started for initialDelay, and use Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(command, initialDelay, 1, TimeUnit.MINUTE).
The question is, do I need the second timer to observe the task and cancel it when the clock comes to 20:00? or do I have to compare if the time is 20:00 at every time when the task's executed?
You probably need a real scheduler.
See Quartz https://quartz-scheduler.org
It should fit your needs
For the cron syntax : http://quartz-scheduler.org/api/2.2.0/org/quartz/CronTrigger.html
Or with a fluent API http://quartz-scheduler.org/api/2.2.0/org/quartz/TriggerBuilder.html

Start Multiple java TimerTasks at the same time

Is it possible to schedule multiple TimerTasks such that they all begin at the same time and if so how would I do it? In particular I would like to initiate these tasks at the same exact time so that the relative time difference between each task is as specified (I want to be as accurate as possible).
toneIntervalClock.scheduleAtFixedRate(tonePlayerTask, 250, 5000);
startRecordingClock.scheduleAtFixedRate(startRecordingTask,0,5000);
stopRecordingClock.scheduleAtFixedRate(stopRecordingTask, 1000, 5000);
Also would it be better practice to use the same Util Timer to schedule each task?
Thanks
The system will try to make scheduleAtFixedRate tasks fire with low jitter, but you don't ever get any guarantees with concurrency.
Your approach of trying to initialize all of the tasks and then run them is a decent one. The only suggestion I would make is to use Timer#scheduleAtFixedRate(TimerTask task, Date firstTime, long period), since then you don't have any jitter in between your schedule calls and can start them all from a fixed time reference.

check if the timer has any pending tasks?

I use the Java's Timer to schedule a task to run after some interval of time.
myTimer.schedule(myTask, delayTime);
At any point in time, is it possible to check if there is any task scheduled to be run (but has not run yet)? If so, how would I do that?
If not, what is the alternative(s) to Timer do I have?
You can (and should) use ScheduledExecutorService instead of Timer.
It handles thread crashes in a robust manner and has more flexible API
You can just add a boolean field to myTask's class, which will be set true at first execute.
Keep it simple.

Timer task does not run

I am using Timertask in my web application to launch a background thread once every 24 hrs every day at midnight. So I have a ServletContextListener and in contextInitialized, I create a Timertask object timertask(say) and a Timer object say t.
I call
t.schedule(timertask, firstTime.getTime(), rescheduleMiliSec);
where firstTime.getTime() = midnight and rescheduleMiliSec = 24 hr.
The thread launches fine and does what it is supposed to do in DIT.Every 24 hrs it launches the background task.
When it moves to PROD, the thread runs only once when context is initialised but not after that.
Is there any specific setting that might be the cause for this?
Is it possible your TimerTask implementation is throwing a RuntimeException?
If not an exception, then some TimerTask being scheduled in that Timer is blocking indefinitely. Those are the only two conditions that I am aware of that could cause a Timer to fail.
BTW, you might want to look into a ScheduledExecutorService. That is the more modern way of scheduling tasks.
I think the reason is simple but it may evade the naked eye.
firstTime.getTime()
is in milliseconds and the following method take precedence:
schedule(TimerTask task, long delay, long period)
insead of the intended:
schedule(TimerTask task, Date firstTime, long period)
In the contextInitialized method after scheduling a task using TimerTask , is there any code below that , may be that is causing an exception.

Categories