I have a class "Applier" which implement Job . Means class "Applier" is one of instance of Quartz Job.
My requirement is to control number of instance of "Applier" execute at a time. Means i want to make limit like at a time maximum 5 instance of "Applier" execute. If 6th instance of "Applier" come and 5 instance already executing than it must have to wait until one of the instance of "Applier" completed.
Is there any wait/notify type mechanism in Quartz Scheduler. Means if 6th instance of Job try to run and 5 instance of already executing than 6th instance must have to wait and notify after any of the 5 instance execution completed.
Means i want to achive some ThreadPool type mechanism for particular instance of Job . I do not want ThreadPool Like mechanisum at Quartz Scheduler level b'coz it is already provided by the Quartz Scheduler.
I am not sure if you are aware of this but, all jobs are unique to jobkey.
Lets start with a convention for this answer
JOB -- This means a quartz job
Job -- Quartz job class
As I was mentioning earlier there can be only one instance of a JOB. How this happens is, each JOB is defined by a jobkey, now jobkey is always unique
This means generally you have one to one mapping between JOB and Job, i.e. a scheduled job (QuartZ job and class implementing the interface Job
which is JOB -> Job
CASE 1
multiple instances of Jobs and not multiple instance of JOBs
which will look like
JOB A -> Job |
JOB B -> Job |...........Multiple JOB running same class
JOB C -> Job |
Simply keeping a different jobkey in same jobgroup allows you to load multiple instances of Job to your scheduler
Have a look at JobFactory because
A JobFactory is responsible for producing instances of Job classes.
..
CASE 2
Now coming to instances of JOB. based on your schedule and the execution time of the JOB, there can be multiple instances running parallelly (it is improper to call it as multiple instance better term would be concurrent execution). TO avoid that you will have to write some custom logic. Or use annotation DisallowConcurrentExecution.
Load 5 instance of the Job as shown in CASE 1 which have DisallowConcurrentExecution. But this will mean 5 unnecessary instances, even though at a time the job may not be needed.
CASE 3
Now lets consider setting a flag initialized to 1. A custom logic which may help handling this
Before you start you Job. you can check if the flag is less than 5. If less than 5 enter to the main business logic and increment the value of flag. After end of execution before coming out of the block decrement it. This way the business logic will not be executed even if you have multiple instances of JOB running.
private static int jobRunningFlag = 0;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
if(JobName.jobRunningFlag <= 5)
{
JobName.jobRunningFlag++
.....execute this piece of code....
JobName.jobRunningFlag--
}
}
Obviously jobRunningFlag will be a static variable stored in same Job class
PS: More answer to this would be based on your comments. Have you looked over JobFactory and related classes/interfaces. What have you tried. My idea is this will come down totally onto your implementation logic. I am not very good with quartz do not have much idea about them.
You can either limit the number of jobs that run concurrently (threadpool) or you can limit a given instance of job to only one concurrent instance. You can't limit a given job to only a given number of instances (other than 1). The only way to do what you want to do is to only run one kind of job for a given scheduler and then limit the threadpool to the number of jobs you want running at the same tieme
来源:https://stackoverflow.com/questions/19978738/how-to-limit-number-of-job-instance-of-particular-class-concurrent-execution-in