Quartz and Spring integration job how to automatically inject spring container-managed objects

Source: Internet
Author: User

The use of quartz in spring is implemented in two ways:
The first is that the task class inherits Quartzjobbean,
The second is to define the task class and the method to be executed in the configuration file, classes and methods can be normal classes. Obviously, the second approach is far more flexible than the first.

Test environment Spring3 M2 quartz-2.1.7 We're going to get this effect.
public class Cancelunpaidordertask implements Job {    @Autowired    private apporderservice orderservice;    @Override public    void execute (jobexecutioncontext ctx) throws Jobexecutionexception {...        }
But the instantiation of the Job object is done in quartz, and Apporderservice is in the spring container, so how do you relate them together? Fortunately quartz provides the Jobfactory interface, allowing us to customize the logic for creating the job.
Public interface Jobfactory {
Job Newjob (triggerfiredbundle bundle, Scheduler Scheduler) throws schedulerexception;
}
Then we implement the Jobfactory interface, after the instantiation of the job, after the ApplicationContext 将Job所需要的属性注入即可The Org.springframework.scheduling.quartz.SchedulerFactoryBean class is used when spring integrates with quartz. The source code below, we only see the most critical place.
//Get Scheduler instance from schedulerfactory.            try {This.scheduler = Createscheduler (schedulerfactory, this.schedulername);            Populateschedulercontext (); if (!this.jobfactoryset &&!) ( This.scheduler instanceof Remotescheduler) {//Use Adaptablejobfactory as default for a local scheduler,                Unless when//explicitly given a null value through the ' jobfactory ' bean property. This.jobfactory = new   adaptablejobfactory            ();                    if (this.jobfactory! = null) {if (this.jobfactory instanceof Schedulercontextaware) {                ((Schedulercontextaware) this.jobfactory). Setschedulercontext (This.scheduler.getContext ());            } this.scheduler.setJobFactory (This.jobfactory); }        }
Where the red flag is the focus, if we do not specify Jobfactory, then spring uses adaptablejobfactory. Let's take a look at the implementation of this class
Package Org.springframework.scheduling.quartz;import Java.lang.reflect.method;import Org.quartz.Job;import Org.quartz.scheduler;import Org.quartz.schedulerexception;import Org.quartz.spi.jobfactory;import Org.quartz.spi.triggerfiredbundle;import Org.springframework.util.reflectionutils;public Class  Adaptablejobfactory implements Jobfactory {public Job newjob (triggerfiredbundle bundle, Scheduler Scheduler) throws    schedulerexception {return newjob (bundle); Public Job Newjob (Triggerfiredbundle bundle) throws Schedulerexception {try {Object jobobject = C            Reatejobinstance (bundle);        Return Adaptjob (Jobobject);        } catch (Exception ex) {throw new Schedulerexception ("Job instantiation Failed", ex);  }} protected Object createjobinstance (Triggerfiredbundle bundle) throws Exception {//reflectively adapting        To differences between Quartz 1.x and Quartz 2.0 ... Method Getjobdetail = Bundle.getclaSS (). GetMethod ("Getjobdetail");        Object Jobdetail = Reflectionutils.invokemethod (Getjobdetail, bundle);        Method Getjobclass = Jobdetail.getclass (). GetMethod ("Getjobclass");        Class Jobclass = (Class) Reflectionutils.invokemethod (Getjobclass, Jobdetail);    return Jobclass.newinstance (); } Protected Job Adaptjob (Object jobobject) throws Exception {if (jobobject instanceof Job) {return        (Job) Jobobject;        } else if (Jobobject instanceof Runnable) {return new Delegatingjob ((Runnable) jobobject); } else {throw new IllegalArgumentException ("Unable to execute job class [" + Jobobject.getclass (). Getna        Me () + "]: only [Org.quartz.Job] and [java.lang.Runnable] supported."); }    }}
The rest of us do not care, we look at the red place, here is the creation of a job, then we are here to inject the properties of the job, let us write a class to inherit it, and then replication this method to inject the job.
public class Myjobfactory extends Adaptablejobfactory {    //This object spring will help us to inject automatically, also belongs to the Spring Technology category.    @Autowired    private autowirecapablebeanfactory capablebeanfactory;        Protected object Createjobinstance (Triggerfiredbundle bundle) throws Exception {        //method of calling Parent class        object jobinstance = Super.createjobinstance (bundle);        To inject, this is a spring technology that is not clear to see the Spring API.        Capablebeanfactory.autowirebean (jobinstance);        return jobinstance;}    }
Then put him in spring.
<bean id= "Jobfactory" class= "Com.gary.operation.jobdemo.demo1.MyJobFactory" ></bean>
Then set the jobfactory of Org.springframework.scheduling.quartz.SchedulerFactoryBean to our own.
<bean name= "Myscheduler" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<!--other properties omitted--<property name= "jobfactory" ref= "Jobfactory" ></property></bean>
This completes the spring to the job injection function, actually is very simple, the principle is in our extension jobfactory creates the job the method, after creates the job to carry on the attribute injection.

Http://www.cnblogs.com/daxin/p/3608320.html

http://my.oschina.net/hhaijun/blog/698498

http://blog.csdn.net/fenglibing/article/details/6847158
http://blog.csdn.net/whaosy/article/details/6298686

Quartz and Spring integration job how to automatically inject spring container-managed objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.