基于SSM框架的定时任务

项目需要,需要使用定时器,基于SSM框架实现,下面是我的配置过程(基于注解)
首先在springMVC的配置文件头部加入

xmlns:task="http://www.springframework.org/schema/task
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd

整体为:

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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.2.xsd">

这个应该是比较完整的,我现在的项目就是这个配置
中间加入:

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

配置包扫描:

<context:component-scan base-package="com.java.cn">
    <!-- 制定扫包规则 ,只扫描使用@Controller注解的JAVA类 -->
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Component" />
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Service" />
</context:component-scan>

因为我是在现有项目上面做的,原本已经配置包扫描规则(除了service之外的都扫),我试了一下中间的Component注解方式的包扫描是可以不加的,不影响定时任务执行

到这里包扫描的配置文件就写好了,现在开始写配置类:

package com.java.cn.interceptor;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

/**
 * 定时执行任务(统计分析和预警功能)
 * 
 * @author Administrator
 *
 */
@Component
public class TimerManager {
    @Bean
    public TaskScheduler scheduledExecutorService() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(8);
        scheduler.setThreadNamePrefix("scheduled-thread-");
        return scheduler;
    }

    /**
     * 每天22点30启动任务
     */
    @Scheduled(cron = "0/2 * * * * ?") // 每隔两秒执行一次
    public void test1() {
        Date date = new Date();
        System.out.println("当前时间为:" + date);
        System.out.println("两秒测试集");
    }

    @Scheduled(cron = "0/5 * * * * ?") // 每隔5秒隔行一次
    public void test2() {
        System.out.println("两秒测试集     *****************       ********     ");
    }
}

我的需求是每天10点执行一次,扫数据库,然后根据结果向用户发送短信,为了当前效果明显,所以我设置了两秒执行一次,具体的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触发

在这里需要注意!!!!配置类中@Bean把一部分一定要加,不然会报错,具体信息请查看另一边文章:
https://blog.csdn.net/single_cong/article/details/81326094
到这里定时任务的配置就完成了,项目启动就能很快看到效果了:
这里写图片描述
我们可以看到的确是两秒执行一次方法。

猜你喜欢

转载自blog.csdn.net/single_cong/article/details/81327085
今日推荐