spring定时器(注解的形式)

最近有个需求,要在凌晨的时候,根据某几张表生成一张定时任务表里的数据,数据的状态为0(未整改),然后在当天晚上,再把这些数据的状态没改变的,改变状态为1(待整改),然后要用到定时器,百度了一下用注解形式的很方便,还能在一个方法里有多个定时任务,所以就试着试了一下,详情如下:

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-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd ">
       
    <!-- 配置task任务扫描注解 -->
    <task:annotation-driven/>
    
    <!-- 指定task任务扫描位置 -->
    <context:annotation-config/> 
        <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
    <context:component-scan base-package="com.infohold.city.map.controller.web"/>
    
</beans> 

web.xml中配上spring-task.xml的加载:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-dao.xml,classpath:spring-mvc.xml,classpath:spring-service.xml,classpath:spring-task.xml</param-value>
  </context-param>

然后就可以通过注解实现定时任务啦,

package com.infohold.city.map.controller.web;

import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import com.infohold.city.map.service.TaskService;

@Controller
@Component
public class TaskController{

    @Resource
    private TaskService taskService;
    
    /**
     * 
     * 定时器--添加排查任务
     * 每天凌晨0:50定时生成数据
     */
    @Scheduled(cron="0 55 0 * * ?")
    public void getCompanyCheckTask() {
        taskService.getCompanyCheckTask();
    }
    
    /**
     * 
     * 定时器--修改排查任务状态为3(未执行)
     * 定时器每天晚上23:50   修改当天凌晨0:50到1:50的数据
     */
    @Scheduled(cron="0 50 23 * * ? ")
    public void updateCompanyCheckTask() {
        taskService.updateCompanyCheckTask();
    }
}

需要注意的是,刚开始我为了偷懒就把定时器的配置加在了spring-mvc.xml中,后来发现定时器可以启动成功,但是接口敲完也测完,发布到服务器之后,发现数据库里生成了两条一模一样的数据,检查代码没发现有啥不妥,当时的serviceimpl层的代码如下:

package com.infohold.city.map.service.impl;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.infohold.city.map.dao.mybatis.CompanyCheckTaskDao;
import com.infohold.city.map.dao.mybatis.CompanycheckplanmonthDao;
import com.infohold.city.map.dao.mybatis.CompanycheckplanmonthitemDao;
import com.infohold.city.map.dao.mybatis.TaskDao;
import com.infohold.city.map.model.CompanyCheckPlanMonth;
import com.infohold.city.map.model.CompanyCheckPlanMonthItem;
import com.infohold.city.map.model.CompanyCheckTask;
import com.infohold.city.map.service.TaskService;
import com.infohold.city.map.util.CommonUtil;

@Service
@Transactional
public class TaskServiceImpl implements TaskService{
    @Resource
    private TaskDao taskDao;
    @Resource
    private CompanycheckplanmonthDao companycheckplanmonthDao;
    @Resource
    private CompanycheckplanmonthitemDao companycheckplanmonthitemDao;
    @Resource
    private CompanyCheckTaskDao companyCheckTaskDao;
    
    @Override
    public void getCompanyCheckTask() {
        //月度计划+月度计划项  列表
        List<CompanyCheckPlanMonth> companycpmlist=companycheckplanmonthDao.getCompanyCheckPlanMonthList();
        GregorianCalendar calendar=new GregorianCalendar();
        // 取出当前的年,月,日
        int year=calendar.get(calendar.YEAR);
        // 月的数值加1,使之变成习惯的月份大小(1~12月)
        int month=calendar.get(calendar.MONTH)+1;
        int today=calendar.get(calendar.DAY_OF_MONTH);
        
        for (CompanyCheckPlanMonth companyCheckPlanMonth : companycpmlist) {
            List<CompanyCheckPlanMonthItem> Companycheckplanmonthitemlist = companyCheckPlanMonth.getGetCompanycheckplanmonthitemlist();
            int i=1;
            for (CompanyCheckPlanMonthItem companyCheckPlanMonthItem : Companycheckplanmonthitemlist) {
                CompanyCheckTask companyCheckTask = new CompanyCheckTask();
                companyCheckTask.setId(CommonUtil.getUUID());
                String num = String.format("%04d",i);
                companyCheckTask.setName(companyCheckPlanMonthItem.getName()+month+today+num);
                companyCheckTask.setState("0");
                companyCheckTask.setCreateUser("定时器机器人");
                companyCheckTaskDao.insertCompanyCheckTask(companyCheckTask);
                i++;
            }
        }
    }

    @Override
    public void updateCompanyCheckTask() {
        CompanyCheckTask companyCheckTask = new CompanyCheckTask();
        companyCheckTask.setCheckTime(new Date());
        companyCheckTask.setState("3");
        companyCheckTask.setCreateUser("定时器机器人");
        companyCheckTaskDao.updateCompanyCheckTask1(companyCheckTask);
    }
    
}

在线Cron自动生成器:http://cron.qqe2.com/

检查了很多遍代码,发现没有问题,然后本地测试也是就生成一次代码,但是为什么代码提交到服务器上就会执行两次呢,百度了老半天,有说tomcat配置的问题,有说spring加载了两次呆滞的问题,后来尝试着把配置文件从springmvc.xml中隔离出来,果然好使,所以这以后的配置文件还是不能瞎加啊,借此警告自己。作为一名小菜鸟程序员,还是不能偷懒啊~~

原文地址:https://www.cnblogs.com/supiaopiao/p/8682869.html