Issue
I am working on automating a task in java that is being run manually 2 times per business day, at fixed times. This task has to check in database if it can run (if some other task is running, it cannot start). I've exported the task into a jar file and created a new eclipse workspace in which the tasks would run, but i could not find a good solution to this problem, mostly because of this task running 2 times per day, with fixed times (7am, 1pm) and it has to check in database for the "green light".
I know that there are solutions as Timer, ScheduledExecutorService and external scheduling service Quartz. I have looked into these solutions, but i dont think they really fit my problem.
I dont wanna end up reinveting the wheel with creating a timer from scratch so I was wondering if somebody has had experience with scheduling tasks the same way i need.
I imagine the solution would look like this:
Task task7am = new Task();
Task task1pm = new Task();
// 1st parameter : which days of week it should run
// 2nd parameter : which hour of day it should run
task7am.schedule("1 2 3 4 5", 7);
task1pm.schedule("1 2 3 4 5", 13);
SomeTaskScheduleService service = new SomeTaskScheduleService();
service.add(task7am);
service.add(task1pm);
service.start();
The Task class:
class Task extends TaskServiceXXX{
@Override
public void run () {
//check if it can run
while(!DbUtil.canRunTask()){
sleep();
}
doTheTask();
}
private void sleep(){
Thread.sleep(1000*60*5); //wait 5 minutes
}
private void doTheTask(){
//the task, which needs to be done
}
}
If you could help, even by pointing to documentation i would be really grateful. Thank you
Solution
After a bit of researching, I found Quartz to be the most useful in my situation (spring was a bit overkill). The biggest downside to Quartz tho, is that this library is not mantained anymore (so no support).
Answered By - Johnys Answer Checked By - Katrina (WPSolving Volunteer)