Spring - schedule task in some period of time - java

Currently I'm using Spring annotation to schedule my task every 20 minutes:
#Scheduled(fixedRate = 1200000)
I want to change schedule that my task will execute only between 2AM and 5AM (still every 20 minutes).
Is it possible with Spring Scheduler? Or are there any best practices to solve this problem?
Thanks

#Scheduled has 'cron' element so you can use full power of cron

#Scheduled(cron = "0 0/20 2-5 * * ?")
0/20 -> every 20mins
2-5 -> between 2 to 5 AM

Related

Quartz Expression that runs every minute only once

I have this quartz cron expression: exp = "0 * * ? * *" that runs every minute.
I use this expression as a trigger to call a HTTP POST method every minute.
The problem is that it calls the post method multiple times.
I need an expression that will execute one time in a minute.
Any suggestion how to do that?
This works with Quartz.
Every 1 minute, exactly at the start of a minute - 0 0/1 * * * ?
How about a simple schedule trigger instead?
trigger = newTrigger()
.withIdentity("trigger7", "group1")
.withSchedule(simpleSchedule()
.withIntervalInMinutes(1) )
.build();
Source : Quartz tutorial-lesson-05 / Build a trigger that will fire now, then repeat every five minutes
It may happen because request lasts longer than 1 min. You can simply annotate class implemented with Jobs with #DisallowConcurrentExecution, which disallow your job execute multiple times in exact time.
You can check example .
use this expression :
"* * * * *"
i.e 5 asteriks
try using that.
use this link for reference :
https://crontab.guru/every-1-minute

How to run Quarz Cron Job between specific time?

I want to execute cron job between specific time. i.e. Cron job should execute every 15 minutes between 7:03 to 21:04?
I can manage between 7 to 9 but can't schedule between 7:03 to 9:05.
After understanding your requirement clearly, AFAIK, you need two schedulers:
First Scheduler (runs between hours 07:00 to 21:00):
Cron expression should be like 0 3/15 7-20 * * *
0 - seconds
3/15 - runs at 3rd, 18th, 33rd, 48th minutes of each hour
7-20 - starting from 07AM to 08PM (included)
Second Scheduler (runs ONLY at 21:04):
Cron expression should be like 0 4 21 * * * (which runs ONLY at 21:04)
Try with below cron expression
0 3/15 7-21 * * ?
If you need it to run daily, you can try to use DailyTimeIntervalTrigger.
There you can set the start time (7:03) and end time (21:04) and choose the interval you want (in seconds, minutes, hours...)

Quartz Scheduler Cron Expression with frequency as specific hour and minute

I am trying to build a cron expression using quartz in java. I get two parameters as hour and minute with which I have to schedule a job every hour and minute.
Till now I have tried this :
Example 1:-
Schedule job to run every 1 hour 10 minutes.
- I used cron expression for this example as "0 */10 */1 * * ?".
- But this job runs every 10th minute and not as 1 hour and 10 minutes.
Can anyone help me understand why this expression is not working ?
Thanks in advance.
Your cron expression will run every 10 minutes because that's what the first */10 means. The second */1 is redundant because it'll run anyway due to the first */10.
It's not clear to me what you're trying to do - recurring schedules can be tricky to express clearly, so I tend to write down a few examples and work from there.
Are you trying to get a pattern like:
01:10
02:10
03:10
If so, I think 0 10 * * * ? should do the job. The documentation I used to understand the expression is at http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06.html and I also found http://www.cronmaker.com/ to be really helpful for sanity checking the expression - it'll work out the next few trigger times for you.
To achieve firing at a fixed rate every 1:10, i.e.
1:10
2:20
3:30
try the SimpleTrigger with a 1:10 interval.

Custom #Scheduled Spring

I have a method doJob() runs at every 10 seconds like below.
I want to customize the running period at run time. I mean , between 2pm-3pm doJob() runs every 5 seconds. between 3pm-4pm doJob() runs at every 50 seconds.
Is is possible to customize running period at run time?
#Scheduled(cron = "0/10 * * * * ?")
public void doJob() {
}
In this article http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06
Note that some scheduling requirements are too complicated to express with a single trigger - such as "every 5 minutes between 9:00 am and 10:00 am, and every 20 minutes between 1:00 pm and 10:00 pm". The solution in this scenario is to simply create two triggers, and register both of them to run the same job.
You won't be able to use the same jobs but certainly two different jobs
Job 1 (between 2pm-3pm runs every 5 seconds)
0/5 * 2-3 * * ?
Job 2 (between 3pm-4pm runs at every 50 seconds)
0/50 * 3-4 * * ?

Quartz for iregular interval of hours

I am using Quartz library to schedule Thread (using Jobs).
My boss ask me if it is possible to solve this situation with Quartz:
There is one Process that must be executed only days of the week at 00:00hs, 5:00hs, 9:00hs, and other diferent hours. As you can see, there isn't regular interval of the repeat. There are 5 o 6 different hours.
Is there some ScheduleBuilder to solve this situation?
I tried with DailyTimeIntervalScheduleBuilder but this Schedule work for regular interval of time.
Simple solution is to schedule job every hour. And within the Job check current time to see if it is 00.00 or 05.00 or 09.00. If it is, do your Job, else do nothing.
It's very simple, if you use a CronTrigger , see: http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger.
This way you will be able to schedule the job only on specific times, using an expression similar to Unix cron expressions.
For your case, if you like to start a job on each working week day at 0, 5 and 9 hours, you will use an expression like 0 0 0,5,9 ? * MON-FRI

Categories