For the integration of spring MVC, MyBatis, maven, see the previous article
Quartz is a powerful jar package that can perform scheduled tasks on the web side.
This article is divided into two different configurations, the first XML configuration, the second note configuration
-----The following is an XML configuration article-----
1. Configuration
In the Spring-mvc.xml file
Here are two configurations, the first is Simpletigger (note), the second is Crontiggerbean
<!--Scheduled Tasks start - <Beanname= "Job1"class= "Org.springframework.scheduling.quartz.JobDetailBean"> < Propertyname= "Jobclass"value= "Com.cjt.quartz.Job1" /> < Propertyname= "Jobdataasmap"> <Map> <entryKey= "Timeout"value= "5" /> </Map> </ Property> </Bean> <BeanID= "Crontrigger"class= "Org.springframework.scheduling.quartz.CronTriggerBean"> < Propertyname= "Jobdetail"ref= "Job1" /> < Propertyname= "Cronexpression"value= "0 0/1 * * *?" /> <!--run once per minute -- </Bean> <!--<bean id= "Simpletrigger" class= "Org.springframework.scheduling.quartz.SimpleTriggerBean" > <property Name= "Jobdetail" ref= "Job1"/> <property name= "Startdelay" value= "+"/> <property name= "RepeatInte Rval "value="/> </bean> - <Beanclass= "Org.springframework.scheduling.quartz.SchedulerFactoryBean"> < Propertyname= "Triggers"> <List> <refBean= "Crontrigger" /> </List> </ Property> </Bean> <!--Scheduled Tasks end -
Pom.xml Add quartz jar package in maven (note that spring does not support Quartz version 2.0 or more)
<Properties> <quartz.version>1.8.5</quartz.version> </Properties> <Dependencies> <Dependency> <groupId>Org.quartz-scheduler</groupId> <Artifactid>Quartz</Artifactid> <version>${quartz.version}</version> </Dependency></Dependencies>
2. Programming
Job1.java
Public classJob1extendsQuartzjobbean {Private inttimeout; Private Static inti = 0; //after scheduling the factory instantiation, execute the schedule after timeout time Public voidSetTimeout (intTimeout) { This. Timeout =timeout; } /*** Specific tasks to dispatch*/@Overrideprotected voidExecuteinternal (Jobexecutioncontext context)throwsjobexecutionexception {System.out.println ("Scheduled Task execution ..."); } }
-----Below is the annotation configuration Chapter-----
1. In the Spring-mvc.xml, add:
<task:annotation-driven /> < Context:annotation-config /> < class= " Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> < base-package= "Com.cjt.quartz"
Add in xmlns
xmlns:task= "Http://www.springframework.org/schema/task"
Join in Xsi:schemalocation
http://www.springframework.org/schema/task http://www.springframework.org/ Schema/task/spring-task-3.1.xsd
2. Code
Package Com.cjt.quartz; Import org.springframework.scheduling.annotation.Scheduled; Import org.springframework.stereotype.Component; @Component Public class Job2 { @Scheduled (cron= "0/5 * * * * * ?) ") publicvoid Excute () { System.out.println (" Execute task Job2 Excute ... "); }
3. Join the MAVEN Package
Please refer to the XML configuration chapter above
Download the project:
Https://github.com/cjt321/MavenSpirngmvcMybatis
Java spring-spring and Quartz integration