zoukankan      html  css  js  c++  java
  • SpringMVC中定时任务配置

    在项目中使用定时任务是常有的事,比如每天定时进行数据同步或者备份等等。

    以前在从事C语言开发的时候,定时任务都是通过写个shell脚本,然后添加到linux定时任务中进行调度的。

    现在使用SpringMVC之后,一起都变得简单了o(∩_∩)o 

    有两种配置方式,我都分别讲讲,但是看了后你肯定只会选择后面那种,没错! 我也是用后面那种方式

    第一种配置方式:这个比较复杂,配置的地方有点多,稍不留意就不成功,具体看代码了

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <!-- 每隔一个小时重新获取一次token -->
        <!-- 1.要调用的工作类 -->
        <bean id="refreshAccessTokenJob" class="com.xiao.weixin.quartz.RefreshAccessToken"/>
        
        <!-- 2.定义调用对象和调用对象的方法 -->
        <bean id="refreshAccessTokenTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <!-- 调用的类 -->
            <property name="targetObject">
                <ref bean="refreshAccessTokenJob"/>
            </property>
            <!-- 调用类中的方法 -->
            <property name="targetMethod">
                <value>work</value>
            </property>
        </bean>
        
        <!-- 3.定义触发时间 -->
        <bean id="refreshAccessToken" class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="refreshAccessTokenTask"/>
            </property>
            <!-- cron表达式 -->
            <property name="cronExpression">
                <value>0 0 */2 * * ?</value>
            </property>
        </bean>
        
        
        <!-- 4.总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序  -->
        <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="refreshAccessToken"/>
                </list>
            </property>
        </bean>
    </beans>

    第二种配置方式:强烈推荐的这种

    <?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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
        <task:scheduled-tasks>
            <task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" />
        </task:scheduled-tasks>
        
        <bean id="quartzTestBean" class="xiaochangwei.zicp.net.service.QuartzTestServiceImpl"/>
    </beans>

    这里面很简单,直接调用service接口实现类中的方法就可以了,

     maven配置中加上

    <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz</artifactId>
                <version>2.2.3</version>
            </dependency>

    比如我的实现类是这样的:

    package xiaochangwei.zicp.net.service;
    
    import java.util.Date;
    
    import org.springframework.stereotype.Service;
    
    import xiaochangwei.zicp.net.common.tools.DateUtil;
    
    @Service
    public class QuartzTestServiceImpl implements QuartzTestService {
    
        public void quartzJobTestMethod() {
            System.out.println("定时任务执行:" + DateUtil.getFormatDate(new Date()));
        }
    
    }

    每隔五秒打印一个当前时间

    执行结果如下:

    定外配置任务多久执行也很简单:

    <task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" />

    上面这个表示五秒钟执行一次

    总共有五个*,从前往后表示秒,分,时。。。。。。。

    多少秒执行一次说了,说多少分执行一次

    cron="* */2 * * * ?"  对么?   

    这样不对哈,要将前面的星改为0 cron="0 */2 * * * ?" 表示两分执行一次

    同理 如果两小时执行一次,则前面两个都是0 哦

    具体规则请百度cron吧 so easy !

  • 相关阅读:
    Sharepoint 2010:基于当前用户判断访问列表项目的权限 Determine access to SPListItem based on a Current User
    vs2010修改sharepoint部署站点
    写入数据内存溢出的错误ErrorCode=2147217900解决
    新鲜网络故障:网线测试器测试,居然2,3信号灯齐亮!解决
    CuteEditor出错了,居然是没有了lic文件
    路由表的故障?如何理解设置
    MSSQL如何快速清除数据库日志转,经实践有效
    iis中web站点无法启动:另一程序正在使用该文件,进程无法访问解决
    二次开发图片浏览系统应用Lightbox image viewer 2.03a并带javascript的字符串处理
    asp session丢失的分析和解决方法
  • 原文地址:https://www.cnblogs.com/xiaochangwei/p/5425618.html
Copyright © 2011-2022 走看看