Spring Boot Integrated quartz inject spring-managed classes

Source: Internet
Author: User

Summary: When using quartz in spring boot, it is generally necessary to refer to spring-managed beans in the job and to implement automatic injection by defining the job factory.

Spring has its own schedule timed task, which cannot be managed dynamicallywhen used in spring boot, so it is implemented using quartz.

Configure Quartz in spring boot:

Import Java.io.ioexception;import Java.util.properties;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.config.propertiesfactorybean;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.core.io.classpathresource;import org.springframework.scheduling.annotation.EnableScheduling ; import Org.springframework.scheduling.quartz.SchedulerFactoryBean; @Configuration @enablescheduling Public classQuartzschedule {@AutowiredPrivatemyjobfactory myjobfactory; @Bean PublicSchedulerfactorybean Schedulerfactorybean () throws IOException {Schedulerfactorybean Factory=NewSchedulerfactorybean (); Factory.setoverwriteexistingjobs (true); //Delayed StartFactory.setstartupdelay ( -); //load Quartz data source configurationfactory.setquartzproperties (Quartzproperties ()); //Custom Job Factory for spring injectionfactory.setjobfactory (myjobfactory); returnFactory; }    /** * Load Quartz Data Source Configuration * * @return * @throws IOException*/@Bean PublicProperties quartzproperties () throws IOException {Propertiesfactorybean Propertiesfactorybean=NewPropertiesfactorybean (); Propertiesfactorybean.setlocation (NewClasspathresource ("/quartz.properties"));        Propertiesfactorybean.afterpropertiesset (); returnPropertiesfactorybean.getobject (); }}

In order to use spring-managed beansin the job, you need to redefine a job Factory:

@Component Public classMyjobfactory extends Adaptablejobfactory {@AutowiredPrivateautowirecapablebeanfactory capablebeanfactory; @OverrideprotectedObject createjobinstance (triggerfiredbundle bundle) throws Exception {//calling a method of the parent classObject jobinstance =super.createjobinstance (bundle); //to injectCapablebeanfactory.autowirebean (jobinstance); returnjobinstance; }}

You can then use spring-managed beans in the job.

 Public class MyJob implements Job, Serializable {    privatestaticlong1L;     Private Logger Logger = Loggerfactory.getlogger (this. GetClass ());    @Autowired    private  someservice someservice;    @Override    publicvoid  Execute (jobexecutioncontext context) throws jobexecutionexception {        someservice.dosomething ();    }}

The following code is the Create job:

Jobdetail Jobdetail =Jobbuilder.newjob ((Job) Class.forName (Job.getclazz ()). newinstance ()). GetClass ()). Withidentity (Jo            B.getjobname (), Job.getjobgroup ()). build (); Jobdetail.getjobdatamap (). Put ("Extdata", Job.getextdata ()); //Expression Scheduler BuilderCronschedulebuilder Schedulebuilder =Cronschedulebuilder.cronschedule (Job.getcronexpression ()). withmisfirehandlinginstructiondonothing            (); //Build a triggerTriggerbuilder<crontrigger> Triggerbuilder =Triggerbuilder.newtrigger (). withidentity (Triggerkey). Withschedule (Schedulebuilder); if(Job.getstarttime ()! =NULL) {Triggerbuilder.startat (Job.getstarttime ()); }            if(Job.getendtime ()! =NULL) {Triggerbuilder.endat (Job.getendtime ()); } Crontrigger Trigger=Triggerbuilder.build (); Scheduler.schedulejob (Jobdetail, trigger);//inject into the management class

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

Spring Boot Integrated quartz inject spring-managed classes

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.