最近项目上用到了spring3的定时器
于是大概配置了个,测试了下没问题就开始使用了。结果项目开始运行后,出错了。错误很奇怪,麻烦大家帮看看定时器的任务是:每天晚上22点调用,进行数据库的读取和写入。项目是5.20开始发布的,5.20-5.24都是正常的,每天晚上10点都执行了,也正常的流程
本以为没什么大问题了,结果从5.25开始出问题出的问题是从5.25开始 每2天1次的执行。期间什么也没改,什么也没动于是5.25、5.27、5.29、5.31、6.2、6.4都没执行任务
而是5.26、5.28、5.30、6.1、6.3、6.5都执行了也就是从原来的的每天都执行,编程了每2天执行1次以下是项目源码,麻烦大家帮看看首先spring的配置文件,不贴完了,贴关键的<?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:aop="http://www.springframework.org/schema/aop" 
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.1.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
http://www.springframework.org/schema/aop  
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task 
            http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<task:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="com.jiuzhai">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>然后是定时器类package com.jiuzhai.task;import java.text.SimpleDateFormat;
import java.util.Date;import javax.annotation.Resource;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import com.jiuzhai.service.PointRecordService;@Component
public class JiuZhaiTask {

private PointRecordService pointRecordService; @Scheduled(cron = "0 0 22 * * ?")
public void pointRecordDay() { try {
// System.out.println("定时调用每天的数据");
pointRecordService.neverUpdatePointRecordDay(new Date());
} catch (Exception e) {
// System.out.println("生成日记录出错");
}
} public PointRecordService getPointRecordService() {
return pointRecordService;
} @Resource
public void setPointRecordService(PointRecordService pointRecordService) {
this.pointRecordService = pointRecordService;
}


}

解决方案 »

  1.   

    个人不同意楼上看法 ,因为@Scheduled(cron = "0 0 22 * * ?") 这个不会又时差这一说吧。
    是有点奇怪。spring定时任务常用 没碰到过这个。继续关注 楼主帮不了。
      

  2.   

    我也是第一次遇到这情况
    搞不清怎么回事有人说因为操作数据库,导致cpu的占用过多
    但是出这样的问题也应该是以后的定时器都不执行了才对呢咋能这么整齐的都是2天执行1次