I am wondering if there is a function/method to create jobs which are triggered on random time. By that I mean that if I set a cron schedule to be triggered every monday at 10.00 am and given a time interval, lets say 30 minutes, the trigger will always go off from 9.30 ~ 10.30.
For example this is the cron schedule.
schedule.setCronSchedule("0 0 10 ? * MON");
trigger = newTrigger()
.withIdentity(triggerId)
.startNow() // <~~~~~~~~~~~~~~~ ???
.withDescription(schedule.getCronSchedule())
.withSchedule(cronSchedule(schedule.getCronSchedule())).build();
If I have a variable with a specific range in minutes can I set it to trigger randomly? And by that I mean not just take the cron schedule string and remodify it, but using a method to trigger the event every time, based on the random range so first monday may be triggered at 10.01 second monday may be triggered at 9.46 and etc.
Thanks in advance.
To fire a schedule at some time between 1.00am and 1.30am every day, you could try this:
schedule.setCronSchedule(String.format("0 %d 1am * * ?", random.nextInt(30)));
Unfortunately there isn't anything built into Quartz, or even unix cron for that matter. And the randomness is going to be the same every day from here on, unless you regularly reset the schedule. But perhaps that isn't a problem for your situation.
Related
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.
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
In quartz scheduler I have a doubt
For example
If we schedule job for every 5 minutes or 10 minutes or 2 hours it is ok we call scheduler using regular time interval like below no problem,
SimpleTrigger simpleTrigger = new SimpleTrigger("mytrigger",sched.DEFAULT_GROUP,new Date(),null,SimpleTrigger.REPEAT_INDEFINITELY,30L*5000L);
But,If I schedule job # 2'0 clock and 3'0 clock today then,tommorow I change it to 5'0 clock and 7'0 clock and 11'0 clock, my doubt is if fixed interval we call it in above way but it not fixed or periodic interval how do we handle it. Any suggestions or Ideas or examples regarding my query....
What you can do is define the intervals in a file or DB and read it from there.
Also define a JobListener.
Store the interval values in the JobDataMap and every time the job starts, use the listeners method jobToBeExecuted() and read the values from the file/DB and compare it with the values in the JobDataMap.
If they are the same, then no update was done and the job runs, if it was changed, reschedule the job using Scheduler.rescheduleJob(TriggerKey triggerKey,Trigger newTrigger).
Note that there is a little problem with this method, the configuration will only change once the job runs, so even though you update the file/DB the next run would still be in the old time, and only the run after that would be in the new time.
I need a cron job to run on every 15 mins in every weekday just
from 8:00 to 16:00.
How to set the Schedule Format for this cron job?
The simplest approach: set it to run every 15 minutes anyway, and then at the very start of your application code just bail out immediately and innocuously if it's not a weekday or the time is too early or late. This will serve you particularly well if and when further complications are involved (this schedule looks suspiciously like "working hours" so it can't be long before somebody asks you to avoid running on Christmas and the like;-).
Take a look at this link which describes how to create a cron expression http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html
It should be something like this for your case 0 0/15 08-16 * * MON-FRI (Not validated)
hello there is something i've realized with quartz when working.Say a cron is set to wake up every 2min with the expression 0 0/2 * * * ? .
When you run the project at say 13:10:30, the first action happens at 13:12:00 and the second 13:14:00 and every 2min 0 second for the rest. Obviously between the startup of the project and the first occurence of the action there have been 1mn:30s only.
Is there a way to for the first occurrence to respect the 2min no matter which at seconds the project starts?
Cron jobs are configured in Quartz using the CronTrigger class. The alternative is to use SimpleTrigger, which you can construct using fixed delay intervals. SimpleTrigger has various constructors, allowing you to specify the start time, end time, number of repeats, repeat interval, and so on.
Having said that, I'd recommend against using Quartz for this kind of scheduling, and use java.util.concurrent.Executors.newScheduledThreadPool(). It's much easier than Quartz when it comes to simple repeating tasks.
Quartz may use cron for the scheduling, which is based on date and time, not duration. This means that the cron expression you define is directly related to the current time on the machine, not on when the application started.
I am not aware of a Quartz configuration that will help you to solve your problem. However, a solution is to create your own Thread, which started during the launch of your application and that basically waits 2 minutes before calling a method:
while (running) {
Thread.sleep(1000 * 120);
doStuff();
}