Spring mvc 定时器实现

1.首先在dispatcher-servlet.xm中引入定时器的配置文件

  如:<import resource="quartz/quartz-data.xml"/>

 2.定时器中配置文件配置相关信息

<?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:p="http://www.springframework.org/schema/p"
       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"
       xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
       xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">

          <bean id="cleanSmsCash"  class="com.fpi.safety.datahandler.common.cache.CleanSmsCash"></bean>
            <!-- 定义系统激活调用对象和调用对象的方法 -->
           <bean id="cleanSmsJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
                <!-- 调用的类 -->
                <property name="targetObject">
                    <ref bean="cleanSmsCash"/>
                </property>
                  <!-- 调用类中的方法 -->
                <property name="targetMethod">
                    <value>execute</value>
                </property>
                <property name="concurrent">
                    <value>true</value>
                </property>
          </bean>  
                
         <!-- 定义触发时间 -->
        <bean id="cTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="cleanSmsJobDetail"/>
            </property>
            <!-- cron表达式 -->
            <property name="cronExpression">
                <!-- 每隔10分钟执行一次 -->
                <value>0 0/10 * * * ?</value>
                <!-- 每隔4小时执行一次 -->
               <!--  <value>0 0 0/4 * * ?</value> -->
            </property>
        </bean>
         
         
         
        <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
        <bean id="startQuertz"  lazy-init="false" autowire="no"   class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                   
                        <ref bean="cTime"/>  <!-- 系统有效期定时任务  -->
                </list>
            </property>
        </bean>
 
</beans>

3.编写需要定时执行的类

package com.fpi.safety.datahandler.common.cache;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fpi.safety.datahandler.common.cache.model.SmsInfo;

/**清除所有短信缓存*/
public class CleanSmsCash {
    private Logger log = LoggerFactory.getLogger(CleanSmsCash.class);
    public void execute(){
        Map<String,SmsInfo> smsMap = SmsCache.INSTANCE.getAllSmsCache();
        if(!smsMap.isEmpty()){
            for(String key : smsMap.keySet()){
                 log.info("-----------------------------------------得到短信缓存的手机号码为--------------------------------"+key);
              }
        }
        SmsCache.INSTANCE.cleanAllSmsCache();
        log.info("-----------------------------------------清除了所有短信缓存--------------------------------");
    }
}
 

扫描二维码关注公众号,回复: 4982049 查看本文章

猜你喜欢

转载自blog.csdn.net/bird_tp/article/details/86293547