Spring + Quartz + JobStoreCMT - spring

I need help with quartz + jdbc Jobstore.
Situation looks that:
I have spring application, and within this application there is quartz scheduler, it use persistence jobstore (JobStoreCMT with oracle Database). When i need to do some scheduled job, basicly I'm creating class with my annotation, where I'm writing for exapmle cron expression. When application starting, for all classes annotated like this are creating jobs, and scheduling with quartz.
When i don't want some job, i'm deleting his class, and there isn't loaded on startup. But what to do with already scheduled Job. When i'm starting my application without this Job Class, quartz want to recovery this job from Database JobStore, but this class is not exist, so i gets exeption.
Is there any way to 'tell' quartz, that if job class doesn't exist, quartz should deleted it from scheduler ?

There exists a deleteJob() method in the Scheduler class in order to delete Jobs.
See http://quartz-scheduler.org/api/1.8.5/
You could also use getJobNames() to know if one of your jobs is missing.

Related

How can I custom number of job per second (job/s) that be executed by quartz scheduler?

I'm attempting to practice with Quartz Scheduler in a small Spring Boot Application. I really want to know if any way to determined number of job per second (job/s) that be executed by quartz scheduler. I checked all quart properties in documentation but can not find anything.
Thanks all!
You can maanage this from your property file though property name is depend on which Job store are you using,
For JDBC Job store, property name is org.quartz.threadPool.threadCount.
Check out http://www.quartz-scheduler.org/documentation/quartz-2.1.7/configuration/ official documentation for respective implementation.
[documentation is for 2.1.7]

Spring task:scheduled or #Scheduler to restrict a Job to run in multiple instance

I have one #Scheduler job which runs on multiple servers in a clustered environment. However I want to restrict the job to run in only in one server and other servers should not run the same job once any other server has started it .
I have explored Spring Batch has lock mechanism using some Database table , but looking for any a solution only in spring task:scheduler.
I had the same problem and the solution what I implemented was a the Lock mechanism with Hazelcast and to made it easy to use I also added a proper annotation and a bit of spring AOP for that. So with this trick I was able to enforce a single schedule over the cluster done with a single annotation.
Spring Batch has this nice functionality that it would not run the job with same job arguments twice.
You can use this feature so that when a spring batch job kicks start in another server it does not run.
Usually people pass a timestamp as argument so it will by pass this logic, which you can change it.

When spring-boot app multi-node deploy, how to handle cron job?

When I use spring task handle a simple sync job! But when I deploy multi-node, how I make sure the cron job just run one time.
Maybe you say that:
1. Use distributed-lock control a flag before the crob job run.
2. Integrated quartz cluster function.
But I hope spring task #EnableScheduling can add a flag argument, so as we can set a flag when launch app.
We are using https://github.com/lukas-krecan/ShedLock with success, zookeeper provider in particular.
Spring boot, in a nutshell, doesn't allow any type of coordination between multiple instances
of the same microservice.
All the work of such a coordination is done by the third parties that spring boot gets integrated with.
One example of this is indeed a #Scheduled annotation.
Another is DB migration support via flyway.
When many nodes start and the migration has to be done, flyway is responsible to lock the migration table by itself, spring boot has nothing to do with it.
So, bottom line, there is no such support and all options that you've raised can work.

What's the point of Spring Batch's JobLocator?

I've created a Spring Batch application (together with Spring Boot). Configuring a Job works well, and each Job is executed at startup. The job configurations also show up in the database as expected.
To launch a Job with parameters, there are two options:
Inject the Job as it is a Java Bean - works great
Use JobLocator to get a instance of the Job - seen in several tutorials, but does not work ("No job configuration with the name [doSomethingJob] was registered")
So my question is: What's the point of a JobLocator if one can easily inject jobs directly?
The JobLocator isn't for injection of jobs. It's to locate an instance to execute. If you have something that will be executing jobs dynamically (not knowing what job it will need to execute), you'll want to use a JobLocator. An example of this is in Spring Batch Admin. There, a JobLocator is used from within the JobService to get the requested Job to launch.
Wiring a Job instance into your class works well when it's predetermined what job you'll be running. However, if you don't, the JobLocator is the way to go.

Couple of questions about spring quartz

I'm thinking of building spring quartz into my spring mvc web application. I have a few questions about it that I could not properly find an answer.
If I want to use cron triggers for spring quartz does quartz execute the job using java system time or the operating system time?
I am planning to have a properties file to hold all of my cron triggers. If someone goes and changes a cron trigger for an ad-hoc execution of a job will quartz automatically pick up the changes in the file? Or is there a way for me to tell quartz how to do this if it is not default behavior.
I have been reading about spring batch admin console recently. Sounds like a nice gui tool to reschedule jobs. Can it be used to make ad-hoc changes to crontab triggers? Or is there another gui tool I could use to manage the job triggers?
thanks in advance
Quartz
Quartz uses custom thread scheduler (org.quartz.core.QuartzScheduler) which use java system time. It can integrate commonj interface to be JEE (WAS and Weblogic) interoperable.
Reload configuration: read Quartz: How to reload jobs and triggers with org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin?
Spring batch admin console is for spring batch and is bale to monitor batches activity
Reload configuration Using API
Generally speaking you can use quartz API programmatically (I use them):
JobDetail job = new JobDetail();
job.setName("myJob");
job.setJobClass(MyJob.class);
CronTrigger trigger = new CronTrigger();
trigger.setName("myTriggerName");
trigger.setCronExpression("0/30 * * * * ?");
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
these APIs provide you fine control.
Reload configuration JMX way
You can control the Qurtz scheduler through RemoteMBeanScheduler:
An implementation of the Scheduler interface that remotely proxies all method calls to the equivalent call on a given QuartzScheduler instance, via JMX.

Resources