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 !

posted @   肖哥哥  阅读(12134)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
生命不息  奋斗不止  每天进步一点点
点击右上角即可分享
微信分享提示