Quartz uses Trigger, Job and JobDetail objects to realize scheduling of all kinds of jobs. For the basic concepts behind Quartz. For convenience purposes, Spring offers a couple of classes that simplify the usage of Quartz within Spring-based applications.
Using the JobDetailBean
JobDetail objects contain all information needed to run a job. The Spring Framework provides a JobDetailBean that makes the JobDetail more of an actual JavaBean with sensible defaults. Let's have a look at an example:
<bean name="exampleJob" class="org.springframework.The job detail bean has all information it needs to run the job (ExampleJob). The timeout is specified in the job data map. The job data map is available through the JobExecutionContext (passed to you at execution time), but the JobDetailBean also maps the properties from the job data map to properties of the actual job. So in this case, if the ExampleJob contains a property named timeout, the JobDetailBean will automatically apply it:
package example;All additional settings from the job detail bean are of course available to you as well.
Using the MethodInvokingJobDetailFactoryBean
Often you just need to invoke a method on a specific object. Using the MethodInvokingJobDetailFactoryBean you can do exactly this:
<bean id="jobDetail"The above example will result in the doIt method being called on the exampleBusinessObject method (see below):
public class ExampleBusinessObject {Using the MethodInvokingJobDetailFactoryBean, you don't need to create one-line jobs that just invoke a method, and you only need to create the actual business object and wire up the detail object.
By default, Quartz Jobs are stateless, resulting in the possibility of jobs interfering with each other. If you specify two triggers for the same JobDetail, it might be possible that before the first job has finished, the second one will start. If JobDetail classes implement the Stateful interface, this won't happen.
The second job will not start before the first one has finished. To make jobs resulting from the MethodInvokingJobDetailFactoryBean non-concurrent, set the concurrent flag to false.
<bean id="jobDetail" class="org.Wiring up jobs using triggers and the SchedulerFactoryBean
We've created job details and jobs. We've also reviewed the convenience bean that allows you to invoke a method on a specific object. Of course, we still need to schedule the jobs themselves. This is done using triggers and a SchedulerFactoryBean. Several triggers are available within Quartz. Spring offers two subclassed triggers with convenient defaults: CronTriggerBean and SimpleTriggerBean.
Triggers need to be scheduled. Spring offers a SchedulerFactoryBean that exposes triggers to be set as properties. SchedulerFactoryBean schedules the actual jobs with those triggers.
Find below a couple of examples:
<bean id="simpleTrigger" class="org.springframework.Now we've set up two triggers, one running every 50 seconds with a starting delay of 10 seconds and one every morning at 6 AM. To finalize everything, we need to set up the SchedulerFactoryBean:
<bean class="org.springframework.scheduling.More properties are available for the SchedulerFactoryBean for you to set, such as the calendars used by the job details, properties to customize Quartz with, etc.
|
|
Java-Springs Related Tutorials |
|
---|---|
Adv Java Tutorial | J2EE Tutorial |
Core Java Tutorial | JSP Tutorial |
Java Servlets Tutorial | Hibernate Tutorial |
Java Tutorial | Framework7 Tutorial |
Java 8 Tutorial |
Java-Springs Related Interview Questions |
|
---|---|
Adv Java Interview Questions | J2EE Interview Questions |
Core Java Interview Questions | JSP Interview Questions |
Java-Springs Interview Questions | Java Servlets Interview Questions |
JMS(Java Message Service) Interview Questions | Hibernate Interview Questions |
Java applet Interview Questions | Java Interview Questions |
Framework7 Interview Questions | Java collections framework Interview Questions |
Java 8 Interview Questions | Java Programmer Interview Questions |
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.