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

Source: Internet
Author: User
Tags tomcat server

In Java we often use timers and timertask to implement timing functions, but in a Java EE project, spring can be used to integrate the quartz timer, very powerful, able to achieve all the desired timing tasks, including the Tomcat server started to start, Regular fixed-point fixed weeks and so on, the introduction of quartz and time configuration on the internet has a lot of information, is not burdensome, the following main introduction SPRINGMVC integration Quartz implementation steps.


SOURCE Blog: Http://blog.csdn.net/fengshizty


1, import Quartz.jar package, or Pom.xml configuration corresponding 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>rest</servlet-name><servlet-class> Org.springframework.web.servlet.dispatcherservlet</servlet-class><init-param><param-name> Contextconfiglocation</param-name><param-value>/web-inf/classes/rest-servlet.xml,    /WEB-INF/ Classes/pyc-spring-quartz.xml</param-value></init-param> <load-on-startup>1</ Load-on-startup></servlet>

3, write the specific tasks of timing scheduling:

Package andy.test.quartz.schedule;/**   * @author zhang,tianyou   * @version: 2014-12-11 pm 12:00:24   *  *  A test print timed task */public class Myprintschedule {public void printsomething () {//content is printed in a word System.out.println ("This is Andy Schedule ");}}

4, the configuration quartz XML configuration information, the name can be casual, and the need to be consistent with Web.

The Pyc-spring-quartz.xml configuration information is as follows:


<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "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-->< Bean id= "Myprintschedule" class= "Andy.test.quartz.schedule.MyPrintSchedule"/> <!--configuration Schedule specific execution method--><bean Id= "Myprintdetail" class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" >< Property Name= "TargetObject" ref= "Myprintschedule"/><property name= "Targetmethod" value= "printsomething"/ ><property name= "Concurrent" value= "false"/></bean><!--Configure the time that the schedule execution is triggered--><bean id= " Myprinttrigger "class=" Org.springframework.scheduling.quartz.CronTriggerBean "><property name=" Jobdetail " ref= "Myprintdetail"/><property name= "cronexpression" ><!--perform task scheduling at 8 O ' Day in the morning--><value>0 0 8 * *? &Lt;/value></property></bean><!--Quartz The Dispatch factory dispatch factory can only have one, multiple scheduling tasks add--><bean class= "in the list" Org.springframework.scheduling.quartz.SchedulerFactoryBean "><property name=" triggers "><list> < !--All scheduling lists--><ref local= "Myprinttrigger"/></list></property></bean></beans>

This realizes the Myprint task scheduling, the execution time is every morning 8 point executes once, prints:

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:

<!--Configure the trigger time for schedule execution--><bean id= "Myprinttrigger" class= " Org.springframework.scheduling.quartz.CronTriggerBean "><property name=" Jobdetail "ref=" Myprintdetail "/> <property name= "Cronexpression" ><!--do not repeat count, only once--><property name= "repeatcount" value= "0"/></ Property></bean>

The above configuration is complete, and the server initiates the task performed. is executed only once.


SOURCE Blog: Http://blog.csdn.net/fengshizty



SPRINGMVC Consolidation Quartz Implement timed tasks and the Tomcat service performs initialization of scheduled 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.