Use Quartz to complete timed tasks under Spring

Source: Internet
Author: User
Tags aop xmlns

The SPRINGMVC framework often uses timed tasks to initialize projects or perform tasks on a timed basis, which can be accomplished using quartz, which is powerful and simple, the first time a project is tried, and can be used independently, but the principle is still to be summed up slowly, the use of the process is written down, The right to summarize, but also for the use of students to provide a reference

The first step is to introduce quartz in the Pom.xml file, in the following format

<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactid>quartz</ artifactid>
	<version>2.2.1</version>
</dependency>

The second step is to write the classes and methods of Java code that need to be executed on a regular basis (the class name and method name of the class need to be configured in the Spring-quartztask.xml file), with the following code:

Package Com.lgg.web.quartz;
Import java.util.Date;
Import Org.slf4j.Logger;
Import org.slf4j.LoggerFactory;

@Component public
class Modifyusernametask {
	private static final Logger Logger = Loggerfactory.getlogger ( Modifynametask.class);
	@Autowired
	private UserService userservice;
	Periodically modify the user name public 
	void Taskjob () {
		logger.info ("Scheduled task starts ....");
		Call the method Userservice.modifyname () to modify the service layer of the user name on a timed basis
		;
		Logger.info ("Timed end ....");}
The third step, add the Spring-quartztask.xml file, configure the Quartz property information, the following:

<?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 "> <!--change user name--<!-- The full path of the class in the second step is written to class, and the ID needs to name itself, cannot be duplicated, and <bean id= "Modifyusernametask" class= " Com.com.lgg.web.quartz.ModifyUserNameTask "></bean> <!--ID needs a name, class does not need to be modified--<bean id="
		Moddesktopreviewdetail "class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> <property name= "TargetObject" ref= "Moddesktopreviewtask"/><!--the value in ref corresponds to the ID of your first bean, both must be the same--< Property Name= "Targetmethod" value= "Taskjob"/><!--value corresponds to the method name in the Java class in the second step----<property name= " Concurrent "value=" false "/><!--multiple jobs will not run concurrently and the second job will not start until the first job is completed. -</bean> <!--This bean's ID needs to be used in the list below,Class does not need to be modified--<bean id= "Moddesktopreviewtrigger" class= " Org.springframework.scheduling.quartz.CronTriggerFactoryBean "> <property name=" jobdetail "ref=" Moddesktopreviewdetail "/><!--the same value as the ID of the second bean above--<property name=" cronexpression "value=" 0 0 3 * *? " /><!--daily 3 o'clock in the morning <property name= "description" value= "Timed change username" ></property> </bean> < Bean id= "Startscheduler" lazy-init= "true" autowire= "no" class= "
		Org.springframework.scheduling.quartz.SchedulerFactoryBean "> <property name=" Autostartup "value=" true "/> <property name= "Schedulername" value= "Cronscheduler"/> <property name= "triggers" > <list> <! --Add a value for the ID of the third bean above--<ref bean= "Moddesktopreviewtrigger"/> </list> </property> </bean > </beans>

Fourth step, configure the load path of the Spring-quartztask.xml file in the Web project App_config.xml, 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 "xmlns:aop=" http://www.springframework.org/ Schema/aop "xmlns:tx=" Http://www.springframework.org/schema/tx "xmlns:context=" http://www.springframework.org/ Schema/context "xmlns:util=" Http://www.springframework.org/schema/util "xsi:schemalocation=" Http://www.spr Ingframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.1.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http ://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd "> < Import resource= "Spring-quartztask.xml" ></import> </beans> 
The entire configuration is complete. Set the startup time, and the project will start after the start time.




Related Article

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.