Do Quartz triggers "pile up" when you set concurrent to false - java

I have a Quartz trigger that is set to execute a process every minute.
Since concurrent is set to false, if the process takes longer than a minute, future triggers are prevented. However, does this mean that the trigger that attempted to run is now "standing in line" and waiting to be executed?
For example, if the process takes 10 minutes to execute, will there be 10 triggers sitting there waiting to execute. I am attempting to prevent a buildup on the server.

No. If concurrency is set to false (with anottation #DisallowConcurrentExecution) further jobs won't start if actual is still running.
See Job State and Concurrency in docu.

The accepted answer is not correct.
I just did the test with quartz 2.2.3 and the jobs are indeed put on the back burner.
Meaning if the time to process the job is superior to the time between jobs, you will pile up the jobs.
The documentation provided by #1ac0 is not mentioning anything apart the job will not run 'immediately'.

Related

Spring's #Scheduled(fixedDelay=...) stops at midnight

I have a Spring Boot application that uses a #Scheduled annotation with a fixed delay. The annotation is used in different classes with different delays. However, at midnight every day all scheduled tasks stop running.
Does anyone know why this might be happening? I can't find any explanation online.
(I am using version 2.0.2.RELEASE of Spring Boot and 5.0.6.RELEASE of Spring Core.)
Thanks in advance!
I'm not sure what do you mean about stopping running.
But the first work you need to do is starting your schedules on different threads and put them into a queue.
If stopping means suspending for a while,then you create a new schedule that make other schedules sleep some time at midnight.Maybe the new schedule should have be started when the thread are sleeping,and the way to prevent is to make sure only one schedule(I mean same kind of schedule) could be put in the queue at the same time.
If you just mean kill all other schedule at midnight,you just need to start your midnight schedules and kill others.
So sorry about my poor English,hope this could help you.
#Scheduled(fixedDelay = 1000)
void scheduleFixedDelayTask() {
System.out.println(
"Fixed delay task - " + System.currentTimeMillis() /
1000);
}
In this case, the duration between the end of last execution and the start of next execution is fixed. The task always waits until the previous one is finished.
This option should be used when it’s mandatory that the previous execution is completed before running again.
So maintain the delay such a way that the previous execution completed.
Hope it helps.
Thanks to everyone who replied.
As it turns out, this was not an issue with Spring. It appeared the tasks stopped because the logs stopped. What really happened was that our scripts that tail the logs were not smart enough to handle log files that roll at midnight (face palm).

Quartz Jobs overlapping

I have a few quartz (2.2) Jobs running. Let's say one is running ever 5 seconds, another on runs every 10 mins.
I don't want 2 jobs to be executed the same time. I've seen this
DisallowConcurrentExecution
but this only applies to jobs from the same instance but I generally don't want two jobs (of any instance) to overlap.
Edit:
All the jobs working with one database, so this is why it's important that they don't run the same time. Each job has different things to do.
The simplest way is to configure the underlying thread pool to use one thread, this will achieve your goal. Add the following property to your quartz.properties configuration file:
org.quartz.threadPool.threadCount
The number of threads available for
concurrent execution of jobs. You can specify any positive integer,
although only numbers between 1 and 100 are practical. If you only
have a few jobs that fire a few times a day, then one thread is
plenty. If you have tens of thousands of jobs, with many firing every
minute, then you want a thread count more like 50 or 100 (this highly
depends on the nature of the work that your jobs perform, and your
systems resources).

Quartz StatefulJob / non-StatefulJob

Would you please explain to me the exact mean of the StatefulJob in quartz and it's difference with none StatefulJob?
StatefulJob interface, provides 2 things,
first: only one job will be run any time
second: in (SimpleTriggerBean) you will not worry about your job running duration. it means that the next run will be done after delay time after ending of previous one.
StatefulJob guarantees only one job will be running at one time. For example, if you schedule your job to run every 1 minute, but your job took 5 minutes to complete, then the job will not be run again until the previous job has completed.
This is useful to make sure there is only one job running at any given time.
The next job will be run on the next schedule, not immediately after the previous job completed.
jobDetail.getJobDataMap().put("type","FULL");
This line is will decide we are using statefull or non-statefull.
If we are passing the argument then it will be statefull.
With out statefull there is no way to pass the arguments in execute method
In state full while execution time if we modify any value then the execution job will be lost it wont re-triggered at simultaneous process time.
Only one job will execute at a time the second will be sleep until the first one is completed.
In multi scheduling process the second job argument will be share to first job at run time. this is one type of disadvantage in multi scheduling process.

Handling Quartz job exception so that it runs its next normal turn without it being refired immediately or dropped

How can I handle the "exceptions" caused by a job run by Quartz scheduler so that it is run on the following scheduled time. I don't want it to refireImmediately or I don't want it to drop this job. Just keep it in the jobstore until its next turn.
If you throw an exception from a Job and it is not JobExecutionException with refireImmediately set, this execution will be discarded and proceed with normal schedule. E.g. when a job is suppose to run every 10 seconds and a single execution threw an exception, Quartz will simply discard this exceution and run next one after 10 seconds.
Unfortunately the only way of refiring with some delay is a custom code (maybe a JobListener implementation?), refireImmediately does what it says. It is a pity Quartz does not support it out-of-the-box.
See a proposed solution here (but not the accepted answer): Quartz retry when failure.

Can I make waiting Quartz Jobs fire in the order they were triggered?

I have an application using the Quartz Scheduler to schedule jobs. The application is currently running Quartz version 1.6.2. My JobStore is org.quartz.impl.jdbcjobstore.JobStoreTX with an Oracle database backing it. Clustering is turned on, but there's only one scheduler using the database. My Quartz threadPool is configured as follows:
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 5
My jobs are long running, so it's fairly common to have 5 jobs running (the maximum allowed by my thead pool) when triggers fire new jobs. The newly triggered jobs misfire and I see log messages like the following:
2011-05-20 04:09:30,097 INFO [QuartzScheduler_scheduler-servername-111305822374881_MisfireHandler] o.q.p.h.LoggingTriggerHistoryPlugin - Trigger DEFAULT.JobName1 misfired job DEFAULT.DEFAULT at: 04:09:30 05/20/2011. Should have fired at: 04:08:29 05/20/2011
2011-05-20 04:09:30,120 INFO [QuartzScheduler_scheduler-servername-111305822374881_MisfireHandler] o.q.p.h.LoggingTriggerHistoryPlugin - Trigger DEFAULT.JobName1 misfired job DEFAULT.DEFAULT at: 04:09:30 05/20/2011. Should have fired at: 04:09:30 05/20/2011
2011-05-20 04:09:30,125 INFO [QuartzScheduler_scheduler-servername-111305822374881_MisfireHandler] o.q.p.h.LoggingTriggerHistoryPlugin - Trigger DEFAULT.JobName2 misfired job DEFAULT.DEFAULT at: 04:09:30 05/20/2011. Should have fired at: 04:08:30 05/20/2011
2011-05-20 04:09:30,138 INFO [QuartzScheduler_scheduler-servername-111305822374881_MisfireHandler] o.q.p.h.LoggingTriggerHistoryPlugin - Trigger DEFAULT.JobName2 misfired job DEFAULT.DEFAULT at: 04:09:30 05/20/2011. Should have fired at: 04:09:30 05/20/2011
2011-05-20 04:11:29,998 INFO [QuartzScheduler_scheduler-servername-111305822376676_MisfireHandler] o.q.impl.jdbcjobstore.JobStoreTX - Handling 2 trigger(s) that missed their scheduled fire-time.
Once a running job finishes, one of the misfired jobs will get picked up and run normally. However, Quartz seems to pick up a misfired job randomly, with no regard to the order the jobs had been originally scheduled to execute. Ideally, I'd like them to be picked up in the order they were supposed to have run, based on their original fire times.
Is it possible to make my waiting (misfired) jobs get fired in the order they were triggered once space in the Quartz ThreadPool becomes available?
When quartz handles a trigger that has missed it's fire time it will update the nextFireTime of the trigger. By default a trigger will be considered missed if it's nextFireTime is over 60 seconds in the past. Missed triggers should still be selected based on nextFireTime and priority order but I'm guessing it seems random because some triggers have been updated and others haven't.
I would suggest increasing the org.quartz.jobStore.misfireThreshold property. See http://www.quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigRAMJobStore.html (although the property is identical for all JobStores). This should make it less likely for your triggers to be re-scheduled.
I sounds like you are running into a misfiring scenario (a scenario where there are more jobs ready to be executing than there are worker threads). Set the misfire instruction and/or priority property on the triggers to change how each behaves after it has past its fire time.
Also, you could consider increasing the misfire threshold, which would change the amount of time that a trigger can be "late" waiting for a thread to execute on before it is considered misfire (and has its misfire instruction applied to it).
Is it possible to make my waiting (misfired) jobs get fired in the order they were triggered once space in the Quartz ThreadPool becomes available?
The "do nothing" instructions will leave the fire times as-is.
Looking at the quartz thread pool , it uses a wait()/notify() loop, which is not fair, and will randomly select a new thread when multiple threads are waiting.
You could use your own instance of ThreadPool which is fair. Copy the code from SimpleThreadPool, but replace the locking around nextRunnableLock with a java.util.ReentrantLock, passing true to the fair constructor. In your modified SimpleThreadPool, use ReentrantLock.lock()/unlock() instead of synchronized, and use ReentrantLock.newCondition().signal()/await() instead of wait/notify, and it might solve your problem.
In case of a CronTrigger, the method updateAfterMisfire() may re-schedule the task at new Date() case MISFIRE_INSTRUCTION_FIRE_ONCE_NOW policy.
If several task are misfired, several of them may be rescheduled at the same time (same millisecond), because computer runs fast.
As a consequence, and if no priority defined, the scheduler will pick-up the first next task, all with same NextFireTime, according to key or full name.
updateAfterMisfire() method should have re-schedule the task to a unique date using a Thread.sleep(25), as an example.

Categories

Resources