1. Import the Quartz.jar package, or the pom.xml configuration corresponding to the Dependency:
<Dependency> <groupId>Org.quartz-scheduler</groupId> <Artifactid>Quartz</Artifactid> <version>1.8.6</version> </Dependency>
2. Configure the Quartz configuration file load path in Web project Xml:
<servlet> <Servlet-name>Roundtrip</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <!--You can customize the location and name of the Servlet.xml configuration file by default to the Web-inf directory with the name [<servlet-name>]-servlet.xml, such as Spring-servlet.xml - <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:spring/roundtrip-servlet.xml,classpath:spring/spring-quartz.xml</Param-value> </Init-param> </servlet>
3. Write the specific scheduled tasks:
package andy.test.quartz.schedule; /** * */publicclass myprintschedule {public void printsomething () { // content is to print a word System.out.println ("this is Andy schedule");} }
4. Configure the XML configuration information for the quartz, the file name is consistent with the web
Spring-quartz.xml
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "http://www.springframework.org/schema/beans"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!--Add a scheduled task bean configuration corresponding to the class - <BeanID= "myprintschedule"class= "andy.test.quartz.schedule.MyPrintSchedule" /> <!--How to configure a schedule for specific execution - <BeanID= "myprintdetail"class= "org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> < propertyname= "targetObject"ref= "myprintschedule" /> < propertyname= "targetmethod"value= "printsomething" /> < propertyname= "concurrent"value= "false" /> </Bean> <!--to configure the trigger time for a scheduled execution - <BeanID= "myprinttrigger"class= "org.springframework.scheduling.quartz.CronTriggerBean"> < propertyname= "jobdetail"ref= "myprintdetail" /> < propertyname= "cronexpression"> <!--perform task scheduling at 8 o ' Day in the morning - <value>0 0 8 * *?</value> </ property> </Bean> <!--Quartz Dispatch Factory Dispatch factory can only have one, multiple scheduling tasks are added in the list - <Beanclass= "org.springframework.scheduling.quartz.SchedulerFactoryBean"> < propertyname= "triggers"> <List> <!--List of all schedules - <refLocal= "myprinttrigger" /> </List> </ property> </Bean></Beans>
The implementation of the Myprint task scheduling, execution time is every morning at 8 o ' day, print:
This is Andy Schedule
This sentence.
Quartz's function is very powerful, the use is also very simple and convenient, something needs to do some initialization tasks when the Web starts, This is the execution of the configuration corresponding to the execution schedule of the trigger time can be completed. The specific implementation is as Follows:
<!--to configure the trigger time for a scheduled execution - <BeanID= "myprinttrigger"class= "org.springframework.scheduling.quartz.CronTriggerBean"> < propertyname= "jobdetail"ref= "myprintdetail" /> < propertyname= "cronexpression"> <!--do not repeat count, only once - < propertyname= "repeatcount"value= "0" /> </ property> </Bean>
The above configuration is complete, and the server initiates the task Performed. Be executed only once
SPRINGMVC Integration Quartz implement timed tasks and Tomcat services perform initialization of timed tasks