How can I run several schedule using quartz with spring configuration - spring

In my situation, clients are allowed to schedule a job. As I can see, quartz often use a cronExpression to perform a schedule. But there are many clients with many schedules, I can't write many trigger beans cuz I don't know how many schedules really are, it depends on clients. So, would some one help?

Quartz is designed to add and remove jobs and triggers at runtime. Spring is a degenerated case where all triggers and jobs are defined at startup time. In Quartz, when having an instance of Scheduler you can easily create, browse and delete triggers at your wish, example from How-To: Scheduling a Job:
// Define job instance
JobDetail job1 = newJob(ColorJob.class)
.withIdentity("job1", "group1")
.build();
// Define a Trigger that will fire "now", and not repeat
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.build();
// Schedule the job with the trigger
schedulder.scheduleJob(job, trigger);
See the official documentation, especially the cookbook.
Also distinguish between jobs (a piece of code wrapped in a class that should be executed) and a trigger (time condition under which job is executed). Remember that by default when last trigger associated with the job (job can have several triggers) fires, the job is removed from the scheduler.

Related

How can I ensure a spring quartz job executed sequentially?

I have below trigger config:
SimpleTrigger trigger = TriggerBuilder.newTrigger().startNow()
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5)).build();
And my job could possibly run more than 5 seconds.
Which means there could be chances that 2 job instances run at the same time.
Is there any policy I can enforce on quartz scheduler:
if job is running, do not fire next one even if reach the repeat interval.
Is it doable?
Thanks
Try using this annotation #DisallowConcurrentExecution.This should not allow executing of same instance of a Job
https://www.quartz-scheduler.org/api/2.1.7/org/quartz/DisallowConcurrentExecution.html

How to prevent Quartz scheduler from missing few executions

We have quartz scheduler to trigger the jenkins job which will be created on the fly through Jenkins create API.
So I have placed my create jenkins job API call inside the execute internal method.When multiple parallel requests are made, sometimes all the requests are picked up for execution and sometimes few are missed.
The count of missed executions differs.
How do I make this stop and make quartz to run all of my requests.
Tried increasing the thread count and misfire threshold but the issue exists
Seems like all you need is to set the correct misfire instruction based on your business logic and type of trigger
Trigger trigger = TriggerBuilder.newTrigger() .
withIdentity(changePoolNameTriggerKey).
startAt(new DateTime().plusSeconds(configuration.getInt(JobConstants.execution_latency)).
toDate().
build();
((MutableTrigger) trigger).setMisfireInstruction(MISFIRE_INSTRUCTION_FIRE_NOW)

Spring Scheduling Quartz and thousands of jobs

According to the business logic of my Spring Boot application with Quartz Scheduling and MongoDB as Job persistent storage, every user of the system can create the postponed job that must be executed at some point in time. The user chooses the time when it must be executed.
Right now I'm thinking about the approach where every user will create a dedicated JobDetail for every postponed job, something like this:
schedulerFactoryBean.getScheduler().addJob(jobDetail(), true, true);
The issue I can potentially see here, that with this approach I can quickly create thousands of jobs in Quartz scheduler. Previously I never scheduled such amount of jobs in Spring Scheduling with Quartz and don't know how the system will handle it. Is it a good idea to implement the system in such way and will Spring Scheduling Quartz handle such amount of jobs without problems?
Yes, Quartz itself can handle thousands of jobs and triggers without any issues.
If you are going to have many jobs executing concurrently, just make sure that you configure Quartz with a sufficient number of worker threads. The number of worker threads should be typically equal to the maximum number of jobs that can be running concurrently + some small buffer (10% or so) just in case.
From what you write I assume that your jobs will be one-off jobs, i.e. each job will be executed only once. If that is the case, Quartz can automatically discard your jobs as soon as they finish executing unless your jobs are marked as durable. Quartz automatically removes non-durable jobs if they are not scheduled to run in the future. This feature may help you reduce the total number of registered jobs.
I hope this helps. If not, please ask.

Prevent Sidetiq from scheduling new jobs based on {add logic here}

We would like to stop Sidetiq from scheduling new jobs based on some condition. In our use case, that there are no newer versions of the code running in another deployment/container)
How can we add logic at the point Sidetiq is scheduling the job and prevent it from doing so?

Spring quartz: Run 4 or 5 jobs using the same cron trigger?

How do i run four or five jobs using the same quartz cron trigger.Extending the code must be easy as we will keep on adding the jobs as we go on.
So any implementation details for this particular scenario ?
Please help.
I think you have 2 choices here:
1 - create a new trigger based on the same cron expression each time you add a new job. This could be achieved really easily using a wrapper bean (i.e. "MyCronJobScheduler") that has the cron expression as an instance, and use it to create a new trigger + job each time you call the MyCronJobScheduler.addJob() method of that bean...
2 - use a parent / child pattern, where your cron trigger schedules a parent job, whose only purpose is to start the children jobs each time it's executed...(since you can fire jobs from another job, or from a trigger/job listener)
Hope that helps.

Resources