SPRINGMVC Integration Quartz implement timed tasks and Tomcat services perform initialization of timed tasks

Source: Internet
Author: User

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

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.