Quartz job tunning - java

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

Related

Start and stop a program at specific time intervals

I am looking for a solution to start and stop a program at predefined time intervals.
A scheduler would do the required justice, but how could the same be attained without using a scheduler.
I could always write a block of code, which checks for the current timings and based on this i could make a decision, however executing this piece of code would consume additional resources and processing power, which is not required if the program runs for hours.
Any efficient suggestions/solution on how this could be attained?
You can use a timer with following algorithm.
in timer.tick Method
Stop Timer
Perform Operation
Start Timer
In Form Load
Set Timer interval
Set Timer Tick
This works with Winforms
You wish to start and stop a program. If by program you mean an entire Java process I suggest that you use crontab for Linux/Unix to define the timing and then invoke some start- and stop-script. Windows has similar capabilities by the use of the Task Scheduler.
Crontab example that starts a job at 11 and 16 every day.
00 11,16 * * * /home/someuser/bin/start-app.sh
If you wish to start a job from within a Java process I suggest that you use a Timer and schedule a TimerTask that does the job for you.
However, I guess that both of the solutions above are technically schedulers that you did not want to use.

is it ok that two spring batch job overlaps?

Suppose I have a spring job run every 5 minute, usually the job will take about one minute to complete, but if something goes wrong the job will last more than 5 minute. Before last job finished , another job will start. So, the two jobs will interfere with each other?
ps: I use the spring schedule annotation to schedule jobs.
You can control this behavior. If you want to leave a fixed amount of time between the end of one job and the start of the next, use the fixedDelay http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html#fixedDelay--.
If you use the fixedRate, then jobs may overlap. Whether that's "ok" depends on what your job does. But you can prevent this from happening with fixedDelay if you want.

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

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.

#Scheduled annotation Spring

I am using Spring 3 annotation #Scheduled to create scheduled jobs on server. But i am confused about the parameters(cron, fixedDelay ,fixedRate) of #Scheduled annotation. Please explain the difference between these parameter and the situations in which I can use these parameters.
I believe the difference among different options are made clear here. It depends on how you need to execute the task:
fixedRate makes Spring run the task on periodic intervals even if the last invocation may be still running.
fixedDelay specifically controls the next execution time when the last execution finishes.
cron is a feature originating from Unix cron utility and has various options based on your requirements.
cron : A cron-like expression, extending the usual UN*X definition to include triggers on the second as well as minute, hour, day of month, month and day of week.
fixedDelay : Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
fixedRate : Execute the annotated method with a fixed period between invocations.
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/scheduling/annotation/Scheduled.html

Categories