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 * * * ?" 表示两分执行一次

本文来源于https://www.cnblogs.com/xiaochangwei/p/5425618.html 如有不当,请告知删除

猜你喜欢

转载自blog.csdn.net/MISSRIVEN/article/details/86705723
今日推荐