Couple of questions about spring quartz - spring

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.

Related

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 modules to use for a synchronization service in Java/Spring?

I'm willing to build a synchronization service in Java. The use case is, that i'm fetching data from an exchange-service (via Exchange Web Services), normalize the data a bit (process probably) and then write it to a backend via GraphQL. I already had a look around the spring modules, but am not quite sure what modules to use. I found spring batch and spring quartz.
The synchronization will have to trigger all X seconds, fetch information from the Exchange, look what's in the backend already and update what's needed.
Do you guys have any suggestions? I started implementing this whole thing in nodejs before, but as it has to run on both, Windows Servers and Docker/Linux, it has been a real pain to keep it running smooth (mostly because bundling nodejs to an application for Windows is pain).
Difference between Spring Batch & Quartz:
Spring Batch and Quartz have different goals. Spring Batch provides functionality for processing large volumes of data and Quartz provides functionality for scheduling tasks.
So Quartz could complement Spring Batch, A common combination would be to use Quartz as a trigger for a Spring Batch job using a Cron expression.
Conclusion : So basically Spring Batch defines what should be done, Quartz defines when it should be done.
Quartz is a scheduling framework. Like "execute something every hour or every last friday of the month"
Spring Batch is a framework that defines that "something" that will be executed.
You can define a job, that consists of steps. Usually a step is something that consists of item reader, optional item processor and item writer, but you can define a custom stem. You can also tell Spring batch to commit on every 10 items and a lot of other stuff.
You can use Quartz to start Spring Batch jobs.
Recommended for your use case :
Quartz scheduling as you want trigger after specific interval.
Reference :https://projects.spring.io/spring-batch/faq.html

Spring Boot & Batch Schedule

I have been checking all over to find out how to configure a schedule for Spring Batch. I am using Spring boot with a web ui. The user can go and select a time to run a job and this must be persistence to a database and run when the time comes. A different user can schedule the same job for a different time. Does spring batch have anything similar to this? If not then what is the best way to go about this? need some examples.
There are no out of the box solution which just work. You can do this with minimal coding. Try to persist the schedule in database and on start up your application you can read all the schedule and schedule using batch scheduler. Also you can do the same thing when user enter new schedule with out restart.
Cron based scheduler is already available in spring. Just need to persist and schedule from your side.
scheduler.schedule(task, new CronTrigger("* 15 9-17 * * MON-FRI"));
documentation on scheduler is given here

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