SpringTask 基于注解方式的实现

  • SpringTask是一个集成在Spring框架中的轻量级的定时器功能。支持注解和配置文件两种实现方式。本文介绍使用注解方式实现定时器。
  • SpringTask默认是单线程执行的,无论有多少方法都要排队执行。 多任务并行执行需要设置’pool-size’参数。
  • 用到的jar包:
    -这里写图片描述
    配置文件:spring-mvc.xml
<?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:aop="http://www.springframework.org/schema/aop" 
            xmlns:task="http://www.springframework.org/schema/task"  
            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/context 
            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <context:component-scan base-package="com.project.controller" /> 
    <task:annotation-driven/>
    <task:scheduler id="taskScheduler" pool-size="100" />
</beans>
  • 配置中component-scan是默认扫描的文件路径,根据项目的实际情况修改base-package内容
  • java实现:
package com.project.controller.task;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class testTimeTask {
    @Scheduled(cron = "0 0/2 * * * ?")
    public void test1() throws ParseException {
        System.out.println("阻塞线程触发");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date = sdf.parse("2018-02-26 12-58-00");
        long longDate = date.getTime();
        do{
        }while(longDate-System.currentTimeMillis()>0);
    }
    @Scheduled(cron = "0 0/1 * * * ?")
    public void test2() {
        Date dt = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
        System.out.println("TEST2定时执行 :"+sdf.format(dt));
    }
    @Scheduled(cron = "0 0/1 * * * ?")
    public void test3() {
        Date dt = new Date();  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");  
        System.out.println("TEST3定时执行 :"+sdf.format(dt));
    }
}
  • @Scheduled 注解是定时器注解,cron 中的数据是定时器执行时间表达式,可参考quartz 表达式。
  • 输出:
  • TEST2定时执行 :2018年02月26日 11时31分00秒
    TEST3定时执行 :2018年02月26日 11时31分00秒
    TEST3定时执行 :2018年02月26日 11时32分00秒
    TEST2定时执行 :2018年02月26日 11时32分00秒
    阻塞线程触发

  • 曾经在对SpringTask实际运用时遇到了定时器阻塞的问题,究其原因是没配置“pool-size”这个参数,这就导致假如一个项目有A、B、C三个定时任务,三个任务是顺序执行的,A在执行时B、C需要等待,直到A任务完成。

猜你喜欢

转载自blog.csdn.net/leo187/article/details/79375877
今日推荐