Disabling injection of Spring Scheduler - java

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

Related

Clustered #schedule annotations in Spring

My codebase currently uses a crontab-like quartz.properties file to define a series of jobs in our Spring web app. Our quartz config currently looks like:
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.jmx.export=true
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=20
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.misfireThreshold=6000000
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.dataSource=dataSourceConnectionProvider
I've recently become enamored of Spring's #scheduled annotation and would really like to take advantage of it. There are plenty of instances where we have some job bean that essentially wraps a single method of a service -- the annotations seem like they'd remove a lot of boilerplate and more our code more legible as a result.
However, I can't figure out how I could get the same guarantees from Spring's scheduling that we do from vanilla quartz right now. That is, setting isClustered=true and the job store implementation to use a backing database enables us to guarantee that our jobs run once per cluster without doing something unpleasant like declaring a master node and checking hostnames -- which isn't even a good guarantee for us, since we have a variable number of nodes. Nonetheless, it seems like it should be possible -- Spring's scheduling is obviously very aware of Quartz, and I have to imagine that there's a way to implement a TaskScheduler that would provide these guarantees.

Programmatic timers and Automatic timers - difference

I' want to find difference between that Timer Services. and which should I use and when.
I'm using Jboss Application Server.
1) java.ejb.Schedule. # Schedule annotation or configure from
xml.
2) javax.ejb.Timer. # Timeout annotation.
3) javax.ejb.TimedObject. # Timeout annotation or configure from
xml.
is 2 and 3 the same? that is difference between Programmatic timers and Automatic timers?
is quartz-scheduler implementation Schedule? does they make the same job?
1) You can use #Schedule annotation on any business method of your EJB, but timer cannot be created dynamically then.
2) When you mark method with #Timeout annotation, it will be invoked when problematically created timer will be triggered. Metadata for triggered timer is in Timer object.
3) TimedObject interface is an alternative to #Timeout annotation, as TimedObject interface contain ejbTimeout(Timer timer) method.
is 2 and 3 the same?
2 and 3 are generally the same,
that is difference between Programmatic timers and Automatic timers?
difference is in the way you create them (with #Schedule annotation there is a limited functionality, as you cannot pass custom object).
is quartz-scheduler implementation Schedule? does they make the same job?
Quartz scheduler is a powerful framework, but no so well integrated with Java EE6 as Timer objects. I prefer EJB Timers, and use quartz only when I need some extra features (e.g. cron expressions).
EJB 2.1 Java 1.4
The ejbTimer has to implement TimedObject interface.
The TimerService has to be accessed through EJBContext().
The business logic has to be placed in ejbTimeout() method.
EJB 3.0 Java 5
Now, the TimerService can be accessed using Dependency Injection with the annotation #Resource TimerService.
The business logic can be placed at any method annotated with #Timeout
The previous versions are called Programatic timer.
EJB 3.1 Java 6
Automatic Timer appears, this means that now you don't have to care about how to get the TimerService due to the ejb container will do the job.
The business logic has to be placed at any method annotated with #Schedule or #Schedules, this annotation also allows to add the timer execution period. (in the previous versions this kind of configuration is placed in xml files)
Quartz is not part of Java EE specification.

Scheduling tasks in Java/Spring

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

Spring Scheduling: #Scheduled vs Quartz

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.

Java – Create a workflow in Quartz

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.

Categories

Resources