Quartz for iregular interval of hours - java

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

Related

cronExpression for last day of month and every 10 min by using CronScheduleBuilder.cronSchedule

I'm familiar with Cron on linux systems but by using Quartz Jobs, I have some doubts as the format is not really standard...
I would need a confirmation of the cronExpression syntax...
Here is the CronExpression I use to trigger my Job every 10 Min:
0 10 * ? * *
And here is the CronExpression to start this Job the last day of month at 11:15pm:
0 15 23 L * ?
Can you confirm the syntax is correct ? my expression to start every 10 min works fine, but it will avoid me to wait until the end of the month to test the second one !
There are two points to consider here.
The cron expression. What if it changes in the future?
Other than reading the manual, lots of information is available online for describing or generating cron expressions (for just one example see here: https://www.freeformatter.com/cron-expression-generator-quartz.html)
Testing it. How are you testing? The question lists Java. Are you using JUnit? Some code is needed but in general to be sure it will fire when you want it to, write a unit test and mock or override the system time.

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.

Quartz stop scheduled repeatative Job from 00h35 and 06h15

I have a Job which runs everyday in 15 minutes but now the requirement is that we have to stop this job from 00h35 and 06h15 time .
We are using Quartz scheduler. How can I do this?
I don't use the quartz scheduler but I found the documentation here and had a quick look through it. There is an example 'Build a trigger that will fire now, then repeat every five minutes, until the hour 22:00' on page 23 which sounds similar to what you want to do (starting at 06h15 and finishing at 00h35)
If it's not what you're looking for, how about putting a bit of detail in your question, specifically what you've already tried.

Complex time schedule for cron job in AppEngine(Java)

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)

Quartz job tunning

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();
}

Categories