前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringMVC中定时任务配置

SpringMVC中定时任务配置

作者头像
肖哥哥
发布2019-02-22 10:44:29
1.2K0
发布2019-02-22 10:44:29
举报
文章被收录于专栏:后台及大数据开发

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

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

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

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

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

代码语言:javascript
复制
<?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>

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

代码语言:javascript
复制
<?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配置中加上

代码语言:javascript
复制
<dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.3</version>
        </dependency>

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

代码语言:javascript
复制
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()));
    }

}

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

执行结果如下:

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

代码语言:javascript
复制
<task:scheduled ref="quartzTestBean" method="quartzJobTestMethod" cron="*/5 * * * * ?" />

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

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

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

代码语言:javascript
复制
cron="* */2 * * * ?"  对么?   

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

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

具体规则请百度cron吧  so easy !
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-04-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档