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)+;
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定时器(注解的形式)的更多相关文章
- Spring定时器注解配置
spring-task.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...
- 注解式开发spring定时器
1:spring 配置文件中增加这句 <task:annotation-driven/> 2:确保扫描程序能够扫描后 下面第3步骤的java类 <context:co ...
- spring的注解形式:@Repository、@Service、@Controller,
Spring的注解形式:@Repository.@Service.@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean. @Repository.@Service.@C ...
- spring 基于注解的@Scheduled和quartz定时器两种实现
一.使用quartz 1.由于我的项目jar包使用的maven托管的,在pom文件中加入quartz的依赖就可以 2.配置quartz-context.xml,确保xml文件能被加载到 <?xm ...
- spring定时器,定时器一次执行两次的问题
Spring 定时器 方法一:注解形式 配置文件头加上如下: xmlns:task="http://www.springframework.org/schema/task" htt ...
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- spring定时器,当遇见半小时的情况时
spring定时器遇见半小时的解决方法(这里只提供注解方式) @Scheduled(fixedRate=6000000)//每隔100分钟执行方法 fixedRate的值是毫秒
- atititt.java定时任务框架选型Spring Quartz 注解总结
atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...
- 使用轻量级Spring @Scheduled注解执行定时任务
WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了 ...
- spring定时器用Annotation兑现
spring定时器用Annotation实现 0人收藏此文章, 我要收藏发表于3个月前 , 已有46次阅读 共0个评论 1.ApplicationContext.xml配置 a).需要在xmlns里面 ...
随机推荐
- 160316、实时处理oracle数据库中表的数据变化
http://blog.csdn.net/as339000204/article/details/45390727 近期接受项目需求,需要实时处理oracle数据库中表的数据变化,首先想到的是 ...
- Piwik网站访问统计软件安装
Piwik是一个PHP和MySQL的开放源代码的Web统计软件. 它给你一些关于你的网站的实用统计报告,比如网页浏览人数, 访问最多的页面, 搜索引擎关键词等等… Piwik拥有众多不同功能的插件,你 ...
- 修改mysql root的秘密
修改mysql root的秘密 ');
- Oracle Schema Objects——Tables——TableStorage
Oracle Schema Objects Table Storage Oracle数据库如何保存表数据? Oracle Database uses a data segment in a table ...
- sersync+rsync原理及部署
标签:sersync+rsync部署文档 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liubao0312.blog.51ct ...
- Storm编程模型及组件流程图
一.Storm编程模型 二.Storm组件流程图
- LeetCode_Isomorphic Strings
Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are isom ...
- Ubutun使用记录——语系错误(转)
add by zhj: 对原文有修改,原文是在创建用户时出现的问题,而我是在使用psql时出现的, 但问题是相同的. 原文:http://www.douban.com/note/362250557/ ...
- python数据分析基础——numpy和matplotlib
numpy库是python的一个著名的科学计算库,本文是一个quickstart. 引入:计算BMI BMI = 体重(kg)/身高(m)^2假如有如下几组体重和身高数据,让求每组数据的BMI值: w ...
- Incorrect key file for table ' '; try to repair it
场景:为有150W的数据表增加字段时,报错 解决:在my.ini配置临时目录configure tmpdir. Where MySQL Stores Temporary Files