I am trying to find a way to run a method at a specific time set by different users, let me explain!
Let's suppose we have 2 sites: siteA and siteB and those sites have admins: adminA and adminB respectively.
Each admin can create a work schedule in which the rabbitmq queues in his site are launched.
Right now, each admin launch his queues manually.
What i want is, lets say for exemple:
adminA created a work schedule from 08:00 to 18:00
adminB created a work schedule from 09:00 to 17:30
I want the method that launches a site queues to be executed at the time specified by the admin of that site so :
queueA1, queueA2, queueA3... launched at 08:00
queueB1, queueB2... launched at 09:30
lets suppose the method called launchQueues(String siteId)
I have learned about #Scheduled but it seems like it is only applicable when i want to call a method in a fixed time
You can have a common job which is scheduled to run every n minutes. This job can find any queue that need to be launched at that time interval and then launch it.
e.g. This job is scheduled for every 5 mins.
At 9:00 am, findQueuesToLaunch() method will find the queues that need to be launched at 9:00 am and not already running.
#Scheduled(cron = "0 0/5 * * * ?")
public void launchQueue() {
List<String> queues = findQueuesToLaunch();
for (String queueId in queues) {
launchQueue(queueId);
}
}
Related
I'm using Quartz Scheduler in Java to schedule jobs based on entries in a database. I'm trying to automate disabling those entries when they will never run again, so I don't try to read them every time the application starts.
I have a cron for a very specific date, for example 0 25 12 4 DEC ? 2021. That is a specific date, it has passed, and cannot ever fire again.
I build a chrontrigger and check endTrigger.getFinalFireTime() and it returns null, which I believe should only happen when the cron allows future firing.
// Build the end cron trigger
CronTrigger endTrigger = TriggerBuilder.newTrigger()
.withIdentity("eventEndCron", "quartzGroup")
.withSchedule(CronScheduleBuilder.cronSchedule("0 25 12 4 DEC ? 2021"))
.forJob("eventEnd", "quartzGroup")
.build();
If I try to schedule the job, it throws an error Based on configured schedule, the given trigger 'quartzGroup.eventStartCron' will never fire.
Why isn't getFinalFireTime giving me a date? Is there some other approach I need to use?
I have a spring boot application in which I am trying to schedule a job using quartz scheduler to run daily at a specific time of the day. The following is my code to build the trigger.
DailyTimeIntervalScheduleBuilder scheduleBuilder = DailyTimeIntervalScheduleBuilder
.dailyTimeIntervalSchedule()
.startingDailyAt(TimeOfDay.hourAndMinuteFromDate(activeStartTime))
.endingDailyAfterCount(1)
.withMisfireHandlingInstructionFireAndProceed();
MutableTrigger trigger = scheduleBuilder.build();
The problem I am facing is that the job is scheduled but starting from the next day. So for instance, if I schedule the job for May 22 16:45, then the first fire time for the job is set to May 23 16:45.
I have tried using the builder with withIntervalInHours(24) instead of endingDailyAfterCount(1), but the result is the same.
I am not sure what seems to be the problem.
Note: This behavior is the same regardless of when I schedule my job, i.e., it doesn't matter if I execute this code before or after 16:45, the job is always scheduled for the next day
I am using spring boot version 1.5.10 and spring-boot-starter-quartz version 2.2.5.RELEASE
Can you try below code
CalendarIntervalScheduleBuilder schedule = CalendarIntervalScheduleBuilder
.calendarIntervalSchedule()
.inTimeZone(TimeZone.getDefault())
.withIntervalInDays((int) 1)
.withMisfireHandlingInstructionFireAndProceed();
Trigger trigger = TriggerBuilder
.newTrigger()
.startAt(startDateTime)
.withSchedule(schedule).build();
For the field startDateTime please use current Date time. if you want to start from May 22 16:45 then create the Date object accordingly.
And set the timezone also, or it will pick default system's timezone.
I have a Spring Boot app which has a scheduler that insert data to a remote database at 2 a.m. every day.
#Scheduled(cron = "0 0 2 * * ?")
public void reportDataToDB() {
// code omitted
}
The problem is, the app runs on multiple machines, so the database would receive multiple duplicate insertions of data.
What is the idiomatic way to solve this?
We solved such a problem by using a central scheduler. In our case we use Rundeck, which then calls a URL on our service (by going through the loadbalancer), which then executes the task (in our case data cleanup). This way you can make sure, that the logic is only executed on one instance of the service.
I have some repeating quartz jobs that use SimpleScheduleBuilder.repeatMinutelyForever(60). Lets say the job initially gets scheduled at 10:02am and runs for 5 minutes completing at 10:07am.
Right now it is getting scheduled to run again at 11:02am but I want it to run again 60 minutes after completing so it should be scheduled at 11:07am instead.
Is there any way to change the scheduling to do this? Or should I use a one time job that creates a new job each time it completes?
Use TriggerListener - see here for some examples in Quartz cookbook.
You will override triggerComplete and in there you will add your rescheduling code:
public void triggerComplete(Trigger trigger, JobExecutionContext context, CompletedExecutionInstruction triggerInstructionCode) {
// check here the triggerInstructionCode value and reschedule your job
super.triggerComplete(trigger, context, triggerInstructionCode);
}
Example for registering a TriggerListener with the scheduler to listen to a specific trigger:
scheduler.getListenerManager().addTriggerListener(myTriggerListener, keyEquals(triggerKey("myTriggerName", "myTriggerGroup")));
See also this answer.
I need to send Notification Messages to the registered customers, by using the mail service we need to send mail everyday at 5AM by calculating the time automatically. We have mail service code completely. But how to execute one job at specified time everyday.
You can use spring scheduler:
#Scheduler(cron="0 0 5 * * *")
public void emailJob() {
// your code
}
also need the annotation on class level: #EnableScheduling