Springboot Tour--Scheduled Tasks two (Spring Schedule and Quartz integration) implementation

Source: Internet
Author: User
Tags fdf

Related articles

Spring Boot Related articles Directory

Objective

The recent use of scheduled tasks in the project has been implemented using quartz, and recently, spring based discovery actually spring provides spring Schedule to help us implement simple scheduled tasks.
Here are two ways to use the Spring Boot project.

Spring Schedule implements timed tasks

Spring Schedule implements timed tasks in two ways, 1. Configure the timer task with XML, 2. Use @Scheduled annotations. Because it is the spring Boot project, it is possible to avoid using the form of XML configuration, mainly in the form of annotations.

Spring Schedule offers three types of scheduled tasks:

Fixed wait time @Scheduled (Fixeddelay = time interval)
@ComponentPublicClass Schedulejobs {PublicFinalStaticLong SECOND =1 *1000; Fastdateformat FDF = Fastdateformat.GetInstance ("Yyyy-mm-dd HH:mm:ss");@Scheduled (Fixeddelay = SECOND *2) public void  fixedDelayJob  () throws interruptedexception {timeunit.sleep (2); System. out. println ( "[Fixeddelayjob Execute]" +fdf .format (new Date ()));}}       
Fixed interval @Scheduled (fixedrate = time interval)
@ComponentPublicClass Schedulejobs {PublicFinalStaticLong SECOND =1 *1000; Fastdateformat FDF = Fastdateformat. getinstance ( "Yyyy-mm-dd HH:mm:ss");  @Scheduled (fixedrate = SECOND * 4) public void fixedratejob () { System. out. println ( "[Fixedratejob Execute]" +fdf .format (new Date ()));}}       
Corn expression @Scheduled (cron = corn expression)
@ComponentPublicClass Schedulejobs {PublicFinalStaticLong SECOND =1 *1000; Fastdateformat FDF = Fastdateformat.getinstance ( "Yyyy-mm-dd HH:mm:ss");  @Scheduled (cron =  "0/4 * * * * *?") public span class= "DT" >void cronjob () {System.println ( "[Cronjob Execute]" +fdfnew Date ()))}}         
Spring Boot Integration Quartz to add Maven dependencies for timed tasks
        <Dependency><Groupid>org.quartz-scheduler</Groupid><artifactid>quartz</artifactid> </dependency> <dependency > <groupid>org.springframework </groupid> <artifactid>spring-context-support</artifactid> </DEPENDENCY>            
Spring Boot Integration Quartz

Spring Project Consolidation Quartz mainly relies on adding schedulerfactorybean to this factorybean, so add Spring-context-support to maven dependencies.

First, add the Quartzconfig class to declare the related bean

@ConfigurationPublicClass Quartzconfig {@AutowiredPrivate Springjobfactory springjobfactory;@BeanPublicSchedulerfactorybeanschedulerfactorybean () { Schedulerfactorybean Schedulerfactorybean = new setjobfactory (springjobfactory); return Schedulerfactorybean;}  @Bean  Public Scheduler  scheduler () {< Span class= "Hljs-keyword" >return schedulerfactorybean ().  

Here we need to note that I injected a custom jobfactory and then set it to Schedulerfactorybean's jobfactory. The purpose is because I need spring to inject some service into the specific job.
So we're going to customize a jobfactory to use the Spring API for Dependency injection when the specific job class is instantiated.

Springjobfactory Specific implementations:

@ComponentPublicClass SpringjobfactoryExtends adaptablejobfactory {@Autowiredprivate Autowirecapablebeanfactory capablebeanfactory; @Override protected Object createjobinstance(triggerfiredbundle bundle) throws  Exception {Object jobinstance = super.  Createjobinstance (bundle); Capablebeanfactory. Autowirebean (jobinstance); return jobinstance;}}           

Specific use (extracted from Project code):

@ServicePublicClass QuartzeventserviceimplImplements Quartzeventservice {PrivateStaticFinal String Job_group ="Event_job_group";PrivateStaticFinal String Trigger_group ="Event_trigger_group";@AutowiredPrivate Scheduler Scheduler;@OverridePublic void Addquartz(Event event)Throws schedulerexception {Jsonobject eventData = Jsonobject.getdate ( "date"); Jobdetail job = Jobbuilder. newjob (Eventjob.withidentity (Event.tostring (), Job_group). usingjobdata (buildjobdatemap (event)). newtrigger (). withidentity (Event.tostring (), Trigger_group). startat (triggerdate). build (); scheduler. schedulejob (Job, trigger);}           
Category: Spring Boot

Springboot Tour--Scheduled Tasks two (Spring Schedule and Quartz integration) implementation

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.