spring 定时任务

关于spring 定时任务大概有2中 java

 

从配置上来讲 大概分为2中1中是 在xml中配置 一堆东西,另一种是基于注解的 python

我要说的是关于注解的 第一种从网上能够看到好多 spring

说一下起因, spring-mvc

  个人整个程序是都是基于注解的,最近要加一个新需求是作统计报表的。天天统计一次便可,其实python挺好 惋惜不会,故就使用spring 的定时任务了 mvc

由于在跑任务过程当中用到了dao ,并且个人dao都是基于注解的,因此就整个沿用注解了 ide

 

先看下类 spa

1接口 component

/***
*
* @author skyyan
*
*/
public interface IStatiticsTask {
    public void execute() throws Exception; xml

} 接口

2.实现

package service.impl.task;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/***
* 统计任务
* @author skyyan
*
*/
@Component("scheduledTaskManager")
public class StatiticsTask implements IStatiticsTask{
    @Autowired
    private IStatiticsService statiticsService;
    private static int cnt=0;
    public void excuteJob() throws Exception{

/***
         * TODO 替换成本身的逻辑
         */

        System.out.println("statiticsService="+statiticsService);
        System.out.println("**************************");
        System.out.println("-------任务StatiticsTask 开始执行["+(cnt)+"]开始-----------");
        String currentDate=ELFunction.getDateDec(new Date(), 1);
        System.out.println("currentDate=="+currentDate);
        statiticsService.addTotalStatitics(currentDate);
        System.out.println("-------任务StatiticsTask 开始执行["+(cnt)+"]结束-----------");
        System.out.println("**************************");
        cnt++;
    }
    @Override
//    @Scheduled(cron="0 0 1 * * ?")天天凌晨1点
//    @Scheduled(cron="0/5 * * * * ?")//每5秒钟 
   @Scheduled(cron="0 0 1 * * ?")//天天凌晨1点执行一次;主要在这里
    public void execute() throws Exception {
        try {
            excuteJob();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

-------------

配置文件

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
      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/mvc
               http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
               http://www.springframework.org/schema/util
               http://www.springframework.org/schema/util/spring-util-3.0.xsd
                http://www.springframework.org/schema/task
             http://www.springframework.org/schema/task/spring-task-3.0.xsd
               "
    default-init-method="init" default-autowire="byName">

       <!-- 定时开始 -->      <task:annotation-driven/>        <context:component-scan base-package="service.impl.task"/>     <!-- 定时 结束-->    </beans>

相关文章
相关标签/搜索