How to make JobDetailBean run non concurrently? - spring

I am running some scheduled jobs using the Quartz Scheduler. All the jobs in my application are JobDetailBean jobs.
I know that using MethodInvokingJobDetailFactoryBean, we can make a job non-concurrent like this:
<property name="concurrent"><value>false</value></property>
I wonder how we can make the JobDetailBean jobs non-concurrent. JobDetailBean jobs run concurrently by default.
The requirement is: jobA needs to run every 5 min but only if the previous instance of it has completed.
Thanks,
Shikha

Your job class needs to implement StatefulJob, or alternatively, if you are using Quartz 2.x, be annotated with #DisallowConcurrentExecution.
This is how concurrent execution can be controlled in Quartz, and in particular MethodInvokingJobDetailFactoryBean just uses different job classes depending on the value of concurrent.

Related

Spring boot Quartz to skip some jobs

I use spring boot + maven multi module project. I am using cluster-aware Quartz configuration, so job details are stored in the database. Lets say, 2 Spring boot maven modules (projects) are using quartz, but I want each module run it is own jobs, which are different to each module. In that case, when I start one module, it actually tries to run jobs from other module too because Quartz engine is reading the jobs to run from the database. So how do I specify in each module to run only specific jobs related to at module?
One way is to have different table prefixes for each module. Is there any other way to use the same quartz tables between 2 different modules, but each module decide which jobs to run?
I have almost the same. My solution was:
Job A writes a flag into the DB table next to the job "running_by" jobA and when its done, it remove the flag.
So JobB checks if this Job has a flag already. If not it will proceed, if yes it skip this job because JobA is already running it.
config a property in quartz.properties
org.quartz.jobStore.isClustered = true
that can make quartz work in cluster mode

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.

What is the difference between spring scheduled tasks and spring batch jobs

I dont understand the difference between scheduled tasks and batch jobs in spring.
By scheduled tasks I mean the ones which are configured like these:
#EnableScheduling
public class AppConfig{
..
and used like
#Scheduled(fixedRate=550)
public void doSomething(){
..
By batch jobs I mean these:
#EnableBatchProcessing
public class AppConfig{
..
and lots of implementations like:
Jobs, Job launcher, Steps, ItemReader, ItemWriter... etc
I would like to know the main difference between them besides the implementation differences and also I am curious why to use batch jobs and make a lot of long implementations while we can use simple scheduled tasks. I mean the implementation of scheduled jobs is quite easy but maybe they had disadvantages according to the batch jobs?
Spring Scheduler is for orchestrating something based on a schedule. Spring Batch is a robust batch processing framework designed for the building of complex compute problems. Spring Batch does not handle the orchestration of jobs, just the building of them. You can orchestrate Spring Batch jobs with Spring Scheduler if you want.
2 aspects which i can think of: afaik when a job-run fails, in 2. run, it will run with the same job parameters.. at least you can configure this i think. and this kind of error situations which you can configure more easily than writing all in code in the same place manually (your scheduled method). Secondly, maybe batch gives a structure to your code when you also have to read your data from somewhere, and write somewhere... batch has some kind of reader, processor, writer schema.. Also some automatically created database tables (BATCH_JOB_INSTANCE) and batch job results.. like when the job started etc...
Edit: More Reasons for a batch: large amount of data, Transaction management, Chunk based processing, Declarative I/O, Start/Stop/Restart, Retry/Skip, Web based administration interface.

Scheduling inside step using Spring-Batch

Part of my job is to poll DB for a specific result status and only after that I can continue with next job's steps.
Is it recommended to stop the job's process while doing polling in one of the steps(tasklet I guess) ?
Polling the DB for a specific result sounds like a situation where you need a scheduler.
Spring-batch assumes the scheduling of the job is done from outside it's scope.
You can use #Scheduled spring annotation if you want to keep all inside spring configuration or use an external tool like this.
If you have more complex situations, have a look at Spring Batch Integration.

Spring + Quartz + JobStoreCMT

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.

Resources