I'm reading the Spring 3.0 docs regarding scheduling. I'm leaning towards Spring's JobDetailBean for Quartz. However, the #Scheduled annotation has captured my eye. It appears this is another way of scheduling task using the Spring Framework. Based on the docs, Spring provides three way of scheduling:
#Scheduled
Via Quartz
Via JDK Timer
I have no interest in the JDK Timer. Why should I choose #Scheduled over Quartz? (When I mention Quartz I mean using Spring's bean wrapper for Quartz).
Let's say my use case is complex enough that I will be communicating to a third-party web service to import and export data at specified intervals.
Quartz is an order of magnitude more complex than Spring's built in scheduler, including support for persistent, transactional and distributed jobs. It's a bit of a pig, though, even with Spring's API support.
If all you need to is to execute methods on a bean every X seconds, or on a cron schedule, then #Scheduled (or the various options in Spring's <task> config schema) is probably enough
I have to state my own experience regarding use of #Scheduled versus Quartz as scheduling implementation in a Spring application.
Scheduling jobs had the following requirements:
End users should have the ability to save and schedule (define execution time) their own tasks
Scheduled jobs during server downtime should not get omitted from jobs queue
Hence, we have to try and use Quartz implementation (version 2.2.3) in order to support persistence of jobs in a database. Some basic conclusions are the following:
Integration with a Spring 4 MVC application is not difficult at all using quartz.properties file.
We had the ability to choose a second database for storing the jobs from the main database.
Jobs scheduled during server downtime begin running as long as server comes up.
As a bonus we managed to maintain in main database some useful (and more user-oriented) information about user defined scheduled jobs using custom JobListener and TriggerListener.
Quartz is a very helpful library in applications with more complex scheduling requirements.
According to Quartz Documentation
We can use some more and complex feature that it doesn't exist in #Scheduler.
for example:
in Quartz we can placing a scheduler in stand-by mode with
scheduler.standby(); and re schedule it with scheduler.start();.
shutting down a scheduler before execution of job or after that with
scheduler.shutdown(true); and scheduler.shutdown(false);
storing a job for later use and when you need the job you can
triggered it.
JobDetail job1 =newJob(MyJobClass.class).
withIdentity("job1","group1").
storeDurably().
build();
Add the new job to the scheduler, instructing it to "replace" the
existing job with the given name and group (if any).
JobDetail job1 = newJob(MyJobClass.class).
withIdentity("job1", "group1").
build();
In Spring you could schedule task by using FixedRate,FixedDelay and cron. But most of the scheduled job requires dynamic handling of execution time. So in this scenario it is better to use Quartz as it provide the option to store scheduled jobs in DBJobstore as well as RAMJobstore.
Related
I need to schedule multiple tasks in the spring boot java project and to do task settings by bringing the value of the task execution time from a database and updating the scheduled appointment during runtime in my program.
Clarification of use case, I have a Rest API, It requires me to make a new schedule making a POST request on the endpoint
the request includes in the body of the necessary information about this schedule, such as the time of the task and the job to be performed, and after making the request, the task is scheduled in my program and executed at its appointed time
I looked at some technology that provides solutions to make his schedules like spring schedule and Quartz, But I cannot create a custom schedule and add it to the scheduling queue in the system, according to the case I mentioned
What technologies should I use for it?
You have the answer in your question. Just creat a scheduler and call the same methods that your api calls internally. example
We have a microservice written using Spring boot which has its own NoSQL datastore. We are working on functionality whereby we want to delete some old data (in magnitude of 0.5 million documents) and want to do it on a regular basis(once a day) based on presence of records of particular type in data store.
Is having a scheduler which runs once everyday and does the deletion, a correct approach for it ? Also since its a microservice and several instances of it will be running, how do we control that this scheduler runs on only 1 instance ?
There are multiple options I can think of now:
If there is a single instance of micro-service deployed, you can use something like quartz to time the job.
Create a RESTful API for cleanup, invoke it using a script, please refer to https://stackoverflow.com/a/15090893/2817980 for example. This will make sure that only one instance of the service works on cleanup.
If there is a master-slave replica, ask the master to allocate to only 1 instance
Create a scheduled job using something like quartz and then check if the job already taken up by some other scheduler in zookeeper/redis/db or any other storage.
I can discuss more on this.
I have some scheduled jobs in my project with #Schedule annotation, but don't want to inject all of them. Do you know how to skip injection of some jobs, or how can I do it in another way?
You could configure the scheduled tasks in a property file. That way, you could add a boolean flag that you inject into any class containing the scheduled task. Then you could check inside your code if the task is enabled and should be executed.
There is detailed code over in question How to conditionally enable or disable scheduled jobs in Spring?.
If you want to decide if a #Scheduled task should be detected and registered at a ScheduledAnnotationBeanPostProcessor, you might want to look into the SchedulingConfigurer:
[It allows] registering scheduled tasks in a programmatic fashion as opposed to
the declarative approach of using the #Scheduled annotation
There is a requirement in the project that will have a scheduled task that will do some job.
The project is Spring based and the scheduled job will be part of the application war.I have
never implemented this kind of functionality before.
I have heard of Quartz. Also, I read somewhere that Spring provides some functionality to schedule tasks. So, I was thinking if I am already using Spring then why to go for some other API(Quartz).
I am not sure which one to use? what will be the pros/cons of one over another?
Please suggest what will be the best way to approach my requirement.
I have used Spring's Task execution and scheduling - http://static.springsource.org/spring/docs/3.0.x/reference/scheduling.html
I am considering using the Quartz framework to schedule the run of several hundred jobs.
According to their API, jobs can be scheduled to run at certain moments in time but not to run one after the other (and stop a chain of jobs if one fails).
The only recommended methods I was able to find are:
Using a listener which notices the completion of a job and schedule the next trigger to fire (how to coordinate this?)
Each job will receive a parameter containing the next job to run and, after completing the actual work, schedule its run. (Cooperative)
Do you know a better method to create a workflow of jobs in Quartz?
Can you recommend other methods/framework for implementing a workflow in Java ?
EDITED: In the meantime I found out about OSWorkflow which appears to be a good match for what I need. It appears that what I need to implement is a "Sequence Pattern".
When Quartz documentation talks about "Job", it is referring to a class implementing the "Job" Interface, which is really just any class with an "execute" method that takes in the Quartz Context object. When creating this implementation you can really do whatever you want.
You could create an implementation of the Quartz Job Interface which simply calls all the jobs in your workflow in series, and throws a JobExecutionException exception on failure.
It sounds to me like you want Quartz to schedule the first job, and chain everything off that.
Have you looked at encapsulating each task using the Command Pattern, and linking them together ?
I've worked on a project called Dynamic Task Scheduler that use Quartz to execute job chains implementing a simple workflow in a fault-tolerant way (definied in XML format).
Take a look at http://sourceforge.net/projects/dynatasksched/
The project is beta, but I think it can gives you some ideas to start..
Hope it's useful!
For job chaining support for Quartz, you may want to check the QuartzDesk project that I have been involved in. In version 2.0. we have added a powerful job chaining engine that enables you to orchestrate your Quartz jobs without the need to modify your application code.
The engine takes care of propagating the job execution result and other parameters from the source job to the chained target job.
QuartzDesk comes with a GUI that allows you to dynamically update your job chains without disrupting your application.