最近有个需求,要在凌晨的时候,根据某几张表生成一张定时任务表里的数据,数据的状态为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)+;
int today=calendar.get(calendar.DAY_OF_MONTH); for (CompanyCheckPlanMonth companyCheckPlanMonth : companycpmlist) {
List<CompanyCheckPlanMonthItem> Companycheckplanmonthitemlist = companyCheckPlanMonth.getGetCompanycheckplanmonthitemlist();
int i=;
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("");
companyCheckTask.setCreateUser("定时器机器人");
companyCheckTaskDao.insertCompanyCheckTask(companyCheckTask);
i++;
}
}
} @Override
public void updateCompanyCheckTask() {
CompanyCheckTask companyCheckTask = new CompanyCheckTask();
companyCheckTask.setCheckTime(new Date());
companyCheckTask.setState("");
companyCheckTask.setCreateUser("定时器机器人");
companyCheckTaskDao.updateCompanyCheckTask1(companyCheckTask);
} }

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

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

spring定时器(注解的形式)的更多相关文章

  1. Spring定时器注解配置

    spring-task.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  2. 注解式开发spring定时器

    1:spring 配置文件中增加这句    <task:annotation-driven/>  2:确保扫描程序能够扫描后  下面第3步骤的java类    <context:co ...

  3. spring的注解形式:@Repository、@Service、@Controller,

    Spring的注解形式:@Repository.@Service.@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean. @Repository.@Service.@C ...

  4. spring 基于注解的@Scheduled和quartz定时器两种实现

    一.使用quartz 1.由于我的项目jar包使用的maven托管的,在pom文件中加入quartz的依赖就可以 2.配置quartz-context.xml,确保xml文件能被加载到 <?xm ...

  5. spring定时器,定时器一次执行两次的问题

    Spring 定时器 方法一:注解形式 配置文件头加上如下: xmlns:task="http://www.springframework.org/schema/task" htt ...

  6. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  7. spring定时器,当遇见半小时的情况时

    spring定时器遇见半小时的解决方法(这里只提供注解方式) @Scheduled(fixedRate=6000000)//每隔100分钟执行方法 fixedRate的值是毫秒

  8. atititt.java定时任务框架选型Spring Quartz 注解总结

    atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz  (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...

  9. 使用轻量级Spring @Scheduled注解执行定时任务

    WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...

  10. spring定时器用Annotation兑现

    spring定时器用Annotation实现 0人收藏此文章, 我要收藏发表于3个月前 , 已有46次阅读 共0个评论 1.ApplicationContext.xml配置 a).需要在xmlns里面 ...

随机推荐

  1. HDU 5876 大连网络赛 Sparse Graph

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) T ...

  2. c#系统中类的方法 Console、Object,ToolStripDropDownItem,string

    一.Console 1.System 命名空间中的 Console 类提供了一个函数 ReadLine(),用于接收来自用户的输入,并把它存储到一个变量中. int num; num = Conver ...

  3. HDU3535——AreYouBusy

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3535 题目意思:给出两个数n,T,分别表示有n个任务集合,T的总时间,对于每个任务集合有两个属性m和t ...

  4. 服务器端IO模型的简单介绍及实现

    https://mp.weixin.qq.com/s?src=3&timestamp=1541726441&ver=1&signature=xPSye3v7miF7aVeLHb ...

  5. 购物车删除商品,总价变化 innerHTML = ''并没有删除节点,内容仍存在

    w元素的上的下. function deleteLi(tmpId) { //document.getElementById(tmpId).innerHTML = ''; var wdel = docu ...

  6. 浏览器端js处理or直接冗余至服务器php处理?

    w交给客户端浏览器js处理,减少向服务器的提交字节.精简处理逻辑.

  7. Spark Streaming源码分析 – DStream

    A Discretized Stream (DStream), the basic abstraction in Spark Streaming, is a continuous sequence o ...

  8. svn搭建(linux下)

    安装svn: 依赖包: yum install openssl openssl-devel 问题(可以忽略不计):configure: WARNING: we have configured with ...

  9. MapReduce自定义InputFormat和OutputFormat

    一.自定义InputFormat 需求:将多个小文件合并为SequenceFile(存储了多个小文件) 存储格式:文件路径+文件的内容 c:/a.txt I love Beijing c:/b.txt ...

  10. Go学习笔记一:解析toml配置文件

    本文系作者原创,转载请注明出处https://www.cnblogs.com/sonofelice/p/9085291.html . 一些mysql或者日志路径的信息需要放在配置文件中.那么本博文主要 ...