Functional Development of Spring Timer

1. Use the quartz.jar package to implement the timer function. First, introduce the maven dependency of the timer package into the project
<!-- spring timer-->
<dependency>
    <groupId>opensymphony</groupId>
    <artifactId>quartz-all</artifactId>
    <version>1.6.6</version>
</dependency>


2. Configure the timer function in spring-time.xml.
<?xml version="1.0" encoding="gb2312"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:task="http://www.springframework.org/schema/task"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/task   
    http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    
    <!-- Timer switch-->  
    <task:annotation-driven />
	
	<!-- Test whether the spring timer can be used -->
    <bean id="testSpringTimeService" class="org.itava.service.impl.TestSpringTimeServiceImpl">
	</bean>
	
    <task:scheduled-tasks>  
        <!-- Execute once every 10s -->
        <task:scheduled ref="testSpringTimeService" method="testQuart" cron="0/10 * * * * ?"/>  
    </task:scheduled-tasks>  
	 
</beans>


3. Load the spring-time.xml file in web.xml
<!-- Spring and mybatis configuration files -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>
        	classpath:spring-mybatis.xml,
        	classpath:spring-time.xml
        </param-value>  
    </context-param>  


4、TestSpringTimeServiceImpl内容
package org.itava.service.impl;

public class TestSpringTimeServiceImpl {

	public void testQuart(){
		System.out.println("Spring timer test...");
	}
}



5. The execution result after starting the project, I configure it to execute once every 10s.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326534641&siteId=291194637