ssm定时任务的实现

一、单独建立一个目录结构用来存放定时任务

二、创建task的配置文件 我起的名字是spring-task.xml,并将配置文件在web.xml中进行配置。如下所示
这里写图片描述

三,补充spring-task.xml

<?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:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
       default-autowire="byName" default-lazy-init="false">
    <!--配置包扫描-->
    <context:component-scan base-package="util.timer" />

    <!-- 定时任务配置 scheduler 方式 注解 -->
    <task:executor id="executor" pool-size="5"/>
    <task:scheduler id="scheduler" pool-size="10"/>
    <task:annotation-driven executor="executor" scheduler="scheduler"/>

</beans>

记得编写的Java定时任务类要放在包扫描的路径下

四、编写定时“`

package util.timer;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
* Created by Administrator on 2017/12/20.
 */

@Component
public class TimerTask {
    @Scheduled(cron = "0/2 * * * * ?")//每隔2秒隔行一次
    public void test2()
    {
        System.out.println("job2 开始执行");
    }
}

CRON表达式 含义
“0 0 12 * * ?” 每天中午十二点触发
“0 15 10 ? * *” 每天早上10:15触发
“0 15 10 * * ?” 每天早上10:15触发
“0 15 10 * * ? *” 每天早上10:15触发
“0 15 10 * * ? 2005” 2005年的每天早上10:15触发
“0 * 14 * * ?” 每天从下午2点开始到2点59分每分钟一次触发
“0 0/5 14 * * ?” 每天从下午2点开始到2:55分结束每5分钟一次触发
“0 0/5 14,18 * * ?” 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
“0 0-5 14 * * ?” 每天14:00至14:05每分钟一次触发
“0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44触发
“0 15 10 ? * MON-FRI” 每个周一、周二、周三、周四、周五的10:15触发

转载自:https://blog.csdn.net/fhjdzkp/article/details/78854076

猜你喜欢

转载自blog.csdn.net/fight_man8866/article/details/81303778